I want to replace all pixels in a rectangular region of a Bitmap with 'transparent white' - i.e., a=0, b=255, r=255, g=255.
FillRectangle doesnt do this - given a transparent brush, the existing pixels are unchanged.
Do I need to use SetPixel individually for each pixel in the rectangle?
I believe that you need to use SetPixel (or equivalent method of setting the color values directly) to get the pixels to be "transparent white".
You can use the Graphics.Clear
method to set the color or pixels, but you can't use it to set them to both transparent and a color. I tried this to set the pixels in a part of a bitmap:
using (Graphics g = Graphics.FromImage(theBitmap)) {
g.Clip = new Region(new Rectangle(10, 10, 80, 80));
g.Clear(Color.FromArgb(0, Color.White));
}
The pixels in the region end up as "transparent black": 0,0,0,0. Even drawing a solid white rectangle before clearing doesn't help. When the alpha is zero in a color, the other color components are also zero.
Using an almost transparent alpha like 1 works fine, the pixels end up as "almost transparent white": 1,255,255,255.
You'll have to set the Graphics.CompositingMode
property. For example:
protected override void OnPaint(PaintEventArgs e) {
var img = Properties.Resources.Chrysanthemum;
e.Graphics.DrawImage(img, 0, 0);
e.Graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
using (var br = new SolidBrush(Color.FromArgb(0, 255, 255, 255))) {
e.Graphics.FillRectangle(br, new Rectangle(50, 50, 100, 100));
}
}
The actual color you use doesn't matter, you'll get a black rectangle with an alpha of 0.
If you use the composite painting methods, then the alpha will be used to blend the colour, so nothing will happen.
If you want to set the bitmap, either create it from data with the background you want, or set the background using LockBits to manipulate the data en-masse.
You also might be able to do in using a bitblt method with the appropriate flags, but I don't know how to translate that to managed code.
Important tip:
Make sure when you create the image you do this
mainImage = new Bitmap(totalWidth, maxHeight, PixelFormat.Format32bppARgb);
and not
mainImage = new Bitmap(totalWidth, maxHeight, PixelFormat.Format32bppRgb);
It'll save you some trouble. Don't assume 32 bit means alpha channel ;-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With