Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have design a form and set a label on form.
now i want to save a form a as a Tiff image any of a location.

VB
Private Shared Function 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 Integer _
) As Boolean
End Function
 

Private Shared SRCCOPY As Integer = &HCC0020
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim path As String = "C:\Users\KavtikP\Desktop\KpDesk\Test.Tiff"
SaveFormToFile(Me, path)
 
End Sub
 
Private Sub SaveFormToFile(ByVal sourceForm As Form, ByVal filename As String)
Dim formGraphics As Graphics = sourceForm.CreateGraphics()
 
Dim bufferRect = sourceForm.ClientRectangle
 
Dim buffer As Image = New Bitmap(bufferRect.Width, bufferRect.Height, formGraphics)
Dim bufferGraphics As Graphics = Graphics.FromImage(buffer)
 
Dim formDC As IntPtr = formGraphics.GetHdc()
Dim bufferDC As IntPtr = bufferGraphics.GetHdc()
 
BitBlt(bufferDC, 0, 0, bufferRect.Width, bufferRect.Height, formDC, 0, 0, SRCCOPY)
 
bufferGraphics.ReleaseHdc(bufferDC)
formGraphics.ReleaseHdc(formDC)
 
bufferGraphics.Dispose()
formGraphics.Dispose()
 
''buffer.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg)
 
buffer.Save(filename, System.Drawing.Imaging.ImageFormat.Tiff)
 
buffer.Dispose()
End Sub
 
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
 
Dim screenRect As Rectangle = Screen.PrimaryScreen.WorkingArea
Dim myBitmapScreenShot = New Bitmap(screenRect.Width, screenRect.Height)
Using targetGraphics As Graphics = Graphics.FromImage(myBitmapScreenShot)
targetGraphics.CopyFromScreen(0, 0, 0, 0, New Size(myBitmapScreenShot.Width, myBitmapScreenShot.Height))
End Using
End Sub
Posted
Updated 3-Aug-13 0:45am
v2
Comments
KAUTIKPATEL 3-Aug-13 6:27am    
given code generate print but,bt not label on print

1 solution

 
Share this answer
 
Comments
KAUTIKPATEL 3-Aug-13 6:27am    
Private Shared Function 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 Integer _
) As Boolean
End Function


Private Shared SRCCOPY As Integer = &HCC0020
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim path As String = "C:\Users\KavtikP\Desktop\KpDesk\Test.Tiff"
SaveFormToFile(Me, path)

End Sub

Private Sub SaveFormToFile(ByVal sourceForm As Form, ByVal filename As String)
Dim formGraphics As Graphics = sourceForm.CreateGraphics()

Dim bufferRect = sourceForm.ClientRectangle

Dim buffer As Image = New Bitmap(bufferRect.Width, bufferRect.Height, formGraphics)
Dim bufferGraphics As Graphics = Graphics.FromImage(buffer)

Dim formDC As IntPtr = formGraphics.GetHdc()
Dim bufferDC As IntPtr = bufferGraphics.GetHdc()

BitBlt(bufferDC, 0, 0, bufferRect.Width, bufferRect.Height, formDC, 0, 0, SRCCOPY)

bufferGraphics.ReleaseHdc(bufferDC)
formGraphics.ReleaseHdc(formDC)

bufferGraphics.Dispose()
formGraphics.Dispose()

''buffer.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg)

buffer.Save(filename, System.Drawing.Imaging.ImageFormat.Tiff)

buffer.Dispose()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Dim screenRect As Rectangle = Screen.PrimaryScreen.WorkingArea
Dim myBitmapScreenShot = New Bitmap(screenRect.Width, screenRect.Height)
Using targetGraphics As Graphics = Graphics.FromImage(myBitmapScreenShot)
targetGraphics.CopyFromScreen(0, 0, 0, 0, New Size(myBitmapScreenShot.Width, myBitmapScreenShot.Height))
End Using
End Sub

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900