C#:以透明方式将一个位图绘制到另一个位图上

C#:以透明方式将一个位图绘制到另一个位图上,c#,graphics,drawing,C#,Graphics,Drawing,我有两个位图,名为largeBmp和smallBmp。我想把smallBmp画到largeBmp上,然后把结果画到屏幕上。SmallBmp的白色像素应该是透明的。以下是我正在使用的代码: public Bitmap Superimpose(Bitmap largeBmp, Bitmap smallBmp) { Graphics g = Graphics.FromImage(largeBmp); g.CompositingMode = CompositingMode.SourceC

我有两个位图,名为largeBmp和smallBmp。我想把smallBmp画到largeBmp上,然后把结果画到屏幕上。SmallBmp的白色像素应该是透明的。以下是我正在使用的代码:

public Bitmap Superimpose(Bitmap largeBmp, Bitmap smallBmp) {
    Graphics g = Graphics.FromImage(largeBmp);
    g.CompositingMode = CompositingMode.SourceCopy;
    smallBmp.MakeTransparent();
    int margin = 5;
    int x = largeBmp.Width - smallBmp.Width - margin;
    int y = largeBmp.Height - smallBmp.Height - margin;
    g.DrawImage(smallBmp, new Point(x, y));
    return largeBmp;
}

问题是,无论smallBmp是透明的,结果都是透明的!我只想看穿大的BMP,而不是它背后的内容。

指定小位图的透明度颜色。e、 g

Bitmap largeImage = new Bitmap();
Bitmap smallImage = new Bitmap();
--> smallImage.MakeTransparent(Color.White);
Graphics g = Graphics.FromImage(largeImage);
g.DrawImage(smallImage, new Point(10,10);

CompositingMode.SourceCopy
是这里的问题。您希望
合成模式.SourceOver
获得alpha混合。


不,它已经在将白色转换为透明。问题是两幅图像都被透明切割了。
    private void timerFFTp_Tick(object sender, EventArgs e)
    {
        if (drawBitmap)
        {
            Bitmap bitmap = new Bitmap(_fftControl.Width, _fftControl.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);   
            _fftControl.DrawToBitmap(bitmap, new Rectangle(0, 0, _fftControl.Width, _fftControl.Height));
            if (!fDraw)
            {
                bitmap.MakeTransparent();
                Bitmap fftFormBitmap = new Bitmap(_fftForm.BackgroundImage);
                Graphics g = Graphics.FromImage(fftFormBitmap);
                g.DrawImage(bitmap, 0, 0);
                _fftForm.BackgroundImage = fftFormBitmap;
            }
            else
            {
                fDraw = false;
                _fftForm.Width = bitmap.Width + 16;
                _fftForm.Height = bitmap.Height + 48;
                _fftForm.BackgroundImage = bitmap;
            }
        }
    }