2

I'm trying to do a client-server program in which it is possible to share the content of the clipboard.

I got a problem when the clipboard contains an Image. I'm able to encode the BitmapSource to a Byte Array, send it and then decode and inject it into the server clipboard. But when I try to paste the clipboard in a program like Paint or Gimp, I get an error since data is not recognized.

This is the client code:

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();
    }
}

This is the server code:

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);
TaW
  • 53,122
  • 8
  • 69
  • 111

0 Answers0