0

Basically, I have an image which could be large (couple of thousand pixels in both height and width), and can vary quite a bit in width's and height's.

What I need to do is display these images at approximately 500 pixels in height and width - but I want to keep the aspect ratio of these intact, so the values would just be fairly close. But I'm a bit confused on what calculations I could use on the original width's and heights to get them to the right figures.

Any ideas?

Chris
  • 7,415
  • 21
  • 98
  • 190
  • 2
    Have you looked here http://stackoverflow.com/questions/1940581/c-sharp-image-resizing-to-different-size-while-preserving-aspect-ratio – Ash Burlaczenko Nov 02 '11 at 18:06
  • display them on what platform? silverlight? wpf? winforms? asp.net? (etc.) your answer can depend on this information. – Muad'Dib Nov 02 '11 at 18:10

2 Answers2

0

Try this:

Size original = new Size(5000,4000);
double ratio = (double)original.Height/original.Width;
int resizePercent = 50;
int newWidth = Convert.ToInt32(original.Width*resizePercent/100);
int newHeight = Convert.ToInt32(newWidth * ratio);
Size resized = new Size(newWidth,newHeight);

Of course simply set original variable to your images size and determine your resize percentage. You could calculate the resize percentage if you had a target width like this:

int targetWidth = 500;
int resizePercent = Convert.ToInt32((double)original.Width/targetWidth);
Joshua Marble
  • 540
  • 3
  • 9
0

I ran into this problem a couple weeks ago. Check out this article https://web.archive.org/web/20200508200042/http://www.4guysfromrolla.com:80/articles/012203-1.2.aspx

You create a Generic Handler (.ashx) that finds the image, resizes it, and returns it

Here is what I ended up using (you can resize on the fly):

public void ProcessRequest (HttpContext context) {
    TimeSpan refresh = new TimeSpan(168, 0, 0);
    HttpContext.Current.Response.Cache.SetExpires(DateTime.Now.Add(refresh));
    HttpContext.Current.Response.Cache.SetMaxAge(refresh);
    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public);
    HttpContext.Current.Response.Cache.SetValidUntilExpires(true);

    string dir = HttpContext.Current.Request.QueryString["dir"];
    string img = HttpContext.Current.Request.QueryString["img"];
    bool force = HttpContext.Current.Request.QueryString["force"] == "yes";
    double w = 0;
    double h = 0;

    string path = null;
    Image orig = null;
    Image thumb = null;

    //make sure we have a directory and image filename
    if (string.IsNullOrEmpty(dir))
        return;

    //make sure that we allow access to that directory, could do some extra sneaky security stuff here if we had to... (only accept a DirectoryID so the user doesn't know the actual path)
    switch (dir)
    {
        case "user":
            dir = "assets/images/users";
            if (string.IsNullOrEmpty(img) || img == "undefined") img = "0.jpg";
            break;
            break;
        case "icon":
            dir = "assets/images/icons";
            break;
        default:
            dir = "assets/images";
            break;
    }

    //make sure that the image filename is just a filename
    if (img.IndexOf("/") > -1 | img.IndexOf("\\") > -1)
        return;

    //make sure the image exists
    path = HttpContext.Current.Server.MapPath("~/" + dir + "/" + img);
    if (System.IO.File.Exists(path))
    {
        orig = Image.FromFile(path);
    }
    else
    {
        return;
    }


    //if there is a max width or height
    if (double.TryParse(HttpContext.Current.Request.QueryString["w"], out w) | double.TryParse(HttpContext.Current.Request.QueryString["h"], out h))
    {
        //thumb is the resized image
        double s = 1;
        if (w > 0 & h > 0)
        {
            double ratio = h / w;
            if (orig.Height / (double)orig.Width > ratio)
            {
                if (orig.Height > h)
                {
                    if (force)
                        s = w / (double)orig.Width;
                    else
                        s = h / (double)orig.Height;
                }
            }
            else
            {
                if (orig.Width > w)
                {
                    if (force)
                        s = h / (double)orig.Height;
                    else
                        s = w / (double)orig.Width;
                }
            }
        }
        else if (w > 0)
        {
            if (orig.Width > w)
            {
                s = w / (double)orig.Width;
            }
        }
        else if (h > 0)
        {
            if (orig.Height > h)
            {
                s = h / (double)orig.Height;
            }
        }

        //double flip gets it to lose the embedded thumbnail
        //https://web.archive.org/web/20200508200042/http://www.4guysfromrolla.com:80/articles/012203-1.2.aspx
        orig.RotateFlip(RotateFlipType.Rotate180FlipNone);
        orig.RotateFlip(RotateFlipType.Rotate180FlipNone);

        thumb = orig.GetThumbnailImage(Convert.ToInt32(orig.Width * s), Convert.ToInt32(orig.Height * s), null, IntPtr.Zero);

        if (force && w > 0 & h > 0)
            thumb = cropImage(thumb, (int)w, (int)h);
    }
    else
    {
        //thumb is the image at it's original size
        thumb = orig;
    }

    HttpContext.Current.Response.ContentType = "image/jpeg";
    thumb.Save(HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}


private Image cropImage(Image img, int w, int h)
{
    if (w > img.Width && h > img.Height)
        return img;
    Rectangle cropArea = new Rectangle((img.Width - w) / 2, (img.Height - h) / 2, w, h);
    Bitmap bmpImage = new Bitmap(img);
    Bitmap bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat);
    return (Image)(bmpCrop);
}

public bool IsReusable {
    get {
        return false;
    }
}
Greg
  • 8,574
  • 21
  • 67
  • 109