WPF 建立空白圖片
阿新 • • 發佈:2020-09-12
本文告訴大家如何在 WPF 建立空白圖片,可以建立1畫素圖片
可以使用 BitmapSource 的 Create 方法建立空白圖片
// 限制不能建立小於2x2的圖片 const int width = 2; const int height = width; BitmapSource.Create(width, height, 96, 96, PixelFormats.Indexed1, new BitmapPalette(new List<Color> { Colors.Transparent }), new byte[width * height], 1);
上面這個方法只有建立 2x2 的圖片,而建立1畫素圖片可以使用下面方法
const int width = 1; const int height = width; const double dpi = 96; // R G B 三個畫素 const int colorLength = 3; var image = BitmapSource.Create(width, height, dpi, dpi, PixelFormats.Bgr24, null, new byte[colorLength], colorLength);
空白圖片儲存到檔案,使用png和jpg等幾個格式裡面,檔案的大小如下
.png byte count = 119
.jpg byte count = 631
.bmp byte count = 58
.gif byte count = 41
也就是說存放為 gif 對於這張圖片最省檔案體積
以下是 bmp 檔案的二進位制
0x42,0x4D,0x3A,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x36,0x00,0x00,0x00,0x28,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC4,0x0E,0x00,0x00,0xC4,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
本文程式碼放在 github 歡迎小夥伴訪問