1

How would I go about adding multiple images to the clipboard in c#...if it is even possible. I've tried adding an array of images, converting the images to byte arrays combining then converting to an image, and a couple of other methods. I've also searched nuget for a package to manage the clipboard but I could not find one.

Images is a List where the byte array is an png

Here's my code:

private void Copy_Click(object sender1, EventArgs eventArgs){

        //List<Image> test (failed)

        List<Image> images = new List<Image>();
        foreach (int v in lvDocumentImages.SelectedIndices)
            images.Add(ByteToImage(Images[v]));

        Clipboard.SetData(DataFormats.Bitmap, images);

       //Combined byte array test (failed)

       var bytes = new byte[] { };
       foreach(int i in lvDocumentImages.SelectedIndices)
           bytes = Combine(bytes,Images[i];
       Clipboard.SetData(DataFormats.Bitmap, ByteToImage(bytes));


      //Suggested article implementation (Failed)
       Clipboard.Clear();
       List<Image> images = new List<Image>();
       foreach (int v in lvDocumentImages.SelectedIndices)
           images.Add(ByteToImage(Images[v]));

       DataObject newObject = new DataObject(images);
       newObject.SetData(images);
       Clipboard.SetDataObject(newObject);
}

//Merges byte arrays, returns combined
private byte[] Combine(params byte[][] arrays){
        byte[] rv = new byte[arrays.Sum(a => a.Length)];
        int offset = 0;
        foreach (byte[] array in arrays)
        {
            System.Buffer.BlockCopy(array, 0, rv, offset, array.Length);
            offset += array.Length;
        }
        return rv;
    }

//Creates a bitmap from the byte array
private static Bitmap ByteToImage(byte[] blob){
        var mStream = new MemoryStream();
        var pData = blob;
        mStream.Write(pData, 0, Convert.ToInt32(pData.Length));
        var bm = new Bitmap(mStream, false);
        mStream.Dispose();
        return bm;
    }
Matthew Wherry
  • 343
  • 2
  • 11
  • 1
    Is there an application that should receive these images? If so, you need to figure out the format it expects. – Emond Feb 06 '18 at 16:53
  • check this out - https://stackoverflow.com/questions/9032673/clipboard-copying-objects-to-and-from – Ctznkane525 Feb 06 '18 at 16:58
  • Either the Desktop or an email. Using Clipboard.SetImage function works fine for email but only for 1 image, not multiple and...I have not tested desktop – Matthew Wherry Feb 06 '18 at 16:59
  • @Ctznkane525 I just tried an implementation of the suggested article, it did not work – Matthew Wherry Feb 06 '18 at 17:22
  • _"Using Clipboard.SetImage function works fine for email but only for 1 image"_ - as the [docs for SetImage say](https://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.setimage(v=vs.110).aspx), the method _"**clears the Clipboard** and then adds an Image in the Bitmap format."_ – stuartd Feb 06 '18 at 18:00
  • The clipboard can only contain one object of each type. As far as I know, the only way multiple objjects are possible is if it's a list of file references. – Nyerguds Feb 06 '18 at 19:58
  • Your `ByteToImage` function is incorrect, by the way. You should not dispose that stream. [Bimaps require their backing resource to be kept open](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.bitmap.-ctor?view=netframework-4.7.2#System_Drawing_Bitmap__ctor_System_IO_Stream_). If not, later manipulation or even saving of the image will cause crashes. – Nyerguds Dec 26 '18 at 09:29

2 Answers2

1

Have a look at this answer, its uses the ClipBoard Class to add multiple files to the clipboard, these could be images?

Copy files to clipboard in C#

Mark Redman
  • 24,079
  • 20
  • 92
  • 147
  • That works for the most part, I can write the byte arrays out to tmp files, and add their paths to the clipboards filedrop...but i dont want to keep the files locally, only in the clip. If they get deleted locally or overwritten, the clipboard loses it's reference – Matthew Wherry Feb 06 '18 at 17:45
0

i face the same requirements , which i do not want to save images to file and add files path to clipboard ,i try this solution and it worked fine:

 RichTextBox temp = new RichTextBox();
                foreach (byte[] data in ArrayOfImagesBytes)
                    {
                    Clipboard.Clear();
                    Bitmap b = new Bitmap(new MemoryStream(data));
                    Clipboard.SetImage(b);
                    temp.Paste();
                    }
                Clipboard.Clear();
                Clipboard.SetText(temp.Rtf, TextDataFormat.Rtf);
Fath Bakri
  • 161
  • 1
  • 12