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>

You might also like...

Comments

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.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There are only two kinds of languages: the ones people complain about and the ones nobody uses” - Bjarne Stroustrup