2

我正在尝试做一个客户端-服务器程序,可以在其中共享剪贴板的内容。

当剪贴板包含图像时出现问题。我能够将 BitmapSource 编码为字节数组,发送它,然后解码并将其注入服务器剪贴板。但是,当我尝试将剪贴板粘贴到诸如 Paint 或 Gimp 之类的程序中时,由于无法识别数据而出现错误。

这是客户端代码:

if (Clipboard.ContainsImage())
{
    JpegBitmapEncoder encoder = new JpegBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(Clipboard.GetImage()));
    using (MemoryStream ms = new MemoryStream())
    {
        encoder.Save(ms);
        return ms.ToArray();
    }
}

这是服务器代码:

JpegBitmapDecoder jpegd = null;
using (MemoryStream ms = new MemoryStream(b)) // b is the byte array received from the client
{
      jpegd = new JpegBitmapDecoder(ms, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None);
}
BitmapSource bitmapSource = jpegd.Frames[0];

// Inject data into the clipboard
Clipboard.SetImage(bitmapSource);
4

0 回答 0