I’d like to know how I create a vignetting effect on a picture using C# and .NET.
Does anyone have any ideas how to do this? Or are there any resources that will have the algorithm already done for me?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I believe this will do what you want:
What’s going on here? First I wrote code that would fill a rectangle with an elliptical gradient brush that went from white to black. Then I modified the code so that the filled area would also include the corners. I did this by increasing the rectangle size by the difference between the rectangle dimensions and sqrt(2)/2 * the rectangle dimensions.
Why sqrt(2)/2? Because the point (sqrt(2)/2, sqrt(2)/2) is the 45 degree angle point on a unit circle. Scaling by the width and height gives the distance needed to inflate the rect to make sure it’s fully covered.
Then I adjusted the Blend of the gradient to be much more white in the center.
Then I changed the color from white to pure transparent black and from black to pure opaque black. This has the effect of painting the far corners black and shade less on the way in to the center.
Finally, I wrote a utility method that runs on a Bitmap (I haven’t tested this part – I tested the code on a graphics from a Panel, but I think it will work here too.