正如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