Can we set the opacity of the background image of the panel?

As you know, this is possible in WPF. But I have a project in Windows Forms, but I do not want to try to move the project to WPF. So is this possible in Windows Forms? (Unlike the one asked in another question, I am not asking for the transparency of the panel. I am asking: β€œIf I used a background image,” can I make it half transparent.)

+4
source share
2 answers

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

From Change Image Opacity to 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 the panel properties will look like this:

panel1.BackColor = Color.Transparent;
panel1.BackgroundImage = SetImageOpacity(backImage, 0.25F);
+3

. .

, , , . image System.Drawing.Image.

using (Graphics g = Graphics.FromImage(image))
{
    Pen pen = new Pen(Color.FromArgb(alpha, 255, 255, 255), image.Width);
    g.DrawLine(pen, -1, -1, image.Width, image.Height);
    g.Save();
}

EDIT: .

+1

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


All Articles