How to save a graphic from the Clipboard to a file by using Visual Basic .NET or Visual Basic 2005

Article translations Article translations
Article ID: 818410 - View products that this article applies to.
Expand all | Collapse all

On This Page

SUMMARY

This step-by-step article describes how to save the graphic that is loaded in the Clipboard as a bitmap file by using Visual Basic .NET or Visual Basic 2005. This article describes how to use the Clipboard class to do this.



Copy a Graphic from the Clipboard

  1. Run Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. Click New on the File menu and then click Project.
  3. Rename the application to ClipBoardDemo.
  4. Under Project types, click Visual Basic Projects. Under Templates, click Windows Application and then click OK.

    By default, Form1 is created.

    Note In Visual Studio 2005, click Visual Basic under Project Types.
  5. From the Toolbox, add a Button control to Form1.
  6. Right-click Form1 and then click View Code.
  7. Paste the following code in the Form1 class code.
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If Not System.Windows.Forms.Clipboard.GetDataObject() Is Nothing Then
                Dim oDataObj As IDataObject = System.Windows.Forms.Clipboard.GetDataObject()
                If oDataObj.GetDataPresent(System.Windows.Forms.DataFormats.Bitmap) Then
                    Dim oImgObj As System.Drawing.Image = oDataObj.GetData(DataFormats.Bitmap, True)
                    'To Save as Bitmap
                    oImgObj.Save("c:\Test.bmp", System.Drawing.Imaging.ImageFormat.Bmp)
                    'To Save as Jpeg
                    oImgObj.Save("c:\Test.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)
                    'To Save as Gif
                    oImgObj.Save("c:\Test.gif", System.Drawing.Imaging.ImageFormat.Gif)
                End If
            End If
        End Sub
  8. On the Build menu, click Build Solution.

    ClipBoardDemo.Exe is created.


Verify the Result

  1. Run the ClipBoardDemo.exe file application.

    Form1 appears.
  2. Press PRINT SCREEN to add the graphical image of the screen to the Clipboard.
  3. Click Button1.
  4. Three image files are created in c:\. The image files are Test.bmp, Test.gif, and Test.jpeg.
  5. Open c:\Test.bmp in Microsoft Paint. Verify the Print Screen image is present in the Test.bmp file.

REFERENCES

For more information about using the Clipboard class, visit the following Microsoft Web site: http://msdn2.microsoft.com/en-us/library/system.windows.forms.clipboard(vs.71).aspx

Properties

Article ID: 818410 - Last Review: November 26, 2007 - Revision: 2.5
APPLIES TO
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
Keywords: 
kbvs2005swept kbvs2005applies kbwindowsforms kbclipboard kbhowtomaster kbhowto KB818410

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com