Click here to Skip to main content
15,884,739 members
Articles / Web Development / ASP.NET

How to make pictures with rounded corners in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.14/5 (6 votes)
14 Sep 2009CPOL 53.1K   582   31   17
How to make pictures with rounded corners in ASP.NET.

PicturesWithRoundedCornes

Introduction

In this article, we will learn about how to make pictures with rounded corners in ASP.NET. Here are the steps involved:

  1. Add to your project a webform (for example, PictureViewer.aspx).
  2. In the code-file "PictureViewer.aspx.cs", insert this code:
  3. C#
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Drawing;
    using System.Drawing.Imaging;
    
    public partial class PictureViewer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
            string Filename = Request.QueryString["Filename"];
            Int32 Radius = Convert.ToInt32(Request.QueryString["Radius"])  ;
            if (!String.IsNullOrEmpty(Filename) &&
                Radius > 0)
            {
                System.Drawing.Bitmap bmp = MakeRoundedCorners(Filename, Radius);
    
                Response.ContentType = "image/Png";
                bmp.Save(Response.OutputStream, ImageFormat.Png);
            }
           
        }
    
        private Bitmap MakeRoundedCorners(String Filename,Int32 Radius)
        {
            System.Drawing.Image im =
             System.Drawing.Image.FromFile(Server.MapPath(Filename));
    
            Bitmap Bmp = new Bitmap(im, im.Width, im.Height);
            Graphics G = Graphics.FromImage(Bmp);
            Brush brush = new System.Drawing.SolidBrush(Color.Red);
    
     
    
            for (int i = 0; i < 4; i++)
            {
                Point[] CornerUpLeft = new Point[3];
    
                CornerUpLeft[0].X = 0;
                CornerUpLeft[0].Y = 0;
    
                CornerUpLeft[1].X = Radius;
                CornerUpLeft[1].Y = 0;
    
                CornerUpLeft[2].X = 0;
                CornerUpLeft[2].Y = Radius;
    
                System.Drawing.Drawing2D.GraphicsPath pathCornerUpLeft =
                   new System.Drawing.Drawing2D.GraphicsPath();
    
                pathCornerUpLeft.AddArc(CornerUpLeft[0].X, CornerUpLeft[0].Y,
                    Radius, Radius, 180, 90);
                pathCornerUpLeft.AddLine(CornerUpLeft[0].X, CornerUpLeft[0].Y,
                    CornerUpLeft[1].X, CornerUpLeft[1].Y);
                pathCornerUpLeft.AddLine(CornerUpLeft[0].X, CornerUpLeft[0].Y,
                    CornerUpLeft[2].X, CornerUpLeft[2].Y);
    
                G.FillPath(brush, pathCornerUpLeft);
                pathCornerUpLeft.Dispose();
    
                Bmp.RotateFlip(RotateFlipType.Rotate90FlipNone);
            }
    
            brush.Dispose();
            G.Dispose();
    
            Color backColor = Bmp.GetPixel(0, 0);
    
            Bmp.MakeTransparent(backColor);
    
            return Bmp;
           
        }
    }
  4. In Default.aspx, insert:
  5. ASP.NET
    <body style="background-color:Black">
      <form id="form1" runat="server"  >
        <asp:Image runat="server" ID="MyPicture" 
            ImageUrl="~/PictureViewer.aspx?Filename=Wolf.jpg&Radius=50"/>
        <asp:Image runat="server" ID="Image1" 
            ImageUrl="~/PictureViewer.aspx?Filename=me.jpg&Radius=120"/>
      </form>
    </body>
  6. Don’t forget to add your own pictures in the project instead of mine: Wolf.jpg and me.jpg.

Good luck!

License

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


Written By
Ukraine Ukraine
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionErrorIn My Host Pin
Member 100224461-May-13 6:50
Member 100224461-May-13 6:50 
AnswerRe: ErrorIn My Host Pin
mshatami8-Nov-13 3:47
mshatami8-Nov-13 3:47 
Questionhow can we treat the address of the image path ? Pin
vijay_dahite17-Feb-10 1:12
vijay_dahite17-Feb-10 1:12 
GeneralTo Dmitri Nesteruk and to All who are worried about “GDI+ is slow” [modified] Pin
Zhupanov Valeriy19-Sep-09 23:42
Zhupanov Valeriy19-Sep-09 23:42 
22:56 18 Sep '09 Dmitri Nesteruk wrote:
"Not to be picky or anything, but using GDI+ server-side is a dubious decision. Ever since Direct2D (and DirectWrite) came out, I've started using them server-side, with huge performance improvements. Look here for an example."

Yes, For games “GDI+ is slow” You are right.
But what about more trivial and very simple tasks like that we are talking about?
No more words…just test it.
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Image[] im = new Image[100];
        Trace.Warn("Start");
        for (int i = 0; i < 100; i++)
        {
            im[i] = new Image();
            if (i % 2 == 0)
            {
   // assign (i + 20) to Radius on each step
 im[i].ImageUrl = "~/PictureViewer.aspx?Filename=Wolf1.png&Color=00ff00&Radius=" + (i + 20);
            }
            else
            {
                
im[i].ImageUrl = "~/PictureViewer.aspx?Filename=me1.png&Color=0000ff&Radius=" + (i + 20);
            }
            Page.Controls.Add(im[i]);
        }
        Trace.Warn("Finish");
    }
}




Results :


Message ---- From First(s)--- From Last(s)

Begin Load ---- 0.0001694 ------ 0.000017
Start ------------ 0.00018896 ----- 0.000020
Finish -------- 0.00033604 ---- 0.000147
End Load ------ 0.00036228 ----- 0.000023


The size of Wolf1.png = 69kb
The size of me1.png = 44kb

0.000147 second for 100 pictures on page!


Do you still want to use Direct2D for such trivial tasks?
GeneralRe: To Dmitri Nesteruk and to All who are worried about “GDI+ is slow” Pin
Dmitri Nеstеruk20-Sep-09 8:59
Dmitri Nеstеruk20-Sep-09 8:59 
GeneralWhat do you think about this? [modified] Pin
Zhupanov Valeriy20-Sep-09 12:00
Zhupanov Valeriy20-Sep-09 12:00 
GeneralRe: To Dmitri Nesteruk and to All who are worried about “GDI+ is slow” Pin
mambo_222-Sep-09 11:34
mambo_222-Sep-09 11:34 
GeneralGDI+ is slow Pin
Dmitri Nеstеruk18-Sep-09 19:56
Dmitri Nеstеruk18-Sep-09 19:56 
GeneralRe: GDI+ is slow Pin
Zhupanov Valeriy20-Sep-09 8:05
Zhupanov Valeriy20-Sep-09 8:05 
GeneralThank everybody for ideas!!! Pin
Zhupanov Valeriy16-Sep-09 4:52
Zhupanov Valeriy16-Sep-09 4:52 
GeneralMy vote of 2 Pin
mambo_215-Sep-09 9:34
mambo_215-Sep-09 9:34 
GeneralRe: My vote of 2 Pin
gstolarov16-Sep-09 4:32
gstolarov16-Sep-09 4:32 
GeneralNice Code Pin
Jagz W14-Sep-09 16:06
professionalJagz W14-Sep-09 16:06 
QuestionNice solution! What about performance overhead? Pin
DrABELL14-Sep-09 11:20
DrABELL14-Sep-09 11:20 
AnswerRe: Nice solution! What about performance overhead? [modified] Pin
Zhupanov Valeriy20-Sep-09 7:34
Zhupanov Valeriy20-Sep-09 7:34 
GeneralIf only that could be done in CSS Pin
nistrum40414-Sep-09 10:53
nistrum40414-Sep-09 10:53 
GeneralMore flexible variant with borders [modified] Pin
Zhupanov Valeriy12-Sep-09 12:44
Zhupanov Valeriy12-Sep-09 12:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.