您好,登錄后才能下訂單哦!
本篇內容介紹了“C# wpf Bitmap如何轉換成WriteableBitmap”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
在wpf中我們有時候需要截屏或者獲取鼠標標時通常拿到的是Bitmap,如果要作顯示,則需要將Bitmap轉成wpf控件兼容的圖像對象,比如轉成BitmapSource在網上已經可以查到實現方法,這里提供一種將Bitmap轉成WriteableBitmap的方法。
WriteableBitmap是一個wpf對象,在命名空間System.Windows.Media.Imaging中,是BitmapSource的子類。如下圖所示:
創建一個與Bitmap大小相同,像素格式兼容的WriteableBitmap。
示例如下:
WriteableBitmap wb = new WriteableBitmap(bitmap.Width, bitmap.Height, 0, 0, System.Windows.Media.PixelFormats.Bgra32, null);
調用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); }
//創建一個Bitmap對象 var bitmap = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); //繪制Bitmap //略 //繪制Bitmap--end //將Bitmap轉換為WriteableBitmap var wb=BitmapToWriteableBitmap(bitmap);
//創建一個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”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。