Save Form as a bitmap

hawkseye

Active member
Joined
Dec 4, 2004
Messages
28
Programming Experience
Beginner
i have a dynamic form on which i draw different labels and images based on the values in my db (basically a report) its length is also unknown (screenshot attached). Please tell me if there is any way i can save this form as an image on my hard drive.
I basically have to print this report but cant find any way of doing it so thought of saving it as an image n then printing it
 

Attachments

  • myReport.gif
    myReport.gif
    23.8 KB · Views: 95

TPM

Well-known member
Joined
Dec 7, 2004
Messages
623
Location
CA
Programming Experience
3-5
If you create it as 1 large image you can use the save function that's built into the bitmap class, you can even save it as different file types (.Jpg .Gif etc)

I thought you had sorted the printing out?
 

Schenz

Well-known member
Joined
Sep 11, 2004
Messages
181
Location
Cincinnati, OH
Programming Experience
5-10
I believe that you want something like this. The following code will take a screenshot of the form as an image, then use that image on a PrintPreview Control. You can then print it from there.

I believe that this will only work if the form/image is about the size of one printed page

VB.NET:
' API call to help generate final screenshot
	Private Declare Auto Function BitBlt Lib "gdi32.dll" (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 Boolean

	' Variable to store screenshot
	Private bmpScreenshot As Bitmap

Private Sub btnPrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintPreview.Click, mnuFilePreview.Click

		GrabScreen()

		Try
			PrintPreviewDialog1.Document = Me.PrintDocument1
			PrintPreviewDialog1.ShowDialog()
		Catch es As Exception
			MessageBox.Show(es.Message)
		End Try

	End Sub

	Private Sub GrabScreen()
		' Performs a screenshot, saving results to bmpScreenshot
		Dim objGraphics As Graphics = Me.CreateGraphics
		Dim objSize As Size = Me.Size
		Const SRCCOPY As Integer = &HCC0020

		bmpScreenshot = New Bitmap(objSize.Width, objSize.Height, _
								   objGraphics)
		Dim objGraphics2 As Graphics = _
			objGraphics.FromImage(bmpScreenshot)
		Dim deviceContext1 As IntPtr = objGraphics.GetHdc
		Dim deviceContext2 As IntPtr = objGraphics2.GetHdc

		BitBlt(deviceContext2, 0, 0, Me.ClientRectangle.Width, _
		  Me.ClientRectangle.Height, deviceContext1, 0, 0, SRCCOPY)
		objGraphics.ReleaseHdc(deviceContext1)
		objGraphics2.ReleaseHdc(deviceContext2)
	End Sub

	Private Sub MyPrintDocument_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
		' Method that handles the printing
		Dim objImageToPrint As Graphics = e.Graphics
		objImageToPrint.DrawImage(bmpScreenshot, 0, 0)
		e.HasMorePages = False
	End Sub
 
Top Bottom