Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

copy image to clipboard and let it be pasted as file (vb.net)

I have a picture box, and if I use snipet below:

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 the clipboard and if gets pasted to a folder then it becomes a file?

like image 323
Jonathan. Avatar asked Jan 22 '23 18:01

Jonathan.


2 Answers

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

Here the code in C#, porting it into VB.net won't be hard, I'm just too lazy to do that :) Anyway, you do have to save it somewhere 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 clipboard data as a copy operation

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 somewhere 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);

</pre>

and viola tries pasting in a folder the file image2.bmp will be pasted.

like image 125
Vivek Bernard Avatar answered Feb 08 '23 17:02

Vivek Bernard


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)
like image 31
Chris Haas Avatar answered Feb 08 '23 15:02

Chris Haas