Make image file from windows form

ilimax

Member
Joined
Sep 2, 2005
Messages
5
Programming Experience
Beginner
I am making drawing form. I already made it and I am able to draw on the form. I want to make one button which will save image of drawing form. So, draw on the form then save as JPEG in C drive. Does anybody have idea how to make this?
 
Draw the same thing you draw to form to the bitmap. Paint's e.Graphics can be used same as a Graphics.FromImage. Consider this very accurate pseudo code:
VB.NET:
Sub Form_Paint(sender, e) Handles Me.Paint
   MyDrawing(e.Graphics)
End Sub

Sub MyDrawing(g As Graphics)
  g.DrawThis()
  g.DrawThat()
End Sub

Sub SaveImage()
   Dim bmp As New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
   Dim g As Graphics = Graphics.FromImage(bmp)
   MyDrawing(g)
   g.Dispose()
   bmp.Save("filename.bmp")
   bmp.Dispose()
End Sub

Sub SaveButton_Click(sender,e) Handles SaveButton.Click
   SaveImage()
End Sub
If you have controls also on form that you want to include in image you have to take a screenshot of the form, Graphics.CopyFromScreen (*) can do that. (* new in .Net 2.0, upgrade or find an old screenshot class)
 
Auto redraw

Well, How do i redraw an image i.e. in case im drawing a chess piece and want to show the moved position. For that, how should im able to draw the image onto a new positon. But how do i delete the previous location image in vb.net . Or, is there a chance that i could use any move method.
Please respond thanks !
raghu!
 
Repositioning of image

Well clearly
I've got draw the image on a new location. So, to do that, I will have to delete the image at the old position and redraw it on the new position(using graphics.Drawimage(..)). But, I'm able to draw the piece only on the new position and am unable to delete the image from the old position.
Please lend me the code for that? thank you :)
 
Didn't you read jmcilhinneys reply? Or didn't you understand it?
t, I'm able to draw the piece only on the new position and am unable to delete the image from the old position.
The point of using the Paint event is that what you draw there is what is drawn and nothing else. If you're using Paint event (as you should) then what you explain is not possible unless you are drawing at two locations for each call of Paint event, or invalidating wrong areas. To control a location from elsewhere than Paint event handler it must be declared at a higher level than locally in the method, like this:
VB.NET:
Private p As Point

Sub MyDrawing(g As Graphics)
  g.DrawThis(p)
  g.DrawThat(p)
End Sub

Sub Timer_Tick(sender,e) Handles timer.Tick
  p.x += 1 
  Me.Refresh()
End Sub
In this example a timer is constantly changing the p Point location and telling the form to repaint, the Painting method uses the p Point in its drawing.
 
Back
Top