Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a GIF from a VB Form layout 2

Status
Not open for further replies.

mmbsports

Programmer
Oct 25, 2005
22
US
Basically, I have a form that has moveable objects on it. Once the user moves the objects in the location he/she would want them to be in, I want my program to take a snapshot (of sorts) of the form in order to create a GIF so I can place the GIF on a web page for viewing...and I'll save the GIF on the harddrive.

I reviewed the MS KB and from there it almost reads as if this is possible, but I have ran into a deadend. The examples were more in VB.NET or ASP.

Thanks for any help this forum might provide.
 
You could use BitBlt to create a bitmap image of the form - you would then just have to convert it to whatever format you want and save it on the web server.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
A valuable member of this forum figured out how to do this on the fly.

refer to thread222-1157687 for complete instructions.

It is possible to create a bmp within a picturebox containing with dynamic labels and other objects. the code provided is plug and play!! Outstanding Code!
 
That's pretty much what I suggested above ("You could use BitBlt to create a bitmap image of the form") and as far as I can tell (hopefully anyway!), the following doesn't haven't any limitations with labels etc.

Here was my attempt:

Code:
Public Class clsScreenGrab
    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
    Private Const SRCCOPY As Integer = &HCC0020

    Public Function SaveImage(ByVal vbForm As Form) As Bitmap

        ' Create the graphics object
        Dim g_form As Graphics = vbForm.CreateGraphics

        ' Create a bitmap
        Dim b As New Bitmap(vbForm.ClientSize.Width, vbForm.ClientSize.Height, g_form)
        Dim g_image As Graphics = g_form.FromImage(b)
        Dim bm_hdc As IntPtr = g_image.GetHdc

        ' Get the form's handle
        Dim f_intptr As IntPtr = g_form.GetHdc

        ' Use BitBlt to copy the forms image to the bitmap
        BitBlt(bm_hdc, 0, 0, vbForm.ClientSize.Width, vbForm.ClientSize.Height, f_intptr, 0, 0, SRCCOPY)
        g_form.ReleaseHdc(f_intptr)
        g_image.ReleaseHdc(bm_hdc)

        ' Return the result.
        Return b

    End Function
End Class

Then, to call the function, you just create a new instance of the class and pass the form to the SaveImage function e.g.

Code:
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim screenGrab As New clsScreenGrab
        Dim bm As Bitmap = screenGrab.SaveImage(Me)
        bm.Save("c:\myimage.gif", System.Drawing.Imaging.ImageFormat.Gif)
    End Sub


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I did so much research on the net, and I know there are allot of programmers looking for such a code like the one you wrote. What I like about your code is that you don't need a picturebox. You can have the moveable objects on the form and then save the form as a bitmap...probably less code as well.

I hope other programmers find this post while searching for (create bitmap or image from screen or visual basic form layout) because it will save them HOURS of researching and heartache.

GREAT JOB ca8msm!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top