Reputation: 1231
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.)
Upvotes: 4
Views: 15372
Reputation: 81610
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);
Upvotes: 8
Reputation: 805
Opacity can only work on top-level windows. You can't use opacity on a Panel.
If you only want a picture with opacity i think you can draw it by yourself, like the following example. image
is an instance of 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: This article maybe give you any further hints.
Upvotes: 1