Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are we able to set opacity of the background image of a panel?

Tags:

c#

.net

winforms

As you know it is possible in WPF. But I have a project in Windows Forms but I don't want to struggle to move project into WPF. So is it possible in Windows Forms? (Unlike asked in another question, I don't ask transparency of a panel. I am asking "If I would use a background image", can I make it half transparent.)

like image 821
Zgrkpnr Avatar asked Mar 20 '23 21:03

Zgrkpnr


1 Answers

You need to try two things: set the BackColor to Transparent and convert the image to something that has opacity.

From Change Opacity of Image in C#:

public Image SetImageOpacity(Image image, float opacity) {
  Bitmap bmp = new Bitmap(image.Width, image.Height);
  using (Graphics g = Graphics.FromImage(bmp)) {
    ColorMatrix matrix = new ColorMatrix();
    matrix.Matrix33 = opacity;
    ImageAttributes attributes = new ImageAttributes();
    attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default,
                                      ColorAdjustType.Bitmap);
    g.DrawImage(image, new Rectangle(0, 0, bmp.Width, bmp.Height),
                       0, 0, image.Width, image.Height,
                       GraphicsUnit.Pixel, attributes);
  }
  return bmp;
}

Then you panel properties would look like this:

panel1.BackColor = Color.Transparent;
panel1.BackgroundImage = SetImageOpacity(backImage, 0.25F);
like image 82
LarsTech Avatar answered Mar 22 '23 12:03

LarsTech