Click here to Skip to main content
Licence CPOL
First Posted 10 Sep 2009
Views 17,641
Downloads 212
Bookmarked 31 times

How to make pictures with rounded corners in ASP.NET

By | 14 Sep 2009 | Article
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. 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. <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)

About the Author

Zhupanov Valeriy



Ukraine Ukraine

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionhow can we treat the address of the image path ? Pinmembervijay_dahite1:12 17 Feb '10  
GeneralTo Dmitri Nesteruk and to All who are worried about “GDI+ is slow” [modified] PinmemberZhupanov Valeriy23:42 19 Sep '09  
GeneralRe: To Dmitri Nesteruk and to All who are worried about “GDI+ is slow” PinmemberDmitri Nesteruk8:59 20 Sep '09  
GeneralWhat do you think about this? [modified] PinmemberZhupanov Valeriy12:00 20 Sep '09  
GeneralRe: To Dmitri Nesteruk and to All who are worried about “GDI+ is slow” Pinmembermambo_211:34 22 Sep '09  
GeneralGDI+ is slow PinmemberDmitri Nesteruk19:56 18 Sep '09  
GeneralRe: GDI+ is slow PinmemberZhupanov Valeriy8:05 20 Sep '09  
GeneralThank everybody for ideas!!! PinmemberZhupanov Valeriy4:52 16 Sep '09  
GeneralMy vote of 2 Pinmembermambo_29:34 15 Sep '09  
GeneralRe: My vote of 2 Pinmembergstolarov4:32 16 Sep '09  
GeneralNice Code PinmemberJ walia16:06 14 Sep '09  
QuestionNice solution! What about performance overhead? PinmemberDrABELL11:20 14 Sep '09  
AnswerRe: Nice solution! What about performance overhead? [modified] PinmemberZhupanov Valeriy7:34 20 Sep '09  
GeneralIf only that could be done in CSS Pinmembernistrum40410:53 14 Sep '09  
GeneralMore flexible variant with borders [modified] PinmemberZhupanov Valeriy12:44 12 Sep '09  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 14 Sep 2009
Article Copyright 2009 by Zhupanov Valeriy
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid