我可以在WPF中绘制位图图片并在WPF中逐个像素地填充颜色吗? [英] Can I draw a bitmap picture in WPF and fill the color pixel by pixel in WPF?

查看:232
本文介绍了我可以在WPF中绘制位图图片并在WPF中逐个像素地填充颜色吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在wpf中绘制一个位图图像并按照我自己的颜色列表以像素方式填充整个图像..对于任何特定像素,所需的颜色......





有人可以提供完整的代码,因为我是wpf的新手...



我是什么尝试过:



这里是我试过的代码...



WriteableBitmap wbitmap = new WriteableBitmap(width,height,96,96,PixelFormats.Bgra32,myPalette);

byte [,,] pixels = new byte [height,width,4];



//清除黑色。

for(int row = 0; row< height; row ++)

{

for(int col = 0; col< width; col ++)

{

for(int i = 0; i< 3; i ++)

像素[row,col,i] = 0;

像素[row,col,3] = 255;

}

}



但我很困惑如何将自定义颜色设置为位图

i am trying to draw a bitmap image in wpf and fill the whole image by my own list of colors pixel wise..i.e, a required color to any particular pixel...


can someone help with complete code as i am new to wpf...

What I have tried:

here is the code which i have tried...

WriteableBitmap wbitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Bgra32, myPalette);
byte[, ,] pixels = new byte[height, width, 4];

// Clear to black.
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
for (int i = 0; i < 3; i++)
pixels[row, col, i] = 0;
pixels[row, col, 3] = 255;
}
}

but i m confused how to set the custom colors to the bitmap

推荐答案

您可以使用 WritePixels 方法之一(需要准备一个矩形)或获取指向像素缓冲区的指针(WriteableBitmap.BackBuffer Property(System.Windows.Media.Imaging) [ ^ ])并在缓冲区中设置数据。



对于第一个解决方案,请参阅WriteableBitmap.WritePixels方法(Int32Rect,Array,Int32,Int32)(System.Windows.Media.Imaging) [ ^ ]其中包含更新单个像素的示例代码。



对于第二个选项,此博客可能会有所帮助: Andy的博客:操纵WriteableBitmap的后台缓冲区 [ ^ ]
You can use one of the WritePixels methods (which requires preparing a rectangle) or get a pointer to the pixel buffer (WriteableBitmap.BackBuffer Property (System.Windows.Media.Imaging)[^]) and set the data in the buffer.

For the first solution see WriteableBitmap.WritePixels Method (Int32Rect, Array, Int32, Int32) (System.Windows.Media.Imaging)[^] which has example code on updating a single pixel.

For the second option this blog might be helpful: Andy's blog: Manipulating WriteableBitmap's back buffer[^]


这篇关于我可以在WPF中绘制位图图片并在WPF中逐个像素地填充颜色吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆