5

I am creating (then altering) a bitmap using "unsafe" code in a C# winforms project. This is done every 30ms or so. The problem I'm having is that "noise" or random pixels will show up sometimes in the resulting bitmap where I did not specifically change anything.

For example, I create a bitmap of 100x100. Using BitmapData and LockBits, I iterate through the bitmap and change certain pixels to a specific color. Then I UnlockBits and set a picturebox to use the image. All of the pixels I set are correct, but pixels that I did not specifically set are sometimes seemingly random colors.

If I set every pixel, the noise disappears. However, for performance reasons, I would prefer only to set the minimum number.

Can anyone explain why it does this?

Here is some example code:

// Create new output bitmap
Bitmap Output_Bitmap = new Bitmap(100, 100);

// Lock the output bitmap's bits
Rectangle Output_Rectangle = new Rectangle(
    0,
    0,
    Output_Bitmap.Width,
    Output_Bitmap.Height);
BitmapData Output_Data = Output_Bitmap.LockBits(
    Output_Rectangle,
    ImageLockMode.WriteOnly,
    PixelFormat.Format32bppRgb);

const int PixelSize = 4;
unsafe
{
    for (int y = 0; y < Output_Bitmap.Height; y++)
    {
        for (int x = 0; x < Output_Bitmap.Width/2; x++)
        {
            Byte* Output_Row = (Byte*)Output_Data.Scan0 + y * Output_Data.Stride;
            Output_Row[(x * PixelSize) + 2] = 255;
            Output_Row[(x * PixelSize) + 1] = 0;
            Output_Row[(x * PixelSize) + 0] = 0;
        }
    }
}

// Unlock the bits
Output_Bitmap.UnlockBits(Output_Data);

// Set picturebox to use bitmap
pbOutput.Image = Output_Bitmap;

In this example, I am only setting the left half of the image (Width/2 in the inner for loop). The right half will have random noise on an otherwise black background.

2 Answers 2

5

This is somewhat speculative, since I'm not privy to the implementation details of any of these classes, but I have a guess.

When you call new Bitmap(100, 100), the memory region that represents the bitmap's pixels is uninitialized, and therefore contains whatever random garbage was in those memory locations before it was allocated. The first time that you write to the bitmap you then set a only a subset of the locations, and the others show the random memory garbage.

If this is the case, then you must be sure to write to every pixel in the new Bitmap the first time you update it. Subsequent updates only need to update the changed pixels.

4
  • Just after creating Output_Bitmap, I have also tried a quick using (Graphics g = Graphics.FromImage(Output_Bitmap)) g.FillRectangle(Brushes.Black, 0, 0, Output_Bitmap.Width, Output_Bitmap.Height); but the same result occurs.
    – JYelton
    Apr 14, 2011 at 21:09
  • @JYelton, that might not work because you are later asking for a specific pixel format, which I could imagine would require a new allocation if it is different from whatever the default was? Apr 14, 2011 at 21:28
  • I'm running two loops now; one to set all pixels to black and another to set the specific ones. It seems to work well, although obviously the performance is taking a hit. @Doug I'll check out the zero memory method you linked, it might take the place of the first loop. Thanks.
    – JYelton
    Apr 15, 2011 at 6:51
  • @JYelton - another optimization that you might consider is to only call LockBits on the region that you intend to update (but update the entire region that you locked). I suspect that the problem actually occurs when the memory is marshalled to/from the device context, and you'll save a lot by locking the smallest possible region. Apr 15, 2011 at 12:03
3

You'll need to create a graphics object over the bitmap and place a Graphics.Clear() call after you create your bitmap to avoid undefined state of the bitmap memory.

You should also change from using Format32bppRgb to Format32PbppRgb because you're not setting the alpha byte. Either that or switch to the 24 bpp format.

4
  • There is no method .Clear() for a bitmap object.
    – JYelton
    Apr 14, 2011 at 21:21
  • True, however, if you create a graphics object over the bitmap object and call graphics.clear(color.black) you should be able to clear the bitmap. Apr 14, 2011 at 21:26
  • Also, do you mean Format32bppPArgb instead of Format32PbppRgb?
    – JYelton
    Apr 14, 2011 at 22:11
  • Using 24bpp is probably somewhat better since I don't need alpha. Thanks for the suggestion. However, the flickering/random pixels still occur.
    – JYelton
    Apr 15, 2011 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.