C# WriteableBitmap到位图帧的转换

C# WriteableBitmap到位图帧的转换,c#,C#,有一些无痛的转化吗? 我在谷歌上搜索了这个样本: using (MemoryStream ms = new MemoryStream()) {//WriteableBitmap wb defined before wb.SaveJpeg(ms, (int)image1.Width, (int)image1.Height, 0, 100); BitmapImage bmp = new BitmapImage(); bmp.SetSource(ms); } 但是wb没有S

有一些无痛的转化吗? 我在谷歌上搜索了这个样本:

using (MemoryStream ms = new MemoryStream())
{//WriteableBitmap wb defined before
    wb.SaveJpeg(ms, (int)image1.Width, (int)image1.Height, 0, 100);
    BitmapImage bmp = new BitmapImage();
    bmp.SetSource(ms);
} 
但是wb没有SaveJpeg方法!(使用System.Windows.Media.Imaging;使用System.Windows.Media;)
bmp没有SetSource=(

有一个函数

var bf = BitmapFrame.Create(wb);

您可以将
WriteableBitmap
直接放入
BitmapFrame.Create()
。然后,您可以使用
JpegBitmapEncoder
BitmapFrame
保存为JPEG格式:

MemoryStream ms = new MemoryStream();
JpegBitmapEncoder enc = new JpegBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(wb));
enc.Save(ms);
如果要在保存之前调整图像大小,我建议使用Visual Studio的NuGet软件包管理器中提供的

WriteableBitmap newWB = wb.Resize(256, 256, WriteableBitmapExtensions.Interpolation.Bilinear);