Microsoft KB Archive/818410

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

Article ID: 818410

Article Last Modified on 11/26/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article refers to the following Microsoft .NET Framework Class Library namespace: System.Windows.Forms.Clipboard

IN THIS TASK

 * SUMMARY
 * Copy a Graphic from the Clipboard
 * Verify the Result
 * REFERENCES



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.

back to the top

Copy a Graphic from the Clipboard
 Run Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. Click New on the File menu and then click Project. Rename the application to ClipBoardDemo . 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. From the Toolbox, add a Button control to Form1. Right-click Form1 and then click View Code.  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(&quot;c:\Test.bmp&quot;, System.Drawing.Imaging.ImageFormat.Bmp) 'To Save as Jpeg oImgObj.Save(&quot;c:\Test.jpeg&quot;, System.Drawing.Imaging.ImageFormat.Jpeg) 'To Save as Gif oImgObj.Save(&quot;c:\Test.gif&quot;, System.Drawing.Imaging.ImageFormat.Gif) End If       End If    End Sub </li> On the Build menu, click Build Solution.

ClipBoardDemo.Exe is created.</li></ol>

back to the top

Verify the Result

 * 1) Run the ClipBoardDemo.exe file application.

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

back to the top

<div class="references_section">