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 ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系