Reputation: 353
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;
}
Upvotes: 1
Views: 1490
Reputation: 191
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);
Upvotes: 0
Reputation: 24515
Have a look at this answer, its uses the ClipBoard Class to add multiple files to the clipboard, these could be images?
Upvotes: 1