Results 1 to 9 of 9

Thread: [Resolved][2005] Saving color information as a picture

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Posts
    5

    [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?
    Last edited by Tyler121; Aug 17th, 2007 at 05:12 PM.

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    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 " )
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Posts
    5

    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?

  4. #4
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    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))
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Posts
    5

    Re: [2005] Saving color information as a picture

    One last thing, how do you use bmp.Save to save a graphics object?

  6. #6
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    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") )
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Posts
    5

    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?

  8. #8

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Posts
    5

    Re: [2005] Saving color information as a picture

    Ah i found the problem
    Thanks for all your help!

  9. #9
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim origBmp As Bitmap = CType(Me.PictureBox1.Image, Bitmap)
    3.         'New bitmap with the same size as the original image
    4.         Dim newBmp As New Bitmap(origBmp.Width, origBmp.Height, origBmp.PixelFormat)
    5.  
    6.         'Scan the image and copy some pixels to the new bitmap
    7.         For y As Integer = 0 To origBmp.Height - 1
    8.             For x As Integer = 0 To origBmp.Width - 1
    9.                 If origBmp.GetPixel(x, y) <> Color.Beige Then
    10.                     newBmp.SetPixel(x, y, origBmp.GetPixel(x, y))
    11.                 End If
    12.             Next x
    13.         Next y
    14.  
    15.         'Save the image
    16.         newBmp.Save( _
    17.         My.Computer.FileSystem.SpecialDirectories.Desktop _
    18.         & "\newImag.bmp", Imaging.ImageFormat.Bmp)
    19.     End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width