PDA

Click to See Complete Forum and Search --> : How to save content in PictureBox?


maychia
Jan 17th, 2003, 11:27 AM
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? :)

hellswraith
Jan 17th, 2003, 02:50 PM
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.

maychia
Jan 19th, 2003, 10:07 AM
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?

Pirate
Jan 19th, 2003, 11:28 AM
as simple as this :
this code goes under you CmdSave Button :


PictureBox1.Image.Save("C:\MypicContent.bmp")

I hope this what you mean!

hellswraith
Jan 19th, 2003, 11:31 AM
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.

hellswraith
Jan 19th, 2003, 11:32 AM
Just a little late...lol.

maychia
Jan 20th, 2003, 09:49 AM
:) do u know how to save it by using savedialogbox?

hellswraith
Jan 20th, 2003, 10:00 AM
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/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformssavefiledialogclasstopic.asp

maychia
Jan 20th, 2003, 10:31 AM
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

Pirate
Jan 20th, 2003, 11:02 AM
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 .

MrPolite
Jan 20th, 2003, 01:20 PM
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?
:rolleyes:

maychia
Jan 21st, 2003, 06:11 AM
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


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

here is my drawing application (attachment), can you do the coding to save it? plsss... :(

Pirate
Jan 21st, 2003, 10:21 AM
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.:rolleyes:

MrPolite
Jan 21st, 2003, 04:11 PM
change mouse move to:
Private Sub picDraw_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picDraw.MouseMove
If shouldpaint Then
Dim graphic As Graphics = createGraphicsEx(picDraw)
graphic.FillEllipse(New SolidBrush(_BrushColor), e.X, e.Y, _BrushSize, _BrushSize)
graphic.Dispose()
picDraw.Invalidate()
End If
End Sub

createGraphicsEx is a function I wrote:
' Returns a Graphics object to be used for drawing on the picturebox
Private Function createGraphicsEx(ByVal picbox As PictureBox) As Graphics
' If the picturebox's image is nothing, create a bitmap the same size as
' the picturebox
If picbox.Image Is Nothing Then
picbox.Image = New Bitmap(picbox.ClientSize.Width, picbox.ClientSize.Height)
Dim gr As Graphics = Graphics.FromImage(picbox.Image)
gr.Clear(picbox.BackColor)
Return gr
Else
Return Graphics.FromImage(picbox.Image)
End If
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 :)

Pirate
Jan 21st, 2003, 04:28 PM
I was wondering If I used Form1_Paint event , does this would solve it ? :rolleyes:

MrPolite
Jan 21st, 2003, 04:40 PM
Originally posted by pirate
I was wondering If I used Form1_Paint event , does this would solve it ? :rolleyes: 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

Pirate
Jan 21st, 2003, 09:16 PM
stupid thought then :D . cool:D

maychia
Jan 22nd, 2003, 10:32 AM
confusing~

MrPolite..then what should i code in cmdSave?

MrPolite
Jan 22nd, 2003, 08:19 PM
Originally posted by maychia
confusing~

MrPolite..then what should i code in cmdSave? aah sorry:D

Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
picDraw.Image.Save("C:\test.png", Imaging.ImageFormat.Png)
picDraw.Image.Save("C:\test.bmp")
End Sub

two examples :) I think it saves it as BMP by default

maychia
Jan 23rd, 2003, 10:15 AM
i though using SaveFileDialog to save????

MrPolite
Jan 23rd, 2003, 10:58 PM
Originally posted by maychia
i though using SaveFileDialog to save???? eeh hellswraith already showed you. Here's another example: Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
Dim bmp As Bitmap = picDraw.Image
' Picturebox.Image is initially set to Nothing. You can set it to a new
' bitmap during startup if you want to aviod this
If bmp Is Nothing Then
MessageBox.Show("Image doesnt exist")
Else
SaveFileDialog1.Filter = "Bitmap (*.bmp)|*.bmp"
' Make sure the user pressed OK
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
bmp.Save(SaveFileDialog1.FileName)
End If
End If
End Sub

maychia
Jan 24th, 2003, 09:59 AM
Mr Polite & hellswraith
Billion thanks to both of you~!
Hopefully it run well after this~!

Thank you thank you~ Thank You~~;) :)

:p

maychia
Jan 26th, 2003, 09:56 AM
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
:o

hellswraith
Jan 26th, 2003, 12:17 PM
I think for you to clear the image of the picture box, you just need to set it to nothing.

MrPolite
Jan 26th, 2003, 01:07 PM
if you have an instance of the Graphics object, then you can do grahpic.clear([color])

maychia
Jan 27th, 2003, 10:57 AM
:o 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


:rolleyes:

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