温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C# wpf Bitmap如何转换成WriteableBitmap

发布时间:2022-08-04 17:48:01 来源:亿速云 阅读:216 作者:iii 栏目:开发技术

本篇内容介绍了“C# wpf Bitmap如何转换成WriteableBitmap”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

    前言

    在wpf中我们有时候需要截屏或者获取鼠标标时通常拿到的是Bitmap,如果要作显示,则需要将Bitmap转成wpf控件兼容的图像对象,比如转成BitmapSource在网上已经可以查到实现方法,这里提供一种将Bitmap转成WriteableBitmap的方法。

    一、WriteableBitmap是什么?

    WriteableBitmap是一个wpf对象,在命名空间System.Windows.Media.Imaging中,是BitmapSource的子类。如下图所示:

    C# wpf Bitmap如何转换成WriteableBitmap

    二、如何实现

    1.创建WriteableBitmap

    创建一个与Bitmap大小相同,像素格式兼容的WriteableBitmap。
    示例如下:

    WriteableBitmap wb = new WriteableBitmap(bitmap.Width, bitmap.Height, 0, 0, System.Windows.Media.PixelFormats.Bgra32, null);

    2.写入数据

    调用Bitmap的LockBits获取其内部的图像数据,通过WritePixels的方式写入WriteableBitmap,这样即完成了转换。

    示例如下:

    var data = bitmap.LockBits(new System.Drawing.Rectangle(new System.Drawing.Point(0, 0), bitmap.Size), System.Drawing.Imaging.ImageLockMode.ReadOnly, src.PixelFormat);
    wb.WritePixels(new Int32Rect(srcRect.X, srcRect.Y, srcRect.Width, srcRect.Height), data.Scan0, data.Height * data.Stride, data.Stride, destinationX, destinationY);
    bitmap.UnlockBits(data);

    三、完整代码

    //将Bitmap 转换成WriteableBitmap 
    public static WriteableBitmap BitmapToWriteableBitmap(System.Drawing.Bitmap src)
    {
        var wb = CreateCompatibleWriteableBitmap(src);
        System.Drawing.Imaging.PixelFormat format = src.PixelFormat;
        if (wb == null)
        {
            wb = new WriteableBitmap(src.Width, src.Height, 0, 0, System.Windows.Media.PixelFormats.Bgra32, null);
            format = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
        }
        BitmapCopyToWriteableBitmap(src, wb, new System.Drawing.Rectangle(0, 0, src.Width, src.Height), 0, 0, format);
        return wb;
    }
    //创建尺寸和格式与Bitmap兼容的WriteableBitmap
    public static WriteableBitmap CreateCompatibleWriteableBitmap(System.Drawing.Bitmap src)
    {
        System.Windows.Media.PixelFormat format;            
        switch (src.PixelFormat)
        {
            case System.Drawing.Imaging.PixelFormat.Format16bppRgb555:
                format = System.Windows.Media.PixelFormats.Bgr555;
                break;
            case System.Drawing.Imaging.PixelFormat.Format16bppRgb565:
                format = System.Windows.Media.PixelFormats.Bgr565;
                break;
            case System.Drawing.Imaging.PixelFormat.Format24bppRgb:
                format = System.Windows.Media.PixelFormats.Bgr24;
                break;
            case System.Drawing.Imaging.PixelFormat.Format32bppRgb:
                format = System.Windows.Media.PixelFormats.Bgr32;
                break;           
            case System.Drawing.Imaging.PixelFormat.Format32bppPArgb:
                format = System.Windows.Media.PixelFormats.Pbgra32;
                break;            
            case System.Drawing.Imaging.PixelFormat.Format32bppArgb:
                format = System.Windows.Media.PixelFormats.Bgra32;
                break;
            default:
                return null;
        }
        return new WriteableBitmap(src.Width, src.Height, 0, 0, format, null);
    }
    //将Bitmap数据写入WriteableBitmap中
    public static void BitmapCopyToWriteableBitmap(System.Drawing.Bitmap src, WriteableBitmap dst, System.Drawing.Rectangle srcRect, int destinationX, int destinationY, System.Drawing.Imaging.PixelFormat srcPixelFormat)
    {
        var data = src.LockBits(new System.Drawing.Rectangle(new System.Drawing.Point(0, 0), src.Size), System.Drawing.Imaging.ImageLockMode.ReadOnly, srcPixelFormat);
        dst.WritePixels(new Int32Rect(srcRect.X, srcRect.Y, srcRect.Width, srcRect.Height), data.Scan0, data.Height * data.Stride, data.Stride, destinationX, destinationY);
        src.UnlockBits(data);
    }

    四、使用示例

    1.直接转换

    //创建一个Bitmap对象
    var bitmap = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
    //绘制Bitmap
    //略
    //绘制Bitmap--end
    //将Bitmap转换为WriteableBitmap
    var wb=BitmapToWriteableBitmap(bitmap);

    2.复用写入

    //创建一个Bitmap对象
    var bitmap = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
    //创建格式兼容的WriteableBitmap
    var wb = CreateCompatibleWriteableBitmap(bitmap);
    System.Drawing.Imaging.PixelFormat format = bitmap .PixelFormat;
    if (wb == null)
    //格式不兼容则强制使用bgr32
    {
        wb = new WriteableBitmap(bitmap .Width, bitmap .Height, 0, 0, System.Windows.Media.PixelFormats.Bgra32, null);
        format = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
    }
    while (true)
    {
        //绘制Bitmap
        //略
        //绘制Bitmap--end
        //将Bitmap数据写入WriteableBitmap
        BitmapCopyToWriteableBitmap(bitmap, wb, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), 0, 0, format);
    }

    “C# wpf Bitmap如何转换成WriteableBitmap”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

    向AI问一下细节

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    AI