Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
UPDATE :
Quote:
The WinForms implementation of Form only permits a solid colour to be used as the transparency key. It's not possible to have varying levels of transparency.



Hello,
I'm actually working with Windows Forms and GDI+.
I've got my background with a color that i MakeTransparent when I save my image as png later.
I have to draw some polygons on this background.
I also have to draw a closed curve filled with a linear gradient brush, going from white to transparent color.

The problem is, when I save the form as a png, the gradient brush is going from white to dark gray/black. It's like if i loose the transparency wanted when i fill my curve with my gradient brush.

I've got something like that :
https://image.noelshack.com/fichiers/2020/37/1/1599462293-transparent.png[^]

The red part is transparent (it's without the gradient brush).

Thanks for all the help that you can bring me to.

What I have tried:

This is my gradient brush :

C#
LinearGradientBrush linGrBrush2 = new LinearGradientBrush(closedCurveRect, Color.FromArgb(94, 94, 94), Color.Transparent, LinearGradientMode.Vertical);


And I try to fill my closed curve like that :
C#
g.FillClosedCurve(linGrBrush2, closedCurve.ToArray(), FillMode.Alternate, tension);


When I save my image, I convert my background to transparent :
C#
bmp1.MakeTransparent(Color.FromArgb(94, 94, 94));
bmp1.Save(filename + ".png", pngEncoder);
Posted
Updated 8-Sep-20 21:37pm
v6
Comments
Richard Deeming 4-Sep-20 9:38am    
The first argument to Color.FromArgb is the alpha value, so your brush is filling from "completely transparent grey" to "completely transparent". I don't think that's actually going to do anything useful.
Mineodo68 4-Sep-20 10:36am    
Hello,
Sorry, i forgot to change this value (that was for my test). It's without an alpha value.
[no name] 4-Sep-20 14:01pm    
You say you're using a gradient that goes to transparent, on a transparent background. At what point is transparent no longer transparent?
Mineodo68 7-Sep-20 3:00am    
Yeah, you got that. A part of my image (drawn without gradient brush) is transparent. But all the part with the gradient brush isn't transparent...
Mineodo68 7-Sep-20 3:06am    
I just uploaded a picture to illustrate.

1 solution

.net - C# Create Gradient Image - Stack Overflow[^]

This is a mod of the above (changed one of gradient colors and the file type). The point is you can save a transparent gradient; you just have to think more about how you apply your other artifacts.

Bitmap bitmap = new Bitmap( 100, 100 ); // 100x100 pixels
         Graphics graphics = Graphics.FromImage( bitmap );

         System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(
            new System.Drawing.Rectangle( 0, 0, 100, 100 ),
            System.Drawing.Color.Blue,
            System.Drawing.Color.Transparent,
            LinearGradientMode.Vertical );

         brush.SetSigmaBellShape( 0.5f );

         graphics.FillRectangle( brush, new System.Drawing.Rectangle( 0, 0, 100, 100 ) );
         bitmap.Save( "gradientImage.png", ImageFormat.Png );

         bitmap.Dispose();
         graphics.Dispose();
         brush.Dispose();
 
Share this answer
 
Comments
Mineodo68 9-Sep-20 3:15am    
So i tried to do what you just wrote (and thanks for your help).
The fact is that it works when it's on my windows form. But when I save it as a PNG, it simply doesn't works anymore...

Here is what i have : https://image.noelshack.com/fichiers/2020/37/3/1599636187-transparent2.png

As you can see, there's been a transparent line on the middle (represented on DarkGray), but all around it, there's some black/blue colors. I think that the problem comes when i save the image, maybe the gradient transparent brush is not supported... I really don't know
Mineodo68 9-Sep-20 3:33am    
Alright so i just found something :
"The WinForms implementation of Form only permits a solid colour to be used as the transparency key. It's not possible to have varying levels of transparency."

So maybe it's not possible on windows Forms. But thanks for you help Gerry !
[no name] 9-Sep-20 12:00pm    
I ran the same code from a Window Form and it runs fine (I figured it would). I think you missed the point where I said you do the gradient "first", and then apply ("paste") your artifacts ("over" the gradient in the areas you want changed). If you keep trying to apply the gradient over everything, it obviously doesn't work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900