[Resolved][2005] Saving color information as a picture
I am writing a program that scans every pixel of a picture. The user will choose the picture they want to use and it will be displayed in a picture box. If the pixel being scaned isn't a certian color(s) specified by the user it will write the color and location of that pixel to a new picture that has the same dimentions as the picture being scaned. Every time another pixel that is not the specified color(s) is scaned it will also be added to the newly created picture which should eventually result in the picture that is being scaned being recreated execpt for the color(s) specified by the user. I also need the user to be able to choose what file type the new picture is.
I can do everything except create a new picture and save to it.
Any help?
Re: [2005] Saving color information as a picture
create a new bitmap, with the dimensions of your picture.
get hold of the picture's graphics object ( the pic, not the new bmp )
create a graphics object for the new bitmap ( Dim objGraphics As Graphics = Graphics.FromImage( your bitmap name here ) )
draw the coloured item at the same location on the new graphics object as it's location is on the pic.
when complete , save the bitmap ( bmp.Save ( " file path " )
Re: [2005] Saving color information as a picture
I created the graphics object, how exactly would I draw the color variable on the graphics object at the same location?
Re: [2005] Saving color information as a picture
use FillRectangle, you can specify the colour of the pixel as the 1st property & RectangleF as the 2nd ( there you would have the x / y position of your pixel & seeing as it's a pixel, you would specify 1 for the width & also for the height )
eg:
Code:
objGraphics.FillRectangle( " YOUR PIXEL COLOUR HERE " , New RectangleF(" X LOCATION HERE ", "Y LOCATION HERE " , 1, 1))
Re: [2005] Saving color information as a picture
One last thing, how do you use bmp.Save to save a graphics object?
Re: [2005] Saving color information as a picture
the name of your bitmap ( eg: if you called it bmp, it'd be bmp.Save("C:\your_file_path.JPG") )
Re: [2005] Saving color information as a picture
Code:
Dim imageFile As Image = Image.FromFile("C:\Result.bmp")
...
imageFile.Save("C:\Result.bmp")
gave me a genericGUD error, what exactly am i doing wrong?
Re: [2005] Saving color information as a picture
Ah i found the problem
Thanks for all your help!
Re: [Resolved][2005] Saving color information as a picture
I would take slightly different approach. I don’t think you need graphics object at all and using FillRectangle method is not really a good approach. Instead I would create bitmap with the size of the original image and would set the pixel colors by using bitmap objects “SetPixel” method. Also you can specify the image type when you save the bitmap object. Specifying the file extension is not enough. Here is an example:
vb Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim origBmp As Bitmap = CType(Me.PictureBox1.Image, Bitmap)
'New bitmap with the same size as the original image
Dim newBmp As New Bitmap(origBmp.Width, origBmp.Height, origBmp.PixelFormat)
'Scan the image and copy some pixels to the new bitmap
For y As Integer = 0 To origBmp.Height - 1
For x As Integer = 0 To origBmp.Width - 1
If origBmp.GetPixel(x, y) <> Color.Beige Then
newBmp.SetPixel(x, y, origBmp.GetPixel(x, y))
End If
Next x
Next y
'Save the image
newBmp.Save( _
My.Computer.FileSystem.SpecialDirectories.Desktop _
& "\newImag.bmp", Imaging.ImageFormat.Bmp)
End Sub