如果可能的话,我如何在c#中将多个图像添加到剪贴板?我尝试添加图像数组,将图像转换为字节数组,然后转换为图像,以及其他几种方法。我还搜索了nuget一个包来管理剪贴板,但我找不到一个。
图像是一个列表,其中字节数组是png
这是我的代码:
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;
}