C#: Draw one Bitmap onto Another, with Transparency

前端 未结 3 558
天涯浪人
天涯浪人 2021-02-05 07:13

I have two Bitmaps, named largeBmp and smallBmp. I want to draw smallBmp onto largeBmp, then draw the result onto the screen. SmallBmp\'s white pixels should be transparent. Her

相关标签:
3条回答
  • 2021-02-05 07:31

    CompositingMode.SourceCopy is the problem here. You want CompositingMode.SourceOver to get alpha blending.

    0 讨论(0)
  • 2021-02-05 07:33

    Specify the transparency color of your small bitmap. 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);
    
    0 讨论(0)
  • 2021-02-05 07:36

    Winform copy image on top of another

        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;
                }
            }
        }
    
    0 讨论(0)
提交回复
热议问题