正如Chris所说,你应该使用WriteableBitmap
。
下面是一些材料开始:
/// <summary>
/// method that will create a source for your image object,
/// and fill it with a specified array of pixels
/// </summary>
/// <param name="pixels">your array of pixels</param>
/// <returns>your image object</returns>
public BitmapSource DrawImage(Int32[,] pixels)
{
int resX = pixels.GetUpperBound(0) + 1;
int resY = pixels.GetUpperBound(1) + 1;
WriteableBitmap writableImg = new WriteableBitmap(resX, resY, 96, 96, PixelFormats.Bgr32, null);
//lock the buffer
writableImg.Lock();
for (int i = 0; i < resX; i++)
{
for (int j = 0; j < resY; j++)
{
IntPtr backbuffer = writableImg.BackBuffer;
//the buffer is a monodimensionnal array...
backbuffer += j * writableImg.BackBufferStride;
backbuffer += i * 4;
System.Runtime.InteropServices.Marshal.WriteInt32(backbuffer, pixels[i,j]);
}
}
//specify the area to update
writableImg.AddDirtyRect(new Int32Rect(0, 0, resX, resY));
//release the buffer and show the image
writableImg.Unlock();
return writableImg;
}
您的阵列中的每个像素必须是一个Int32。 这里是一个函数,RGB颜色代码转换成的Int32
/// <summary>
/// Return the specified color code as a Int32
/// </summary>
public Int32 GetColor(byte r, byte g, byte b)
{
return Int32.Parse(Color.FromRgb(r, g, b).ToString().Trim('#'), System.Globalization.NumberStyles.HexNumber);
}
填写您的像素阵列你想要的方式。
将您的XAML添加为Image
并更新源代码。
<Image x:Name="Chart"/>
Chart.Source = DrawImage(pixels);
来源
2017-10-13 04:54:19
Bob
像素阵列被称为图像..!看看'WriteableBitmap'。 – Chris
是的,像素逐像素不会很快......尽管30秒看起来极端。 – zzxyz
嗨克里斯我已经用WriteableBitmap尝试过,但我找不到任何如何生成它或稍后在画布上显示它的好例子。所以我希望如果你能分享一些关于这方面的知识,也许给我写了一个适用于我的案例的示例代码。 – N1no