Measurement Studio for VB6

Showing results for 
Search instead for 
Did you mean: 

Do you know how to save a VB Form as JPEG image?

I have a form displaying graph and knob (dial meter) output that is output only. I want to save the form "image" as a JPEG file (or other picture format) so I can display it in a web page for info purposes only. I plan to "refresh/rewrite" the JPEG every minute of so. I have seen software that does this. It is nice for presenting info in the browser (to non wintel machines for example).

Any idea how to do it?

0 Kudos
Message 1 of 7
The best place to post this question is probably on the microsoft vb newsgroups as you'll be more likely to get a good response. I have a couple of partial solutions that you could use to get a complete solution. I've got some code that you can use to capture a window into a bitmap. My code isn't really optimal for your purpose in that if the form got obscured by another window you would actually capture the obscuring windows graphics, but it still might work. I can't really take full credit for it since I'm sure that I found at least some part of it somewhere on the Internet. I can post it here if you need it.

The second piece, the conversion to jpeg, can be pretty easily done through intel's jpeg library. I used to have some code laying around that used
it but I don't know where it is anymore. That said, the libary and sample code can be obtained from intel's web site (just search for "jpeg library"). There's functions in that library to convert a bitmap handle or bitmap file into a jpeg.

You might also try doing a google search or looking on some of the vb source code sites (ex. for this information. You may not find a complete solution but you can probably piece one together.
0 Kudos
Message 2 of 7
Found a reference to an MS KB, but you might need to do the conversion to JPEG using some other libraries
(like the Intel libraries mentioned);en-us;Q240653

Hope this helps

Bilal Durrani
Bilal Durrani
0 Kudos
Message 3 of 7
Also, if you're using VB.NET, this is pretty easy ... something like this would work:

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 SaveFormToFile(ByVal sourceForm As Form, ByVal filename As String)
Dim formGraphics As Graphics = sourceForm.CreateGraphics()

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)



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

If you copied and pasted this to your form, you could then save the form to a .jpg file anytime you want by passing the Me reference in for this first parameter and the filename that you want to save it to. For example, if you wanted to create a .jpg file in the root directory of your C drive called "Test.jpg":

SaveFormToFile(Me, "Test.jpg")

- Elton
Message 4 of 7

Thanks!. I used this code. It doesnt shows any error.When I run,  getting a blacked picture saved. I didnt changed any part of the coding. Please helpppp me

0 Kudos
Message 5 of 7

I have much simpler solution


Isert a Panel say panel1 including the area you want to print. You can change

the property of the panel like  Panel1.Dock = Fill if you want to print the whole form. the insert the following code


Private Sub SaveFormToFile(ByVal filename As String)
Dim bmp As New Bitmap(Panel1.Width, Panel1.Height)

Panel1.DrawToBitmap(bmp, Panel1.DisplayRectangle)

bmp.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg)

End Sub


You can save the form by calling the following




Hope this will work


0 Kudos
Message 6 of 7

Please correct the sily mistake as


0 Kudos
Message 7 of 7