开发者

How to paint an area of a bitmap with 'transparent white'?

开发者 https://www.devze.com 2022-12-30 23:20 出处:网络
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.

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 unchang开发者_如何学编程ed.

Do I need to use SetPixel individually for each pixel in the rectangle?


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.


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.


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 ;-)


This little hack should do the trick:

Graphics g = Graphics.FromImage(myBitmap);
g.SetClip(new RectangleF(10, 10, 20, 20));
g.Clear(Color.Transparent);
g.RestoreClip();
g.Dispose();


FillRectangle doesnt do this - given a transparent brush, the existing pixels are unchanged.

Makes sense, since you are drawing with 0% opacity. :)

A fast solution for a Control would be a color key. You can set the key color of the control to a specific color (i.e. magenta); all pixels that are of this color would then be rendered transparent. (If the control supports it, of course.)

That, or bitmap.MakeTransparent(color) for general purposes. The problem is, again, that the color specified would be transparent, so you'd have to choose a color that doesn't exist in the image. (There's another example here: https://web.archive.org/web/20131030025202/http://msdn.microsoft.com/en-us/library/ms172507(v=VS.80).aspx)

Edit: In the end, the LockBits() approach mentioned in the other comments could be what you seek. Once you understand how the width and stride values interact, it's easy to manipulate the image at the lowest possible level. Here's a general example: https://web.archive.org/web/20141229164101/http://bobpowell.net/lockingbits.aspx Just make sure the image supports transparency (see PixelFormat), then lock the image with an appropriate mode (i.e. PixelFormat.Format32bppArgb), loop over every pixel, read for bytes, write your new color, etc.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号