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? :)
Printable View
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? :)
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.
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?
as simple as this :
this code goes under you CmdSave Button :
Ihopethiswhatyoumean!VB Code:
PictureBox1.Image.Save("C:\MypicContent.bmp")
You might want to make sure the picturebox isn't empty first. It will throw an error if it is.Code:Dim bm As New Bitmap(PictureBox1.Image)
bm.Save(Application.StartupPath & "\myimage.jpg")
Just a little late...lol.
:) do u know how to save it by using savedialogbox?
There is more you can do with the savefiledialog. Here is a link to start you out: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
http://msdn.microsoft.com/library/de...classtopic.asp
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 .Quote:
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 NothingQuote:
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
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:
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... :(
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:
change mouse move to:
VB Code:
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:
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.VB Code:
' 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
You can save the picture now, and it wont disappear again :)
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 pictureQuote:
Originally posted by pirate
I was wondering If I used Form1_Paint event , does this would solve it ? :rolleyes:
stupid thought then :D . cool:D
confusing~
MrPolite..then what should i code in cmdSave?
aah sorry:DQuote:
Originally posted by maychia
confusing~
MrPolite..then what should i code in cmdSave?
two examples :) I think it saves it as BMP by defaultVB Code:
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
i though using SaveFileDialog to save????
eeh hellswraith already showed you. Here's another example:Quote:
Originally posted by maychia
i though using SaveFileDialog to save????
VB Code:
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
Mr Polite & hellswraith
Billion thanks to both of you~!
Hopefully it run well after this~!
Thank you thank you~ Thank You~~;) :)
:p
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
I think for you to clear the image of the picture box, you just need to set it to nothing.
if you have an instance of the Graphics object, then you can do grahpic.clear([color])
: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