Library code snippets

Creating a dynamic graphic which returns a .jpg

This code allows an .aspx file to receive parameters (file name and width of picture) and return a .jpg so that in you HTML code you can use access an image like this: src="SmartPicture.aspx?f=Employee878.jpg&w=50" and the file Employee878.jpg will be displayed with a width of 50 and a calculated proportional height. I have a little error handling code in here so that if no file is sent then it returns a default picture which says "No Access" and if no width is given, then the default width of the picture is used. You could easily build in a content management feature so that only those with rights to view the picture could do so, no matter where it is called from. You could then put your actual .jpgs in a directory without web access so that you could lock down all access to them and even log how many times they are viewed and at what sizes no matter where they are access, even from plain HTML. Very nice.


<%@ Page Language="C#" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<script runat="server">

   void Page_Load(Object s, EventArgs e) {
       
       double photoWidth,photoHeight;
       double percentageDifference = 0;
       bool heHasAccess = false;
       System.Drawing.Image inputImage;
       
       //get information being sent
       heHasAccess = true;
       
       //get file name
       string pictureFileName = Request.QueryString["f"];
       if(pictureFileName == null || pictureFileName == "") {
           heHasAccess = false;
       }
       
       //get width
       try {
           if (Request.QueryString["w"] == null) {
               photoWidth = 0;
           } else {
               photoWidth = Int32.Parse(Request.QueryString["w"]);
           }
       }
       catch {
           photoWidth = 0;
       }
       
       //if anything went wrong, show error picture
       if(!heHasAccess) {
           inputImage = System.Drawing.Image.FromFile(Server.MapPath("images/pictureNoAccess.jpg"));
       } else {
           inputImage = System.Drawing.Image.FromFile(Server.MapPath("images/" + pictureFileName));
       }
       
       //if no width was given, assume the default now
       if(photoWidth==0) {
           if(!heHasAccess) {
               photoWidth = 100;
           } else {
               photoWidth = inputImage.Width;
           }
       }
       
       //define size for new image
       percentageDifference = inputImage.Width / photoWidth;
       photoHeight = inputImage.Height / percentageDifference;
       
       //output new image with different size
       Bitmap outputBitMap = new Bitmap(inputImage,Convert.ToInt32(photoWidth),Convert.ToInt32(photoHeight));
       Response.ContentType = "image/jpeg";
       outputBitMap.Save(Response.OutputStream, ImageFormat.Jpeg);
       
   }
</script>

Comments

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of Creating a dynamic graphic which returns a .jpg.

Leave a comment

Sign in or Join us (it's free).

Edward Tanguay Edward Tanguay updates his personal web site tanguay.info weekly with code, links, quotes and thoughts on web development. Sign up for the free newsletter.

Related podcasts

Events coming up

  • Mar 15

    DevWeek 2010

    London, United Kingdom

    DevWeek is Europe’s leading independent conference for software developers, database professionals and IT architects, and features expert speakers on a wide range of topics, including .NET 4.0, Silverlight 3, WCF 4, Visual Studio 2010, REST, Windows Workflow 4, Thread Synchronization, ASP.NET 4.0, SQL Server 2008 R2, LINQ, Unit Testing, CLR & C# 4.0, .NET Patterns, WPF 4, F#, Windows Azure, ADO.NET, Entity Framework, Debugging, T-SQL Tips & Tricks, and more.

Want to stay in touch with what's going on? Follow us on twitter!