gpt4 book ai didi

.net - System.Drawing.Bitmap 转 JPEG XR

转载 作者:行者123 更新时间:2023-12-04 05:23:21 37 4
gpt4 key购买 nike

我如何编码 System.Drawing.Bitmap (v4.0) 到 JPEG XR 流?

最佳答案

这可以通过 extension method 解决:

using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Media.Imaging;

/// <remarks>
/// Requires reference to <c>System.Drawing</c>, <c>PresentationCore</c> and <c>WindowsBase</c>.
/// </remarks>
public static class JpegXr {

public static MemoryStream SaveJpegXr(this Bitmap bitmap, float quality) {
var stream = new MemoryStream();
SaveJpegXr(bitmap, quality, stream);
stream.Seek(0, SeekOrigin.Begin);
return stream;
}

public static void SaveJpegXr(this Bitmap bitmap, float quality, Stream output) {
var bitmapSource = bitmap.ToWpfBitmap();
var bitmapFrame = BitmapFrame.Create(bitmapSource);
var jpegXrEncoder = new WmpBitmapEncoder();
jpegXrEncoder.Frames.Add(bitmapFrame);
jpegXrEncoder.ImageQualityLevel = quality / 100f;
jpegXrEncoder.Save(output);
}

/// <seealso cref="http://stackoverflow.com/questions/94456/load-a-wpf-bitmapimage-from-a-system-drawing-bitmap"/>
public static BitmapSource ToWpfBitmap(this Bitmap bitmap) {
using (var stream = new MemoryStream()) {
bitmap.Save(stream, ImageFormat.Bmp);
stream.Position = 0;
var result = new BitmapImage();
result.BeginInit();
// According to MSDN, "The default OnDemand cache option retains access to the stream until the image is needed."
// Force the bitmap to load right now so we can dispose the stream.
result.CacheOption = BitmapCacheOption.OnLoad;
result.StreamSource = stream;
result.EndInit();
result.Freeze();
return result;
}
}

}

关于.net - System.Drawing.Bitmap 转 JPEG XR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13469631/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com