在C#中将多个图像添加到剪贴板

时间:2018-02-06 16:40:38

标签: c# image winforms clipboard

如果可能的话,我如何在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;
    }

1 个答案:

答案 0 :(得分:1)

看看这个答案,它使用ClipBoard Class将多个文件添加到剪贴板,这些可能是图像吗?

Copy files to clipboard in C#