網站首頁 編程語言 正文
前言
在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也剛好有寫入數據的接口。這種方法避免了新的句柄產生,不會出現內存泄漏。而且直接的數據拷貝,不需要編解碼,性能是非常好的。
原文鏈接:https://blog.csdn.net/u013113678/article/details/121881806
相關推薦
- 2022-06-06 PowerShell yarn : 無法加載文件 C:\Users\Admin\AppData\Ro
- 2023-01-01 利用Python腳本實現傳遞參數的三種方式分享_python
- 2022-09-26 在?React?Native?中使用?CSS?Modules的配置方法_React
- 2022-04-05 Android判斷是否Root方法介紹_Android
- 2022-11-25 詳解React中Fragment的簡單使用_React
- 2022-04-27 Python中的元組(Tuple)操作實例詳解_python
- 2022-08-07 Python算法練習之二分查找算法的實現_python
- 2022-06-20 一文搞懂Go語言中條件語句的使用_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支