This project is read-only.

Issue with FillRectangle() and minor improvement for Clear()

Jan 27, 2012 at 9:33 PM

Hi. I have found your blog article few days ago while I was research for different way of display code drawing graphic on WPF or Siverlight control. Which got some idea from your project. 

As I working through my project, I have discover issue with FillRectangle. Method you have written for FillRectangle will take 4 position argument. During process, your code missed last pixel line which cause by loop's condition. As you set up offset2 = y2 * w + x1, that should be position for bottom left corner. But at while loop, your codition is "y < offset2". Which code didn't copy first line's memory to last line's position. 


For method Clear(). You have treated it like rectangle. My approach is to treat it like one dimensional array. Thus I don't have to use for loop to create first pixel line at all. Just fill up top left pixel which is at position ( 0, 0 ). Then use that pixel to expand to whole pixel bitmap. Here is the loop I'm come up with. 

int totalPixels = Pixelwidth * PixelHeight;

pixelIndex = 1;

blockPixels = 1;

While(PixelIndex < totalPixels)


CopyUnmanagedMemory(pixels, 0, pixelIndex * sizeOfColor, blockPixels * sizeOfColor);

pixelIndex += blockPixels;

blockPixels = Math.Min(2 * blockPixels, totalPixels - pixelIndex);


It's not huge performance improvement, but should be something for huge PixelWidth bitmap. 

Also, as your code partly support WPF, it should be good idea to make some code block to let method only process for PixelFormat.Pbgra32. as WPF version of writeableBitmap support many different PixelFormat, if people didn't read your code just simply use it on 8bit format or format other than Pbgra32, memory copy might cause problem. 

Apr 23, 2012 at 8:50 PM

Thanks for the input and sorry for the late reply.

This project is for WPF, Silverlight and Windows Phone and therefore unmanaged pointer handling isn't possible on all platforms. The small performance benefit doesn't really justify the overhead of the extra code path.

The issue with the FillRectangle will be fixed in the next version.

- Rene Schulte