Search This Blog

Wednesday, July 8, 2009

How to create a image of form in vb.net?

'DLL FUNCTION USED TO CREATE A FORM IMGAE
Public Declare Function BitBlt Lib "gdi32.dll" Alias _
"BitBlt" (ByVal hdcDest As IntPtr, _
ByVal nXDest As Integer, ByVal nYDest As _
Integer, ByVal nWidth As Integer, _
ByVal nHeight As Integer, ByVal _
hdcSrc As IntPtr, ByVal nXSrc As Integer, _
ByVal nYSrc As Integer, _
ByVal dwRop As System.Int32) As Long

Public formImage As Bitmap
Public Sub GetFormImage()
'CRATING A IMAGE OF THE FORM TO PRINT
' print_Graphs_cpy2 is a form
Dim g As Graphics = print_Graphs_cpy2.CreateGraphics

Dim s As Size = print_Graphs_cpy2.Size
formImage = New Bitmap(s.Width - 50, s.Height, g)
Dim mg As Graphics = Graphics.FromImage(formImage)
Dim dc1 As IntPtr = g.GetHdc
Dim dc2 As IntPtr = mg.GetHdc
' added code to compute and capture the form
' title bar and borders
Dim widthDiff As Integer = _
(print_Graphs_cpy2.Width - print_Graphs_cpy2.ClientRectangle.Width - 50)
Dim heightDiff As Integer = _
(print_Graphs_cpy2.Height - print_Graphs_cpy2.ClientRectangle.Height)
Dim borderSize As Integer = widthDiff \ 2
Dim heightTitleBar As Integer = heightDiff - borderSize
BitBlt(dc2, 0, 0, _
print_Graphs_cpy2.ClientRectangle.Width - 50 + widthDiff, _
print_Graphs_cpy2.ClientRectangle.Height + heightDiff, dc1, _
0 - borderSize, 0 - heightTitleBar, 13369376)

g.ReleaseHdc(dc1)
mg.ReleaseHdc(dc2)

formImage.Save("c:\grpah.jpg")

End Sub

No comments:

Post a Comment