Richard Knop
Richard Knop

Reputation: 83697

How to set a pixel in a bitmap to white color?

This does not work:

Color col;
objBitmap = new Bitmap(Resource1.im);
col = Color.FromName("White");
// Perform an operation on the Color value here.
objBitmap.SetPixel(x, y, col);

When I load the image to a picture box control, it is still black (the pixel that should be white).

EDIT:

This is how I load the bitmap to a picture box control after editing it:

objBitmap.Save(Resource1.im.ToString());

this.pictureBox2.Image = ResizeBitmap(Resource1.im, 100, 100);

objBitmap.Dispose();

Upvotes: 1

Views: 1193

Answers (3)

SLaks
SLaks

Reputation: 887453

You're probably showing the wrong Bitmap in the PictureBox.

In the debugger's Watch window, right-click on objBitmap and on somePictureBox.Image and click Make Object ID.
If the two objects have different IDs, you're displaying the wrong image.

EDIT: Bitmap.Save takes a filename.
Your code creates a file named Bitmap in the current directory.
Change it to pictureBox2.Image = ResizeBitmap(objBitmap, 100, 100);

ResX files are readonly; you cannot modify the stored Bitmap at runtime.
Instead, you can put the modified image in a static Bitmap field. (And make sure not to dispose it until you don't need it anymore)


Note, by the way, that you can write

Color col = Color.White;

Upvotes: 2

Jason Kresowaty
Jason Kresowaty

Reputation: 16500

Not sure if it is a good idea to modify a Bitmap thatis some kind of resource. I'd Clone it to be safe. That being said, I could not reproduce a problem.

Bitmap objBitmap = (Bitmap)Resource1.im.Clone();

Upvotes: 2

winwaed
winwaed

Reputation: 7801

Have you checked what col is set to? In particular what the alpha component is set to? Ie. You might be adding transparent white?

Upvotes: 1

Related Questions