Save ClipBoard As image file

Steve48

New member
Joined
Apr 11, 2009
Messages
4
Programming Experience
10+
Hi,
I am trying to save a bmp on the clipboard as a .bmp file. If there is no .bmp on the clipboard, thiscode executes with no error. If I have a bmp file saved on the clipboard, I get an error at the line shown in red.
error code
-2147467259 generic error in GDI+. Not sure what to do next. -Steve
VB.NET:
Private Sub Button1_Click_1(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
                [COLOR="Red"]oImgObj.Save("c:\Test.bmp", System.Drawing.Imaging.ImageFormat.Bmp)[/COLOR]                
'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
 
I think you are looking for something like this.

VB.NET:
        Dim img As Image = Clipboard.GetImage
        PictureBox1.Image = img
        img.Save("D:\tmp.bmp", System.Drawing.Imaging.ImageFormat.Bmp)

If there is no image on the clipboard, img would be nothing, so test for it.

Regards,
 
Back
Top