首页 文章

在GDI中绘制一个用透明度覆盖不透明像素的形状

提问于
浏览
3

我有一个掩码位图(bmpMask),我正在绘制到目标位图(bmpDest) . 两个位图都有alpha通道,但已经充满了不透明的内容 .

我想要做的是在bmpMask上使用GDI'Draw ...'方法生成透明区域,以便当我在其上绘制bmpMask时bmpDest显示 .

当然 gMask.DrawLine(Pens.Transparent, 0, y, wMax, y) 导致bmpMask没有变化,因为GDI按设计工作并且不会更新像素 .

但我想要做的是发出一个draw方法,它将改变bmpMask的alpha通道,使其在绘制到bmpDest时是透明的 . 我知道我可以使用SetPixel或更快速的不安全或Marshall替代品来做到这一点,但这会导致更复杂的解决方案 . 谢谢 .

2 回答

  • 6

    3年后发现''答案'!

    我从来没有试过尝试Dan的解决方案,所以我不想放弃它,但这正是我想要的:

    ' (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's 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 是关键!我终于试图在我的程序中再次修复这个缺点,谷歌搜索出现了这个与我搜索的内容无关的项目(嘿,谷歌可以读懂你的想法) .

    Is Graphics.DrawImage too slow for bigger images?

    精彩!现在10块钱说,整个世界上没有人读过这个 . 755

  • 1

    据我所知,改变alpha通道的唯一方法是使用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,看看它如何改变固体/透明的颜色 . 但是,如果您想直接更改位图的Alpha通道,请按以下步骤操作:Soft edged images in GDI+.

相关问题