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.)
Asked
Active
Viewed 1.4k times
4
-
No no. I know how to do that. I am asking "If I use background Image". Please don't get confused. – Zgrkpnr Apr 16 '14 at 15:56
-
2winforms doesn't really support transparency. If you want to stick to winforms, don't try to do fancy UI stuff, only the regular, ugly, boring, battleship-gray stuff. Otherwise use WPF and you'll be able to create a much richer UI. – Federico Berasategui Apr 16 '14 at 15:57
-
I have been on WPF for a while. I moved to it. But this app was very big. It will be a very hardwokr to create it in WPF again. – Zgrkpnr Apr 16 '14 at 16:06
-
Its kind of unheard of to move "backwards" to winforms, its graphics framework is archaic in comparison – Sayse Apr 16 '14 at 16:07
-
@Zgrknr you could also use an `ElementHost` approach and move parts of your app to WPF, in a gradual fashion, as opposed to an entire rework, or if you have several `Form`s, you can rework them into WPF `Window`s one by one, gradually – Federico Berasategui Apr 16 '14 at 16:09
-
@HighCore Once I've tried to host my Zedgraph in WPF and I regret it. It is very complicated for me for now. I am still in the phase of understanding some intermediate issues in WPF. – Zgrkpnr Apr 16 '14 at 16:29
2 Answers
8
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);

LarsTech
- 80,625
- 14
- 153
- 225
1
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.
-
Yep, that's what I was doing. But to give 3D effect I am using a picture designed in Photoshop. Of course if I can't find a solution, I will draw on Panel. Still voting it up because it is creative. Thank you. – Zgrkpnr Apr 16 '14 at 16:01
-
-
"Opacity can only work on top-level windows. You can't use opacity on a Panel." LarsTech is changing opacity on the bitmap and drawing the image on a panel. – Mark Ronollo Jul 19 '23 at 13:25