欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#?wpf?Bitmap轉(zhuǎn)換成WriteableBitmap的方法

 更新時(shí)間:2022年08月04日 15:28:23   作者:CodeOfCC  
本文主要介紹了C#?wpf?Bitmap轉(zhuǎn)換成WriteableBitmap的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

在wpf中我們有時(shí)候需要截屏或者獲取鼠標(biāo)標(biāo)時(shí)通常拿到的是Bitmap,如果要作顯示,則需要將Bitmap轉(zhuǎn)成wpf控件兼容的圖像對(duì)象,比如轉(zhuǎn)成BitmapSource在網(wǎng)上已經(jīng)可以查到實(shí)現(xiàn)方法,這里提供一種將Bitmap轉(zhuǎn)成WriteableBitmap的方法。

一、WriteableBitmap是什么?

WriteableBitmap是一個(gè)wpf對(duì)象,在命名空間System.Windows.Media.Imaging中,是BitmapSource的子類。如下圖所示:

二、如何實(shí)現(xiàn)

1.創(chuàng)建WriteableBitmap

創(chuàng)建一個(gè)與Bitmap大小相同,像素格式兼容的WriteableBitmap。
示例如下:

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

2.寫入數(shù)據(jù)

調(diào)用Bitmap的LockBits獲取其內(nèi)部的圖像數(shù)據(jù),通過WritePixels的方式寫入WriteableBitmap,這樣即完成了轉(zhuǎn)換。

示例如下:

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 轉(zhuǎn)換成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;
}
//創(chuàng)建尺寸和格式與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數(shù)據(jù)寫入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.直接轉(zhuǎn)換

//創(chuàng)建一個(gè)Bitmap對(duì)象
var bitmap = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
//繪制Bitmap
//略
//繪制Bitmap--end
//將Bitmap轉(zhuǎn)換為WriteableBitmap
var wb=BitmapToWriteableBitmap(bitmap);

2.復(fù)用寫入

//創(chuàng)建一個(gè)Bitmap對(duì)象
var bitmap = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
//創(chuàng)建格式兼容的WriteableBitmap
var wb = CreateCompatibleWriteableBitmap(bitmap);
System.Drawing.Imaging.PixelFormat format = bitmap .PixelFormat;
if (wb == null)
//格式不兼容則強(qiáng)制使用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數(shù)據(jù)寫入WriteableBitmap
    BitmapCopyToWriteableBitmap(bitmap, wb, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), 0, 0, format);
}

總結(jié)

以上就是今天要講的內(nèi)容,本質(zhì)上就是對(duì)圖像數(shù)據(jù)的直接拷貝,剛好Bitmap有獲取內(nèi)部數(shù)據(jù)的接口,而WriteableBitmap也剛好有寫入數(shù)據(jù)的接口。這種方法避免了新的句柄產(chǎn)生,不會(huì)出現(xiàn)內(nèi)存泄漏。而且直接的數(shù)據(jù)拷貝,不需要編解碼,性能是非常好的。

到此這篇關(guān)于C# wpf Bitmap轉(zhuǎn)換成WriteableBitmap的方法的文章就介紹到這了,更多相關(guān)C# Bitmap轉(zhuǎn)換成WriteableBitmap內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Unity3D實(shí)現(xiàn)導(dǎo)航效果

    Unity3D實(shí)現(xiàn)導(dǎo)航效果

    這篇文章主要為大家詳細(xì)介紹了Unity3D實(shí)現(xiàn)簡(jiǎn)單導(dǎo)航效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • unity 如何使用LineRenderer 動(dòng)態(tài)劃線

    unity 如何使用LineRenderer 動(dòng)態(tài)劃線

    這篇文章主要介紹了unity 使用LineRenderer 動(dòng)態(tài)劃線的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • C# 開發(fā)日志本地化工具

    C# 開發(fā)日志本地化工具

    這篇文章主要介紹了C# 開發(fā)日志本地化工具的步驟,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-02-02
  • C#實(shí)現(xiàn)gRPC服務(wù)和調(diào)用示例詳解

    C#實(shí)現(xiàn)gRPC服務(wù)和調(diào)用示例詳解

    gRPC?是一種與語(yǔ)言無(wú)關(guān)的高性能遠(yuǎn)程過程調(diào)用?(RPC)?框架,這篇文章主要為大家詳細(xì)介紹了C#如何實(shí)現(xiàn)gRPC服務(wù)和調(diào)用,需要的可以參考一下
    2024-01-01
  • C#入門之索引器使用實(shí)例

    C#入門之索引器使用實(shí)例

    這篇文章主要介紹了C#的索引器使用方法,對(duì)此,C#初學(xué)者應(yīng)予以牢固掌握,需要的朋友可以參考下
    2014-08-08
  • Unity實(shí)現(xiàn)手機(jī)搖一搖震動(dòng)

    Unity實(shí)現(xiàn)手機(jī)搖一搖震動(dòng)

    這篇文章主要為大家詳細(xì)介紹了untiy實(shí)現(xiàn)手機(jī)搖一搖震動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • C#利用OpenCvSharp實(shí)現(xiàn)玉米粒計(jì)數(shù)

    C#利用OpenCvSharp實(shí)現(xiàn)玉米粒計(jì)數(shù)

    這篇文章主要為大家詳細(xì)介紹了C#如何結(jié)合OpenCVSharp4實(shí)現(xiàn)玉米粒計(jì)數(shù),文中的示例代碼簡(jiǎn)潔易懂,具有一定的學(xué)習(xí)價(jià)值,需要的小伙伴可以參考下
    2023-11-11
  • unity實(shí)現(xiàn)無(wú)限列表功能

    unity實(shí)現(xiàn)無(wú)限列表功能

    這篇文章主要介紹了unity實(shí)現(xiàn)無(wú)限列表功能,水平方向,豎直方向滑動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • c#操作附加數(shù)據(jù)庫(kù)的方法

    c#操作附加數(shù)據(jù)庫(kù)的方法

    這篇文章主要介紹了c#操作附加數(shù)據(jù)庫(kù)的方法,涉及C#針對(duì)附加數(shù)據(jù)庫(kù)的相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-08-08
  • C#反射之基礎(chǔ)應(yīng)用實(shí)例總結(jié)

    C#反射之基礎(chǔ)應(yīng)用實(shí)例總結(jié)

    這篇文章主要介紹了C#反射之基礎(chǔ)應(yīng)用實(shí)例總結(jié),包括了反射的基本原理與用法實(shí)例,需要的朋友可以參考下
    2014-10-10

最新評(píng)論