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

This article refers to the following Microsoft .NET Framework Class Library namespace:



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.

back to the top

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.
back to the top

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.
back to the top


For more information about using the Clipboard class, visit the following Microsoft Web site:

back to the top

ID do Artigo: 818410 - Última Revisão: 26 de nov de 2007 - Revisão: 1