locked
Image resizing RRS feed

  • Question

  • User-1208754703 posted

    Hi All,

    I am using the following code to display a resized image, all is working fine, I am wanting to only resize images who's width or height are over 500px.
    I have added in an IF statement to check the size of the image (this can be seen from lines 39 to 49) but it doesnt seem to do anything, I know I have made a coding error somwhere here but I cant work it out.

    If you need anymore information then please let me know,

    regards,

    Scott.

    Code Below:

    1    <%@ WebHandler Language="C#" Class="Thumbnail" %>
    2    
    3    using System;
    4    using System.Drawing;
    5    using System.Drawing.Drawing2D;
    6    using System.Drawing.Imaging;
    7    using System.IO;
    8    using System.Text.RegularExpressions;
    9    using System.Web;
    10   
    11   public class Thumbnail : IHttpHandler {
    12       private Regex _nameValidationExpression = new Regex(@"[^\w/]");
    13       private int _thumbnailSize = 500;
    14       
    15   
    16       public void ProcessRequest(HttpContext context) {
    17           string photoName = context.Request.QueryString["p"];
    18           string personID = context.Request.QueryString["id"];
    19           
    20           if (_nameValidationExpression.IsMatch(photoName)) {
    21               throw new HttpException(404, "Invalid photo name.");
    22           }
    23           string cachePath = Path.Combine(HttpRuntime.CodegenDir, photoName + ".png");
    24           if (File.Exists(cachePath)) {
    25               OutputCacheResponse(context, File.GetLastWriteTime(cachePath));
    26               context.Response.WriteFile(cachePath);
    27               return;
    28           }
    29           string photoPath = context.Server.MapPath("~/Demo/UserImages/" + personID + "/" + photoName + ".jpg");
    30           Bitmap photo;
    31           try {
    32               photo = new Bitmap(photoPath);
    33           }
    34           catch (ArgumentException) {
    35               throw new HttpException(404, "Photo not found.");
    36           }
    37           context.Response.ContentType = "image/png";
    38           int width, height;
    39           if (photo.Width > 500) (photo.Height > 500);
    40           {
    41               if (photo.Width > photo.Height) {
    42                   width = _thumbnailSize;
    43                   height = photo.Height * _thumbnailSize / photo.Width;
    44               }
    45               else {
    46                   width = photo.Width * _thumbnailSize / photo.Height;
    47                   height = _thumbnailSize;
    48               }
    49           }
    50           Bitmap target = new Bitmap(width, height);
    51           using (Graphics graphics = Graphics.FromImage(target)) {
    52               graphics.CompositingQuality = CompositingQuality.HighSpeed;
    53               graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
    54               graphics.CompositingMode = CompositingMode.SourceCopy;
    55               graphics.DrawImage(photo, 0, 0, width, height);
    56               using (MemoryStream memoryStream = new MemoryStream()) {
    57                   target.Save(memoryStream, ImageFormat.Png);
    58                   OutputCacheResponse(context, File.GetLastWriteTime(photoPath));
    59                   using (FileStream diskCacheStream = new FileStream(cachePath, FileMode.CreateNew)) {
    60                       memoryStream.WriteTo(diskCacheStream);
    61                   }
    62                   memoryStream.WriteTo(context.Response.OutputStream);
    63               }
    64           }
    65       }
    66   
    67       private static void OutputCacheResponse(HttpContext context, DateTime lastModified) {
    68           HttpCachePolicy cachePolicy = context.Response.Cache;
    69           cachePolicy.SetCacheability(HttpCacheability.Public);
    70           cachePolicy.VaryByParams["p"] = true;
    71           cachePolicy.SetOmitVaryStar(true);
    72           cachePolicy.SetExpires(DateTime.Now + TimeSpan.FromDays(365));
    73           cachePolicy.SetValidUntilExpires(true);
    74           cachePolicy.SetLastModified(lastModified);
    75       }
    76    
    77       public bool IsReusable {
    78           get {
    79               return true;
    80           }
    81       }
    82   }
    
     
    Wednesday, December 3, 2008 8:04 AM

Answers

  • User193237405 posted

     

    if (photo.Width > 500) (photo.Height > 500);
    40 {
    41 if (photo.Width > photo.Height) {
    42 width = _thumbnailSize;
    43 height = photo.Height * _thumbnailSize / photo.Width;
    44 }
    45 else {
    46 width = photo.Width * _thumbnailSize / photo.Height;
    47 height = _thumbnailSize;
    48 }
    49 }
    I'd think you'd want 
    if(photo.Width > 500 || photo.Height > 500) 
    {
    . . .
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 3, 2008 9:19 AM

All replies

  • User187056398 posted

    39           if (photo.Width > 500) (photo.Height > 500);
    40           {
    41               if (photo.Width > photo.Height) {

    What you have is:

       if (photo.Width > 500)
           (photo.Height > 500);

    What you probably want is:

       if (photo.Width > 500) || (photo.Height > 500)
       {
           if (photo.Width > photo.Height) 
           {
    

     

    It's better to have curly braces line up in the same column...errors are easier to see.

     

     

     

     
    Wednesday, December 3, 2008 9:17 AM
  • User193237405 posted

     

    if (photo.Width > 500) (photo.Height > 500);
    40 {
    41 if (photo.Width > photo.Height) {
    42 width = _thumbnailSize;
    43 height = photo.Height * _thumbnailSize / photo.Width;
    44 }
    45 else {
    46 width = photo.Width * _thumbnailSize / photo.Height;
    47 height = _thumbnailSize;
    48 }
    49 }
    I'd think you'd want 
    if(photo.Width > 500 || photo.Height > 500) 
    {
    . . .
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 3, 2008 9:19 AM
  • User-1208754703 posted

    Thanks Rampidbyter, that was spot on.

    I also added an ELSE at line 54 with the following

    else
            {
                width = photo.Width;
                height = photo.Height;
            }

    Works great, thanks both for the replies.

    Scott. 

    Wednesday, December 3, 2008 9:42 AM