Draw a shape in GDI + that overlays opaque pixels with transparency

I have a mask bitmap (bmpMask) that I draw on the target bitmap (bmpDest). Both bitmaps have alpha channels, but are already filled with opaque content.

I want to make transparent areas using the GDI + 'Draw ...' methods on bmpMask so that bmpDest will show when I draw bmpMask on it.

Of course, it gMask.DrawLine(Pens.Transparent, 0, y, wMax, y)does not cause any changes to bmpMask, because GDI + works the way it was designed and does not make anything transparent. Even with translucent colors, only the rmp, g, b pixel values ​​of bmpMask are updated.

But what I want to do is to issue a draw method that will change the bmpMask alpha channel so that it is transparent when it was drawn on bmpDest. I know that I can do this with SetPixel or faster unsafe or alternative Marshall, but this will cause a much more complex solution. Thank.

+3
source share
2 answers

Found the "answer" after 3 years!

I have never come across an attempt to solve Dan, so I don’t want to take it away, but it does exactly what I want:

    ' (setting composting mode is the key to getting the transparent
    ' line to actually result in setting the pixels to transparent-
    ' instead of just not drawing anything 'because it transparent dummy' - 
    ' because it turns off the 'math' that is done to cause transparent
    ' image drawing to occur and just verbatim copies the bits).
    g1.CompositingMode = Drawing2D.CompositingMode.SourceCopy
    g1.DrawLine(Pens.Transparent, 0, y, wMax, y)
    g1.CompositingMode = Drawing2D.CompositingMode.SourceOver

CompostingMode- this is the key! Finally, I am trying to fix this flaw in my program, and a Google search has discovered this element that has nothing to do with what I was looking for (hey, Google can read your mind).

Graphics.DrawImage ?

! 10 , . 755

+6

, - - , , SetPixel (, ). , , ColorMatrix. DrawImage , R/G/B/A . , :

        using ( var bmp = new Bitmap( 100, 100 ) )
        using ( var g = Graphics.FromImage( bmp ) )
        using ( var ia = new ImageAttributes() )
        {
            float R = 1;
            float G = 0;
            float B = 0;
            ia.SetColorMatrix( new ColorMatrix( new float[][] {
                new float[] {1, 0, 0, R, 0},
                new float[] {0, 1, 0, G, 0},
                new float[] {0, 0, 1, B, 0},
                new float[] {0, 0, 0, 0, 0},
                new float[] {0, 0, 0, 0, 1}} ) );
            g.Clear( Color.White );
            g.FillEllipse( Brushes.Blue, 10, 10, 30, 30 );
            g.FillEllipse( Brushes.Red, 60, 10, 30, 30 );
            g.FillEllipse( Brushes.Green, 10, 60, 30, 30 );
            g.FillEllipse( Brushes.Black, 60, 60, 30, 30 );
            e.Graphics.DrawImage(
                bmp,
                new Rectangle( 0, 0, 100, 100 ),
                0, 0, 100, 100,
                GraphicsUnit.Pixel,
                ia );
        }

R/G/B 1 , , . , - , : GDI +.

+1

Source: https://habr.com/ru/post/1734328/


All Articles