Search code examples
c#imageclipboardcopy-paste

Allow user to copy image from picturebox and save it everywhere


In my application I have a pictureBox that shows an image. When user right clicks on the pictureBox and selects Copy from the context menu, I want to copy the image into the clipboard so the user can paste it in folders and anywhere else. How can I do that?

EDIT: I use this code but by this user only can paste image into word.

var img = Image.FromFile(pnlContent_Picture_PictureBox.ImageLocation);
Clipboard.SetImage(img);

Solution

  • Clipboard.SetImage copies the image content (binary data) to the clipboard not the file path. To paste a file in Windows Explorer you need to have file paths collection in the clipboard not their content.

    You can simply add the path of that image file to a StringCollection and then call the SetFileDropList method of Clipboard to achieve what you want.

    System.Collections.Specialized.StringCollection FileCollection = new System.Collections.Specialized.StringCollection();
    FileCollection.Add(pnlContent_Picture_PictureBox.ImageLocation);
    Clipboard.SetFileDropList(FileCollection);
    

    Now user can past the file anywhere e.g. Windows Explorer.

    More info on Clipboard.SetFileDropList Method http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.setfiledroplist.aspx