How can I save a vb form as Image?

  • 15 years ago

    I am a vb programmer. I want to save a form with it's content to different image format (like jpeg, bmp etc).


    I also want to save a picture box with it's content to different image format (like jpeg, bmp etc).


    Anybody help me in this regard?
    This is urgent for me. Any help will be appriciable.

  • 15 years ago

    Hi srbbd!


    You can take and save a picture of your form like this:

    Code:
    Option Explicit


    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
    ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)


    Private Const KEYEVENTF_KEYUP = &H2


    Private Function SaveFormPic() As Picture
       Dim pic As StdPicture
       Set pic = Clipboard.GetData(vbCFBitmap)
       keybdevent vbKeyMenu, 0, 0, 0
       keybd
    event vbKeySnapshot, 0, 0, 0
       DoEvents
       keybdevent vbKeySnapshot, 0, KEYEVENTFKEYUP, 0
       keybdevent vbKeyMenu, 0, KEYEVENTFKEYUP, 0
       DoEvents
       Set SaveFormPic = Clipboard.GetData(vbCFBitmap)
       Clipboard.SetData pic, vbCFBitmap
    End Function


    Private Sub Command1_Click()
       SavePicture Clipboard.GetData(vbCFBitmap), "C:\MyPic.jpg" 'picture location
    End Sub



    The example requires one command button. It will take a picture of your form and save it into file "c:\mypic.jpg". You can change that.


    Good luck.



    Best Regards,
    Programmer

  • 15 years ago

    Dear Sir


    Thanks for your help.
    But following code return "Invalid property value"


    SavePicture Clipboard.GetData(vbCFBitmap), "C:\MyPic.jpg" 'picture location


    Comments please.......



  • 15 years ago

    Hi srbbd!

    Quote:
    Dear Sir


    Thanks for your help.
    But following code return "Invalid property value"


    SavePicture Clipboard.GetData(vbCFBitmap), "C:\MyPic.jpg" 'picture location


    Comments please.......



    I'm sorry it didn't work. But this one will:

    Code:
    Option Explicit


    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
    ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)


    Private Const KEYEVENTF_KEYUP = &H2


    Private Function SaveFormPic() As Picture
      Dim pic As StdPicture
      Set pic = Clipboard.GetData(vbCFBitmap)
      keybdevent vbKeyMenu, 0, 0, 0
      keybd
    event vbKeySnapshot, 0, 0, 0
      DoEvents
      keybdevent vbKeySnapshot, 0, KEYEVENTFKEYUP, 0
      keybdevent vbKeyMenu, 0, KEYEVENTFKEYUP, 0
      DoEvents
      Set SaveFormPic = Clipboard.GetData(vbCFBitmap)
      Clipboard.SetData pic, vbCFBitmap
    End Function


    Private Sub Command1_Click()
      SavePicture SaveFormPic, "C:\MyPic.jpg" 'picture location
    End Sub



    Hope it works now...



    Best Regards,
    Programmer

  • 15 years ago

    Thank you for this code, it works (nearly) great!


    There is a problem though that I experience. I wrote an application that surfs to an URL in the Webbrowser object (Internet Explorer object) and makes a screenshot every 3 seconds. In about 1 of 10 calls to the function SaveFormPic, the function doesn't copy the screen to the clipboard, but sends it to the printer. This results in displaying the Print window (Select Printer window) and thus the applications hangs. Also when the Silent property of the Webbrowser object is set to true, the problem remains.


    What can be the cause or the solution? Any help will be appreciated. Thank you!


  • 15 years ago
    Hi jorrit77!

    I'm glad my code was helpful.

    Try clearing the clipboard each time the picture is taken and saved. That should fix the problem.

    The first example clears the Clipboard after the picture has been saved and the second one clears it before the picture has been taken or saved.

    Code:
    Option Explicit

    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
    ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

    Private Const KEYEVENTF_KEYUP = &H2

    Private Function SaveFormPic() As Picture
     Dim pic As StdPicture
     Set pic = Clipboard.GetData(vbCFBitmap)
     keybd_event vbKeyMenu, 0, 0, 0
     keybd_event vbKeySnapshot, 0, 0, 0
     DoEvents
     keybd_event vbKeySnapshot, 0, KEYEVENTF_KEYUP, 0
     keybd_event vbKeyMenu, 0, KEYEVENTF_KEYUP, 0
     DoEvents
     Set SaveFormPic = Clipboard.GetData(vbCFBitmap)
     Clipboard.SetData pic, vbCFBitmap
    End Function

    Private Sub Command1_Click()
     SavePicture SaveFormPic, "C:\MyPic.jpg" 'picture location
     Clipboard.Clear
    End Sub


    ...or like this:

    Code:
    Option Explicit

    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
    ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

    Private Const KEYEVENTF_KEYUP = &H2

    Private Function SaveFormPic() As Picture
     Clipboard.Clear
     Dim pic As StdPicture
     Set pic = Clipboard.GetData(vbCFBitmap)
     keybd_event vbKeyMenu, 0, 0, 0
     keybd_event vbKeySnapshot, 0, 0, 0
     DoEvents
     keybd_event vbKeySnapshot, 0, KEYEVENTF_KEYUP, 0
     keybd_event vbKeyMenu, 0, KEYEVENTF_KEYUP, 0
     DoEvents
     Set SaveFormPic = Clipboard.GetData(vbCFBitmap)
     Clipboard.SetData pic, vbCFBitmap
    End Function

    Private Sub Command1_Click()
     SavePicture SaveFormPic, "C:\MyPic.jpg" 'picture location
    End Sub


    One of these two samples should work.


    If the problem persists please let me know so I can fix it.
    -----------------------------------------------------
    Best Regards,
    Programmer
  • 15 years ago

    Hai Programmer,
    Thanks  for your code to save VB form in JPG fomat or a image.it works nicely for to save vb forms. i've a problem in that coding.after usaing that coding my keyboard locked. if double click on icon or file name,it doesn't open it.it shows the properties of icon or file.but i use the ctrl+alt+del key, the keyboard unlocked.


    my requirement is,
     i want to unlock the keyboard through VB coding after save the vb forms. i tried in many ways to do that.i didn't get.i can't the get syntax of keybd_event.
    could you help me?


    Thanks for help in advance.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Every language has an optimization operator. In C++ that operator is ‘//’”