Solved

My customer needs to view a captured image of the input form.

Posted on 2008-09-29
5
225 Views
Last Modified: 2013-11-26
I have used VB to create an application with a user interface that needs to be viewed as an image.  My customer inputs several parameters and selections to initiate analysis but needs to view the input at a later date and would like it to be an image.  In the past I have used SendKeys.Send("({PRTSC})") but that is unrelable and doesn't work this time.  

The user doesn't have any application tools on their PC.

Does anyone know how to save a form as an image?
0
Comment
Question by:fsoucy
5 Comments
 
LVL 13

Expert Comment

by:SameerJagdale
ID: 22596422
have you checked
e.Graphics.CopyFromScreen(...) method?
0
 
LVL 25

Accepted Solution

by:
Luis Pérez earned 500 total points
ID: 22596430
Try the code below. The form itself (as well as any other control in .net framework 2.0 and above) has a method called DrawToBitmap.
Imports System.Drawing
Imports System.Drawing.Imaging
 
Public Class Form1
    Public Sub SaveFormImage(ByVal Path As String)
        'Create a new empty bitmap with form dimensions
        Dim oBitmap As New Bitmap(Me.Width, Me.Height)
        'Create a rectangle with form bounds
        Dim rect As New Rectangle(0, 0, Me.Width, Me.Height)
        'Draw the form onto the bitmap
        Me.DrawToBitmap(oBitmap, rect)
        'Save the bitmap in JPEG format
        oBitmap.Save(Path, ImageFormat.Jpeg)
        'Dispose the bitmap (no more needed)
        oBitmap.Dispose()
    End Sub
End Class

Open in new window

0
 
LVL 3

Expert Comment

by:Diveblue
ID: 22596483
<System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")> _
    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 System.Int32) As Boolean
    End Function

    Private Sub CaptureForm()
        Dim g1 As Graphics = Me.CreateGraphics()
        Dim MyImage As Bitmap = New Bitmap(Me.ClientRectangle.Width, Me.ClientRectangle.Height, g1)
        Dim g2 As Graphics = Graphics.FromImage(MyImage)
        Dim dc1 As IntPtr = g1.GetHdc()
        Dim dc2 As IntPtr = g2.GetHdc()
        BitBlt(dc2, 0, 0, Me.ClientRectangle.Width, Me.ClientRectangle.Height, dc1, 0, 0, 13369376)
        g1.ReleaseHdc(dc1)
        g2.ReleaseHdc(dc2)
        MyImage.Save("c:\temp\foo.jpg")
        MessageBox.Show("Finished Saving Image")
    End Sub 'Capture_Click
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 22596679
Hi fsoucy;

This snippet will do what you need.

        Dim controlImage As Bitmap = New Bitmap(Me.Width, Me.Height, Me.CreateGraphics())
        Dim g As Graphics = Graphics.FromImage(controlImage)
        g.CopyFromScreen(Me.Location, New Point(0, 0), New Size(Me.Width, Me.Height))
        controlImage.Save("MainForm.jpg", Imaging.ImageFormat.Jpeg)
        controlImage = Nothing
        g.Dispose()

Fernando
0
 

Author Closing Comment

by:fsoucy
ID: 31501166
This was easy, thanks.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
HTTPS jquery doesn't work 9 61
Capture logon name 13 73
Call an IPL program from vb.net and pass in a file location parmeter 2 20
Help with preventing downloading a zip file 10 37
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question