views:

760

answers:

2

I have a picture box and if I use Clipboard.SetImage(PictureBox.image) Then I can only paste the image into things like Paint and MS word. I can't paste it as a file into a folder/desktop.

So how can I copy the image to to the clipboard and if gets pasted to a folder then it becomes a file?

+1  A: 

If you're using .net and your ultimate goal is to save the file, theres a LOT easier way,

Here the code in C sharp, porting it into VB.nt wont be hard, I'm just to lazy to do that :) Anyway you do have to save it some where before you can paste it so...

It loads the file to the Picture box and again saves it to a file, (lame, I know) and set the clipbaord data as a copy opertion

then when you paste (Ctrl+V) it, it gets pasted.

C#

___

Bitmap bmp;
string fileName=@"C:\image.bmp";
//here I assume you load it from a file , you might get the image from somewere else, your code may differ

pictureBox1.Image=(Image) Bitmap.FromFile(fileName);
bmp=(Bitmap)pictureBox1.Image;
bmp.Save(@"c:\image2.bmp");

 System.Collections.Specialized.StringCollection st = new System.Collections.Specialized.StringCollection();
            st.Add(@"c:\image2.bmp");
            System.Windows.Forms.Clipboard.SetFileDropList(st);

and viola try pasting in a folder the file image2.bmp wil be pasted.

Vivek Bernard
I know about saving them, however I'd like to copy the image as a file.
Jonathan
+1  A: 

Here's basically what @Vivek posted but ported to VB. Up-vote his if this works for you. What you have to understand is that explorer will only allow you to paste files, not objects (AFAIK anyway). The reason is because if you copy image data to the clipboard, what format should it paste in? PNG, BMP, JPG? What compression settings? So like @Vivek said, you need to think those over, create a file on your own somewhere on the system and use SetFileDropList which will add the temp file to the clipboard.

'   Add it as an image
    Clipboard.SetImage(PictureBox1.Image)

    'Create a JPG on disk and add the location to the clipboard
    Dim TempName As String = "TempName.jpg"
    Dim TempPath As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Temp, TempName)
    Using FS As New System.IO.FileStream(TempPath, IO.FileMode.Create, IO.FileAccess.Write, IO.FileShare.Read)
        PictureBox1.Image.Save(FS, System.Drawing.Imaging.ImageFormat.Jpeg)
    End Using
    Dim Paths As New System.Collections.Specialized.StringCollection()
    Paths.Add(TempPath)
    Clipboard.SetFileDropList(Paths)
Chris Haas
Thanks, and I never thought about the format etc :)
Jonathan