BitmapSource to BitmapImage

2019-01-22 20:28发布

I need to parse the content of Clipboard.GetImage() (a BitmapSource) to a BitmapImage. Does anyone knows how can this be done?

2条回答
ゆ 、 Hurt°
2楼-- · 2019-01-22 21:15
using System.IO; // namespace for  using MemoryStream

private static byte[] ReadImageMemory()
{
    BitmapSource bitmapSource = BitmapConversion.ToBitmapSource(Clipboard.GetImage());
    JpegBitmapEncoder encoder = new JpegBitmapEncoder();
    MemoryStream memoryStream = new MemoryStream();
    encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
    encoder.Save(memoryStream);
    return memoryStream.GetBuffer();
}

// and calling by this example........
byte[] buffer = ReadImageMemory();
查看更多
看我几分像从前
3楼-- · 2019-01-22 21:24

I've found a clean solution that works:

BitmapSource bitmapSource = Clipboard.GetImage();

JpegBitmapEncoder encoder = new JpegBitmapEncoder();
MemoryStream memoryStream = new MemoryStream();
BitmapImage bImg = new BitmapImage();

encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
encoder.Save(memoryStream);

memoryStream.Position = 0;
bImg.BeginInit();
bImg.StreamSource = memoryStream;
bImg.EndInit();

memoryStream.Close();

return bImg;
查看更多
登录 后发表回答