I'm working on some image transformations using ASP.NET/C#/GDI+. I have a method that looks like this to make a rectangular image round:
public static Image MakeRound(Image img)
{
Bitmap bmp = new Bitmap(img.Width, img.Height);
GraphicsPath gp = new GraphicsPath();
Graphics gr = Graphics.FromImage(bmp);
using (gp)
{
gp.AddEllipse(0, 0, img.Width, img.Height);
using (gr)
{
gr.SetClip(gp);
gr.DrawImage(img, Point.Empty);
}
}
return bmp;
}
It takes a square image and then an ellipse is added that is as big as the image. I then use SetClip to remove everything that lies outside the path and return a round image. This works as intended.
The returned image is then drawn onto another (larger) image at a certain position and the resulting composite image is saved as a file. A real world example of how to use this could be adding a logo or a watermark to an existing image. Here is some code from this operation:
// Get backdrop image from server
string backdropPath = Server.MapPath("/img/backdrop.jpg");
System.Drawing.Image backdrop = Bitmap.FromFile(backdropPath);
// Create a graphics object to work with
Graphics gra = Graphics.FromImage(backdrop);
gra.DrawImage(MakeRound(smallerImage), new Point(50,50));
// Save the new image
backdrop.Save(saveFilePath);
The only problem is that the edges of the superimposed/round/returned image are a bit rough. I'd like to make its edges smoother so it blends in better with the larger background image it's superimposed upon.
Are there any anti-aliasing parameters that I could use? Do you think that this smoothing should be done in the method above or is it applied when the round image is superimposed on the bigger backdrop?
All pointers and tips are most welcome!
The technique you're looking for is called Feathering, ie: feathered edges is a form of anti-aliasing applied at the edge of a drawn image.
Take a look at this post on soft edges in GDI+. It may be applicable to your scenario.