patryk.beza patryk.beza - 1 year ago 142
C# Question

Not clear unsafe code in draw pixel method

Lately I got low level graphics programming job to do (implementing raster graphics algorithms).
That's why I started to look for tools (classes) which would be helpful to draw primitives (lines, rectangles, ellipses etc.) on the low level of abstraction (even using


I consider if I should implement my algorithms in WPF, WindowsForms, WinAPI or other environment. For now I'm going to give a try to WPF. I found some examples how to draw pixels on bitmap but unfortunately I have problem with understanding this code (source code from MSDN):

// The DrawPixel method updates the WriteableBitmap by using
// unsafe code to write a pixel into the back buffer.
static void DrawPixel(MouseEventArgs e)
int column = (int)e.GetPosition(i).X;
int row = (int)e.GetPosition(i).Y;

// Reserve the back buffer for updates.

// Get a pointer to the back buffer.
int pBackBuffer = (int)writeableBitmap.BackBuffer;

// Find the address of the pixel to draw.
pBackBuffer += row * writeableBitmap.BackBufferStride;
pBackBuffer += column * 4;//??

// Compute the pixel's color.
int color_data = 255 << 16; // R
color_data |= 128 << 8; // G
color_data |= 255 << 0; // B

// Assign the color data to the pixel.
*((int*)pBackBuffer) = color_data;//??

// Specify the area of the bitmap that changed.
writeableBitmap.AddDirtyRect(new Int32Rect(column, row, 1, 1));

// Release the back buffer and make it available for display.

  1. What is this
    pBackBuffer += column * 4;
    line for? Why

  2. What does it
    *((int*)pBackBuffer) = color_data;
    mean? I know pointers from C/C++ but in C# there is
    and this line
    int pBackBuffer = (int)writeableBitmap.BackBuffer;
    suggests that we can treat equally
    which is also not clear for me.

  3. Which programming environment should I use?
    or other?

I would be very thankful if someone could explain me this unsafe code.

Answer Source

1: What is this pBackBuffer += column * 4; line for? Why 4?

Assuming that pixel is ARGB, it is 4 bytes per pixel. Since column is X coordinate, it must be multiplied by 4.

2: What does it ((int)pBackBuffer) = color_data; mean?

(int*)pBackBuffer - treat value of int pBackBuffer as pointer to an int


*((int*)pBackBuffer) = color_data - store color_data to that pointer

IntPtr and int are not quite the same. IntPtr is 32-bit when running on 32-bit OS and 64-bit when running on 64-bit OS. int is always 32-bit.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download