BitmapSource to BitmapImage

后端 未结 2 1874
小蘑菇
小蘑菇 2020-12-14 18:25

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

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-14 18:50

    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;
    

提交回复
热议问题