C# wpf Bitmap转换成WriteableBitmap的方法

目录
  • 前言
  • 一、WriteableBitmap是什么?
  • 二、如何实现
    • 1.创建WriteableBitmap
    • 2.写入数据
  • 三、完整代码
  • 四、使用示例
    • 1.直接转换
    • 2.复用写入
  • 总结

前言

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

一、WriteableBitmap是什么?

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

二、如何实现

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

总结

以上就是今天要讲的内容,本质上就是对图像数据的直接拷贝,刚好Bitmap有获取内部数据的接口,而WriteableBitmap也刚好有写入数据的接口。这种方法避免了新的句柄产生,不会出现内存泄漏。而且直接的数据拷贝,不需要编解码,性能是非常好的。

到此这篇关于C# wpf Bitmap转换成WriteableBitmap的方法的文章就介绍到这了,更多相关C# Bitmap转换成WriteableBitmap内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • C#合并BitMap图像生成超大bitmap

    当只需要两个图像合并的时候,可以简单的使用gdi+,把两个图像画到一个画布上面实现合并bitmap. 当需要将许多bitmap合并时,由于bitmap类限制,长度或宽度太大时会报异常,前面这种方法就行不通了. 由于bitmapp属于位图格式,了解图像格式后,发现,bitmap文件的第3-8位存储了文件大小信息,第19-22位存储了高度信息,第23-26位存储了宽度信息.文件头后面都是像素的argb,并无其它信息.于是,试想一下,如果把第二张图像的像素argb放到第一张后面,并修改第一张的文件头信

  • C# Bitmap图像处理(含增强对比度的三种方法)

    目录 Bitmap类 BitmapData类 参考: Bitmap类 Bitmap对象封装了GDI+中的一个位图,此位图由图形图像及其属性的像素数据组成.因此Bitmap是用于处理由像素数据定义的图像的对象.该类的主要方法和属性如下: 1. GetPixel方法和SetPixel方法:获取和设置一个图像的指定像素的颜色. 2. PixelFormat属性:返回图像的像素格式. 3. Palette属性:获取和设置图像所使用的颜色调色板. 4. Height Width属性:返回图像的高度和宽度.

  • c# Bitmap转bitmapImage高效方法

    网上有很多人都记录以下方法进行转换,这个方法存在一个问题,就是低效,我在进行图片拼接时,图片大了之后就会很慢.所以我有找了一个高效的替代方法. public BitmapImage BitmapToBitmapImage(System.Drawing.Bitmap bitmap) { System.Drawing.Bitmap bitmapSource = new System.Drawing.Bitmap(bitmap.Width, bitmap.Height); int i, j; for

  • C# Bitmap图像处理加速的实现

    目录 BitmapData类 传统代码 使用BitmapData的代码 效率对比 代码 GPU加速 生成Dll 调用Dll 耗时 BitmapData类 BitmapData类专门用于位图处理,与Bitmap的不同点在于,它使用指针直接修改内存,而Bitmap是使用SetPixel()方法间接修改颜色,因此其效率远远超过SetPixel() 传统代码 以灰度处理为例,为了便于演示,此处的灰度算法采用 Gray=(R+G+B) / 3 private void Gray_Tradition() {

  • C# Bitmap 复制的小例子

    复制代码 代码如下: public Bitmap CopyBitmap(Bitmap source){    int depth = Bitmap.GetPixelFormatSize(source.PixelFormat); if (depth != 8 && depth != 24 && depth != 32)    {        return null;    } Bitmap destination = new Bitmap(source.Width, sou

  • c# 实现位图算法(BitMap)

    算法原理 BitMap的基本思想就是用一个bit位来标记某个元素对应的Value,而Key即是该元素.由于采用了Bit为单位来存储数据,因此可以大大节省存储空间. BitMap可以看成一种数据结构. 假设有这样一个需求:在20亿个随机整数中找出某个数m是否存在其中,并假设32位操作系统,4G内存. 在Java中,int占4字节,1字节=8位(1 byte = 8 bit). 如果每个数字用int存储,那就是20亿个int,因而占用的空间约为 (2000000000*4/1024/1024/102

  • C# wpf Bitmap转换成WriteableBitmap的方法

    目录 前言 一.WriteableBitmap是什么? 二.如何实现 1.创建WriteableBitmap 2.写入数据 三.完整代码 四.使用示例 1.直接转换 2.复用写入 总结 前言 在wpf中我们有时候需要截屏或者获取鼠标标时通常拿到的是Bitmap,如果要作显示,则需要将Bitmap转成wpf控件兼容的图像对象,比如转成BitmapSource在网上已经可以查到实现方法,这里提供一种将Bitmap转成WriteableBitmap的方法. 一.WriteableBitmap是什么?

  • C#实现位图转换成图标的方法

    本文实例讲述了C#实现位图转换成图标的方法.分享给大家供大家参考.具体实现方法如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication2 { public par

  • C#实现把图片转换成二进制以及把二进制转换成图片的方法示例

    本文实例讲述了C#实现把图片转换成二进制以及把二进制转换成图片的方法.分享给大家供大家参考,具体如下: private void button1_Click(object sender, EventArgs e) { string path = this.textBox1.Text; byte[] imgBytesIn = SaveImage(path); ShowImgByByte(imgBytesIn); //Parameters.Add("@Photo", SqlDbType.B

  • Oracle实现行转换成列的方法

    本文实例讲述了Oracle实现行转换成列的方法.分享给大家供大家参考,具体如下: 把行转成列 把学生表,成绩表,班级表,学科表 合并成一张成绩表效果如下: 创建表 --班级表 create table CLASS ( ID VARCHAR2(5) not null primary key, CLASSNAME VARCHAR2(10) ); --学生表 create table STUDENT ( ID VARCHAR2(10) not null primary key, NAME VARCHA

  • python实现将英文单词表示的数字转换成阿拉伯数字的方法

    本文实例讲述了python实现将英文单词表示的数字转换成阿拉伯数字的方法.分享给大家供大家参考.具体实现方法如下: import re _known = { 'zero': 0, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6, 'seven': 7, 'eight': 8, 'nine': 9, 'ten': 10, 'eleven': 11, 'twelve': 12, 'thirteen': 13, 'fourt

  • python将字符串转换成数组的方法

    python将字符串转换成数组的方法.分享给大家供大家参考.具体实现方法如下: #----------------------------------------- # Name: string_to_array.py # Author: Kevin Harris # Last Modified: 02/13/04 # Description: This Python script demonstrates # how to modify a string by # converting it

  • JS自定义函数实现时间戳转换成date的方法示例

    本文实例讲述了JS自定义函数实现时间戳转换成date的方法.分享给大家供大家参考,具体如下: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>获取当前年/月/日(www.jb51.net)</title> </head> <body> <script> function UnixToDate(unixTime

  • ASP.NET实现将word文档转换成pdf的方法

    本文实例讲述了ASP.NET实现将word文档转换成pdf的方法,分享给大家供大家参考.具体实现步骤如下: 一.添加引用 复制代码 代码如下: using Microsoft.Office.Interop.Word; 二.转换方法   1.方法 复制代码 代码如下: /// <summary>     /// 把Word文件转换成pdf文件     /// </summary>     /// <param name="sourcePath">需要转

  • asp.net实现将ppt文档转换成pdf的方法

    本文实例讲述了asp.net实现将ppt文档转换成pdf的方法.分享给大家供大家参考.具体实现方法如下: 一.添加引用 复制代码 代码如下: using Microsoft.Office.Core; using Microsoft.Office.Interop.PowerPoint; 二.转换方法 复制代码 代码如下: ///<summary>        /// 把PowerPoint文件转换成PDF格式文件       ///</summary>        ///<

  • php实现字符串首字母转换成大写的方法

    本文实例讲述了php实现字符串首字母转换成大写的方法.分享给大家供大家参考.具体分析如下: php中可以通过ucfirst函数将一个字符串中的第一个字母转换成大写,而ucwords函数可以将一个字符串中每个单词的首字母转换成大写 <?php $string = "php string functions are easy to use."; $sentence = ucfirst($string); $title = ucwords($string); print("$

随机推荐