Reputation: 3558
What is fast approach to manipulate pixels in WritableBitmap (i also use WritableBitmapEx extensions)? SetPixel is very slow method for things like filling a background for my Paint-like application and also it does some weird things like memory corruption (don't know why).
Upvotes: 0
Views: 442
Reputation: 7082
SetPixel very slow - it's true.
You should use the LockBits method, and then iterate all pixels using unsafe code (pointers for pixels).
Example:
// lock the bitmap.
var data = image.LockBits(
new Rectangle(0, 0, image.Width, image.Height),
ImageLockMode.ReadWrite, image.PixelFormat);
try
{
unsafe
{
// get a pointer to the data.
byte* ptr = (byte*)data.Scan0;
// loop over all the data.
for (int i = 0; i < data.Height; i++)
{
for (int j = 0; j < data.Width; j++)
{
operate with pixels.
}
}
}
}
finally
{
// unlock the bits when done or when
// an exception has been thrown.
image.UnlockBits(data);
}
I suggest you read this article.
Upvotes: 1