System.Drawing.Bitmap为JPEG XR [英] System.Drawing.Bitmap to JPEG XR

查看:247
本文介绍了System.Drawing.Bitmap为JPEG XR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能连接codeA System.Drawing.Bitmap (V4.0)的JPEG XR流?

How can I encode a System.Drawing.Bitmap (v4.0) to the JPEG XR stream?

推荐答案

这可以通过的扩展方法

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;
        }
    }

}

这篇关于System.Drawing.Bitmap为JPEG XR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆