开发者

BitmapSource to BitmapImage

开发者 https://www.devze.com 2023-02-17 22:17 出处:网络
I need to parse the content开发者_JAVA技巧 of Clipboard.GetImage() (a BitmapSource) to a BitmapImage.

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


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;


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

精彩评论

暂无评论...
验证码 换一张
取 消