Vignetting the image effect algorithm using .NET.

I would like to know how to 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 an algorithm already executed for me?

+6
c # image-manipulation
source share
2 answers

I believe this will do what you want:

public void PaintVignette(Graphics g, Rectangle bounds) { Rectangle ellipsebounds = bounds; ellipsebounds.Offset(-ellipsebounds.X, -ellipsebounds.Y); int x = ellipsebounds.Width - (int)Math.Round(.70712 * ellipsebounds.Width); int y = ellipsebounds.Height - (int)Math.Round(.70712 * ellipsebounds.Height); ellipsebounds.Inflate(x, y); using (GraphicsPath path = new GraphicsPath()) { path.AddEllipse(ellipsebounds); using (PathGradientBrush brush = new PathGradientBrush(path)) { brush.WrapMode = WrapMode.Tile; brush.CenterColor = Color.FromArgb(0, 0, 0, 0); brush.SurroundColors = new Color[] { Color.FromArgb(255, 0, 0, 0) }; Blend blend = new Blend(); blend.Positions = new float[] { 0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0F }; blend.Factors = new float[] { 0.0f, 0.5f, 1f, 1f, 1.0f, 1.0f }; brush.Blend = blend; Region oldClip = g.Clip; g.Clip = new Region(bounds); g.FillRectangle(brush, ellipsebounds); g.Clip = oldClip; } } } public Bitmap Vignette(Bitmap b) { Bitmap final = new Bitmap(b); using (Graphics g = Graphics.FromImage(final)) { PaintVignette(g, new Rectangle(0, 0, final.Width, final.Height)); return final; } } 

What's going on here? First, I wrote code that would fill the rectangle with an elliptical gradient brush that went from white to black. Then I changed the code so that the filled area also included angles. I did this by increasing the size of the rectangle by the difference between the dimensions of the rectangle and the dimensions of the rectangle sqrt (2) / 2 *.

Why sqrt (2) / 2? Since the point (sqrt (2) / 2, sqrt (2) / 2) is a 45 degree angle point on a unit circle. Scaling in width and height gives the distance needed to inflate the rectangle to make sure it is completely covered.

Then I adjusted the gradient blend to be much whiter in the center.

Then I changed the color from white to pure transparent black and from black to pure opaque black. This results in painting the extreme corners of black and the shadow lower along the path to the center.

Finally, I wrote a utility method that works on Bitmap (I did not test this part - I checked the code on the chart from the panel, but I think it will work here too.

+10
source share

If your image is in a file, and if it is fast enough for your requirements, you can use the convert ImageMagick command-line tool, it has the -vignette option. To call this from a C # program, you can run it through System.Diagnostics.Process.Start or use this .NET wrapper for ImageMagick.

+2
source share

All Articles