0

I'm trying to "draw" areas of transparency onto a bitmap -- like cutting holes in the image.

The following code does not draw a line of transparency because drawing a transparent line onto a bitmap of course blends instead of replaces. (Why the default is to do the more complicated of the two drawing operations, makes no sense.)

Bitmap myBitmap = new Bitmap(50, 50);
Graphics g = Graphics.FromImage(myBitmap);
g.FillRectangle(Brushes.Black, 0, 0, 50, 50);
g.FillEllipse(Brushes.Transparent, 25, 0, 25, 25); //Does nothing
g.DrawLine(Pens.Transparent, 0, 0, 50, 50); //Does nothing

How would I modify this so that a transparent circle and line replace what's in the bitmap instead of blending?

(Note that this is the trivial case of "drawing" complete transparency. The end I'm going toward is the ability to "draw" modifying the alpha channel only without creating my own pixel by pixel operation. Being able to do complete transparency will suffice though.)


Following answer in article suggested as a duplicate, I've also tried the following (which does not work)

        base.OnPaint(e);
        Bitmap myBitmap = new Bitmap(50, 50);
        e.Graphics.FillRectangle(Brushes.Black, 0, 0, 50, 50);
        Graphics g = Graphics.FromImage(myBitmap);
        g.FillEllipse(new SolidBrush(Color.FromArgb(150, 125, 125, 125)), 25, 0, 25, 25);
        g.DrawLine(new Pen(Color.FromArgb(150,25,25,25)), 0, 0, 50, 50);
        g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
        e.Graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
        e.Graphics.DrawImage(myBitmap, 0, 0);

also tested this with SourceCopy

AppFzx
  • 1,445
  • 2
  • 14
  • 23
  • possible duplicate of [Draw shape to alpha channel only](http://stackoverflow.com/questions/3222411/draw-shape-to-alpha-channel-only) – asawyer Feb 22 '13 at 21:51
  • It's 5'o'clock here. Will look at any answers and accept on Monday. Question isn't dead! – AppFzx Feb 22 '13 at 22:07

1 Answers1

2

This works totally fine for me

Bitmap bmp = new Bitmap(50, 50, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
using (Graphics g = Graphics.FromImage(bmp))
{
    g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
    g.FillRectangle(Brushes.Black, 0, 0, 50, 50);
    g.FillEllipse(Brushes.Transparent, 25, 0, 25, 25);
    g.DrawLine(Pens.Transparent, 0, 0, 50, 50);
    g.Flush();
}
bmp.Save("Test.bmp");
FlyingStreudel
  • 4,434
  • 4
  • 33
  • 55
  • This is what I'm playing with testing right now but it isn't working. – AppFzx Feb 22 '13 at 22:02
  • Does your bitmap allow transparency? You might want to set the format like in my edit. – FlyingStreudel Feb 22 '13 at 22:05
  • I'm actually using `Format32bppPargb` – AppFzx Feb 22 '13 at 22:06
  • It doesn't solve the actual problem (illustrated by what I added saying I'd tried from the linked suggested duplicate) but it does in fact solve the question exactly as I asked it. Well done! – AppFzx Feb 27 '13 at 13:55
  • @AppFzx This answer is showing the solution applied. `CompositingMode.SourceCopy` is your friend here and then like create a pen with `alpha = 0`: `new Pen(Color.FromArgb(0, 255, 255, 255), 3)` – Bitterblue Jul 26 '16 at 13:05