WPF 創建空白圖片

  • 2020 年 2 月 19 日
  • 筆記

本文告訴大家如何在 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);

上面這個方法只有創建 2×2 的圖片,而創建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);

本作品採用 知識共享署名-非商業性使用-相同方式共享 4.0 國際許可協議 進行許可。歡迎轉載、使用、重新發布,但務必保留文章署名林德熙(包含鏈接: https://blog.lindexi.com ),不得用於商業目的,基於本文修改後的作品務必以相同的許可發布。如有任何疑問,請 與我聯繫