I've a little program to draw in pixels. It actually works fine, but every time it updates the image via my timer, I notice a flicker. I think that's due to always setting a new source, so my question is: if there is a way to draw directly on the image instead of setting a new source?
Here is my draw method:
private void SetPixel(int x, int y)
{
WriteableBitmap wrb = new WriteableBitmap((BitmapSource)image.Source);
byte[] color = new byte[] { b, g, r, a };
wrb.WritePixels(new Int32Rect(x, y, 1, 1), color, 4, 0);
image.Source = wrb;
}
You could try to cast the Source
property to a WriteableBitmap
instead of setting it to a new WriteableBitmap
each time:
private void SetPixel(int x, int y)
{
WriteableBitmap wrb = image.Source as WriteableBitmap;
if (wrb == null)
{
wrb = new WriteableBitmap((BitmapSource)image.Source);
image.Source = wrb;
}
byte[] color = new byte[] { b, g, r, a };
wrb.WritePixels(new Int32Rect(x, y, 1, 1), color, 4, 0);
}