Results 1 to 26 of 26

Thread: How to save content in PictureBox?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2003
    Location
    USA
    Posts
    31

    Angry How to save content in PictureBox?

    I created one simple drawing program. Free hand drawn in the picturebox and wanted to save the content as image file

    Can you teach me?
    Best Regards,
    May C

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Create a Bitmap object and store the picture in there. Then call the bitmaps save method to save the image to file.

    There might even be a save method for the picturebox.....not sure though.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2003
    Location
    USA
    Posts
    31
    i can't really understand what you mean...
    let say... my picturebox is picDRAW, and i created a graphic... using brush to draw oon the picture box

    then a cmdSAVE..

    inside the cmdSAVE..what code should i write to save the content in picturebox as IMAGE file?
    Best Regards,
    May C

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    as simple as this :
    this code goes under you CmdSave Button :

    VB Code:
    1. PictureBox1.Image.Save("C:\MypicContent.bmp")
    Ihopethiswhatyoumean!

  5. #5
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Code:
            Dim bm As New Bitmap(PictureBox1.Image)
            bm.Save(Application.StartupPath & "\myimage.jpg")
    You might want to make sure the picturebox isn't empty first. It will throw an error if it is.

  6. #6
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Just a little late...lol.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jan 2003
    Location
    USA
    Posts
    31

    Lightbulb

    do u know how to save it by using savedialogbox?
    Best Regards,
    May C

  8. #8
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim result As String
            Dim bm As New Bitmap(PictureBox1.Image)
    
            SaveFileDialog1.ShowDialog()
            result = SaveFileDialog1.FileName
    
            bm.Save(result)
        End Sub
    There is more you can do with the savefiledialog. Here is a link to start you out:
    http://msdn.microsoft.com/library/de...classtopic.asp

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jan 2003
    Location
    USA
    Posts
    31
    An unhandled exception of type 'System.NullReferenceException' occurred in system.drawing.dll

    Additional information: Object reference not set to an instance of an object.


    I got this error when i execute the program.it highlighted at
    Dim bm As New Bitmap(PictureBox1.Image)

    what's the problem
    Best Regards,
    May C

  10. #10
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by maychia
    An unhandled exception of type 'System.NullReferenceException' occurred in system.drawing.dll

    Additional information: Object reference not set to an instance of an object.


    I got this error when i execute the program.it highlighted at
    Dim bm As New Bitmap(PictureBox1.Image)

    what's the problem
    just leave out the keyword New .

  11. #11
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by maychia
    An unhandled exception of type 'System.NullReferenceException' occurred in system.drawing.dll

    Additional information: Object reference not set to an instance of an object.


    I got this error when i execute the program.it highlighted at
    Dim bm As New Bitmap(PictureBox1.Image)

    what's the problem
    as he said you have to make sure the picturebox.image isnt = to Nothing

    you could do something like this

    bm= new bitmap (picbox.clientsize.width, picbox.clientsize.height)

    I think it's a little different to pass the picturebox.image to the bitmap's constructor though, isnt it?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Jan 2003
    Location
    USA
    Posts
    31
    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click

    Dim save As String
    Dim picture As New Bitmap(picDraw.ClientSize.Width, picDraw.ClientSize.Height)

    With SaveFileDialog1
    .Filter = "Windows Bitmap Files(*.BMP)|*.BMP|Jpeg Files (*.jpg)|*.JPG|GIF (*.gif)|*.GIF|TIFF (*.tif)|*.TIF|PNG (*.png)|*.PNG|Icon Files(*.ico)|*.ICO"
    .FilterIndex = 1
    .OverwritePrompt = True
    End With
    SaveFileDialog1.ShowDialog()
    save = SaveFileDialog1.FileName

    picture.Save(save)
    End Sub


    correct ?
    It show nothing after i saved it...
    tired with this saving method...pls teach me


    here is my drawing application (attachment), can you do the coding to save it? plsss...
    Attached Files Attached Files
    Best Regards,
    May C

  13. #13
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    strange behavior :
    when I draw in the picturebox and any window covers the area of drawing then it's erased.in other word , the picturebox is not holding its content.First you have to fix this bug then I don't think you would face problems.

  14. #14
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    change mouse move to:
    VB Code:
    1. Private Sub picDraw_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picDraw.MouseMove
    2.         If shouldpaint Then
    3.             Dim graphic As Graphics = createGraphicsEx(picDraw)
    4.             graphic.FillEllipse(New SolidBrush(_BrushColor), e.X, e.Y, _BrushSize, _BrushSize)
    5.             graphic.Dispose()
    6.             picDraw.Invalidate()
    7.         End If
    8.     End Sub

    createGraphicsEx is a function I wrote:
    VB Code:
    1. ' Returns a Graphics object to be used for drawing on the picturebox
    2.     Private Function createGraphicsEx(ByVal picbox As PictureBox) As Graphics
    3.         ' If the picturebox's image is nothing, create a bitmap the same size as
    4.         ' the picturebox
    5.         If picbox.Image Is Nothing Then
    6.             picbox.Image = New Bitmap(picbox.ClientSize.Width, picbox.ClientSize.Height)
    7.             Dim gr As Graphics = Graphics.FromImage(picbox.Image)
    8.             gr.Clear(picbox.BackColor)
    9.             Return gr
    10.         Else
    11.             Return Graphics.FromImage(picbox.Image)
    12.         End If
    13.     End Function
    basically the only thing different is that instead of saying picbox.creategraphics, you are calling graphics.fromimage. That's all. I just made that a function so that you could use it again if you want. Also you shouldnt be drawing this with an ellipse. You should save the mouse position on each mousemove event and then draw a line from the last mouse position to the current mouse position.
    You can save the picture now, and it wont disappear again
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  15. #15
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I was wondering If I used Form1_Paint event , does this would solve it ?

  16. #16
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by pirate
    I was wondering If I used Form1_Paint event , does this would solve it ?
    if you want to use form_paint, then you have to know all the points needed to redraw the picture. OR you could draw the picture on a separate bitmap and repaint that every time on the paint event, which doesnt make any sense. This way you just draw the picture on the picturebox. When you use .CreateGraphics, it will not actually draw on the picturebox.Image object, that's why he couldnt save the picture
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  17. #17
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    stupid thought then . cool

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Jan 2003
    Location
    USA
    Posts
    31

    Unhappy

    confusing~

    MrPolite..then what should i code in cmdSave?
    Best Regards,
    May C

  19. #19
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by maychia
    confusing~

    MrPolite..then what should i code in cmdSave?
    aah sorry
    VB Code:
    1. Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
    2.         picDraw.Image.Save("C:\test.png", Imaging.ImageFormat.Png)
    3.         picDraw.Image.Save("C:\test.bmp")
    4.     End Sub
    two examples I think it saves it as BMP by default
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  20. #20

    Thread Starter
    Junior Member
    Join Date
    Jan 2003
    Location
    USA
    Posts
    31
    i though using SaveFileDialog to save????
    Best Regards,
    May C

  21. #21
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by maychia
    i though using SaveFileDialog to save????
    eeh hellswraith already showed you. Here's another example:
    VB Code:
    1. Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
    2.         Dim bmp As Bitmap = picDraw.Image
    3.         ' Picturebox.Image is initially set to Nothing. You can set it to a new
    4.         ' bitmap during startup if you want to aviod this
    5.         If bmp Is Nothing Then
    6.             MessageBox.Show("Image doesnt exist")
    7.         Else
    8.             SaveFileDialog1.Filter = "Bitmap (*.bmp)|*.bmp"
    9.             ' Make sure the user pressed OK
    10.             If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
    11.                 bmp.Save(SaveFileDialog1.FileName)
    12.             End If
    13.         End If
    14.     End Sub
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  22. #22

    Thread Starter
    Junior Member
    Join Date
    Jan 2003
    Location
    USA
    Posts
    31

    Talking

    Mr Polite & hellswraith
    Billion thanks to both of you~!
    Hopefully it run well after this~!

    Thank you thank you~ Thank You~~

    Best Regards,
    May C

  23. #23

    Thread Starter
    Junior Member
    Join Date
    Jan 2003
    Location
    USA
    Posts
    31

    Question

    GREAT !!!!!!!! IT"S WORK!!!

    SO weird....

    Why I can't change the background color for the second time?
    After the program start, I can change the background color if I draw
    nothing on the picturebox, after I draw something there..
    the background color not working?!~

    How to clear all the content in the picturebox?
    Previously, I using 'picDraw.invalidate'
    now it's not working anymore
    Best Regards,
    May C

  24. #24
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    I think for you to clear the image of the picture box, you just need to set it to nothing.

  25. #25
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    if you have an instance of the Graphics object, then you can do grahpic.clear([color])
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  26. #26

    Thread Starter
    Junior Member
    Join Date
    Jan 2003
    Location
    USA
    Posts
    31

    Wink

    Private Sub backC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles backC.Click
    Dim colorBox As ColorDialog = New ColorDialog()
    Dim result As DialogResult = colorBox.ShowDialog()
    picDraw.BackColor = Nothing


    If result = DialogResult.Cancel Then
    Return
    End If

    picDraw.BackColor = colorBox.Color

    End Sub




    ehh... cannot huh~
    it can change the color only ONCE...if the ERASE button is picDraw.Image=NOTHING.
    then i can only erase all then only can change backcolor
    Best Regards,
    May C

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