Draw rounded corner rectangle with gdi+ on image

I would just like to know if anyone can give me some advice on how to take a jpeg image and make the corners rounded at runtime using gdi+ and aspnet.

following code will dynamically create 250X250 Px image and draw rectangle with rounded corner.

Rounded corner iamge

Run sample code

<%@ Page Language="C#"%>
<%@ Import Namespace="System.Drawing"%>
<script runat="server">
    void Page_Init(Object sender,EventArgs e)
    {
        System.Drawing.Bitmap bitmap =new System.Drawing.Bitmap(202, 202);
        
        Graphics g = Graphics.FromImage(bitmap);
        // put white background.
        g.Clear(Color.White);
        // set AntiAlias for smooth curves.
        g.SmoothingMode = 
                System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        //Fill rectangle and draw line to make it smooth.
        DrawRoundedRectangle(g,new Rectangle(1,1, 200,200), 30, Pens.Black);
        // done with drawing dispose graphics object.
        g.Dispose();
        
        // Stream Image to client.
        Response.Clear();
        Response.ContentType ="image/pjpeg";
        bitmap.Save(Response.OutputStream, 
                System.Drawing.Imaging.ImageFormat.Jpeg);
        Response.End();
        
    }
    
    public static void DrawRoundedRectangle(Graphics g,
        Rectangle r,int d,Pen p){
		
		System.Drawing.Drawing2D.GraphicsPath gp = 
				new System.Drawing.Drawing2D.GraphicsPath();
		
		gp.AddArc(r.X, r.Y, d, d, 180, 90);
		gp.AddArc(r.X + r.Width - d, r.Y, d, d, 270, 90);
		gp.AddArc(r.X + r.Width - d, r.Y + r.Height - d, d, d, 0, 90);
		gp.AddArc(r.X, r.Y + r.Height - d, d, d, 90, 90);
        gp.AddLine(r.X, r.Y + r.Height - d, r.X, r.Y + d/2);
            
        g.DrawPath(p, gp);
	}
    
</script>
Bookmark with:
Technorati   Digg   Delicious   StumbleUpon   Facebook
My name is Jigar Desai I share my ideas on this site and you can contact me by filling contact form.

Categories