C# wpf Bitmap轉換成WriteableBitmap的方法

前言

在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內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: