Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[How To] Use the DrawToBitmap function in vb.net
04-30-2012, 08:39 PM (This post was last modified: 04-30-2012 08:43 PM by Hefin.)
Post: #1
Use the DrawToBitmap function in vb.net

The DrawToBitmap function accepts two parameters ,
1) bitmap
Which accepts the object of bitmap where it will draw to.
2) targetBounds
Which specifies the size of the drawing.

The main use of the function is like a Screen Shot or to draw the specified bounds to Image, which can be saved in different formats.

This function can be found on most of the controls including the form. So this means you can make a complete form and the controls inside it as a Image and save it.

There are many cases where you could use this function some of the examples are
If you want the whole form to be saved as a image.
If you want only the image of the Panel and the controls inside it.

How to Use the Code
  1. Printing a Form : All you need to do is the following.
    Code:
    Dim bmp As New Bitmap(Me.Width, Me.Height)
            Me.DrawToBitmap(bmp, Me.ClientRectangle)
            bmp.Save("D:\sample.png", ImageFormat.Png)
    a) First you create a Object of the Bitmap class which is comes from the System.Drawing Namespace ,
    b) Then you call the Me.DrawToBitmap function and pass the object of Bitmap and then pass the size , in this case i used Me.ClientRectangle which gives the exact client area of the control.
    c) Then use the Save function to save the captured image to the disk , you can use the ImageFormat enum to specify the type of image you want to save.
    The ImageFormat enum is in the System.Drawing.Imaging Namespace.
  2. Printing a Panel or Printing a Single Control
    You use the same code but instead of the Me you use the name of the Panel or the Name of the control.


Please ask if you have any doubts.
Find all posts by this user
Add Thank You Quote this message in a reply

Advertisements
Post Reply 


[-]
Share/Bookmark (Show All)
Facebook Linkedin Technorati Twitter Digg MySpace Delicious

Forum Jump:


User(s) browsing this thread: 1 Guest(s)