Sunday, December 18, 2005

"Create Screen Dump of a Windows Form"

In the Minority (Ken Getz) : Create Screen Dump of a Windows Form:

"Recently, friend Paul Sheriff sent this email:

Remember in VB6 we had the Me.Print and it would print the bitmap of the form? Is there anyway to do that in VB.NET?

I sat on it for a while, knowing that it would be fun to work out, once I got to it. Turns out, in VS 2005, it's really easy. The Control class adds a DrawToBitmap method, so you can create a method like this to do the work:

Public Function GetFormImage(ByVal frm As Form) As Bitmap
Dim bmp As Bitmap
Using g As Graphics = Me.CreateGraphics()
Dim sz As Size = frm.Size
bmp = New Bitmap(sz.Width, sz.Height, g)
frm.DrawToBitmap(bmp, New Rectangle(0, 0, sz.Width, sz.Height))
End Using
Return bmp
End Function

Pass in a form reference, and you've got yourself a bitmap of the form's contents. ..."


Interesting... I can see a couple places where this could help. Think "Support Request" or "Bug Report" function/email/upload.

I hate having to train users to take screen-shots of a given app when requesting help, reporting a bug, etc. So instead build the capture into the app itself, with OS/app version/etc info and upload it all to a web service, published via RSS feed, etc (or email... but email is so 90's... and I have enough crap in my inbox already).

All in all, being able to easily grab a form as a bitmap is pretty cool...

Check out the entire post for the VS2003 version.

1 comment:

Anonymous said...

How about making it complete and extending it by giving a function to make a bmp file?

Public Sub WriteBMPToFile(ByVal in_BMP as Bitmap, ByVal in_strFullFileName as String)
' Greg supplies code...
End Sub