views:

289

answers:

2

I'm trying to make a simple picture thumbnail application. I've searched the web and have found fairly complicated thumbnail apps, to simple ones. I have a good one working, If I could get the ImageButton Resolution looking good. Currently, everything works fine, but the resoultion of the buttons is horrible (i've tried various width/height variations).

I simply iterate through an array of button Image's and set their properties. I call ThumbnailSize() to set the width/height of the Imagebutton.

The code is kind of sloppy as of right now, but thats besides the point. I want to know if there is a way to keep or increase the ImageButton resolution while taking a picture (800x600+/-) and shrinking it into a Imagebutton.

string[] files = null;
files = Directory.GetFiles(Server.MapPath("Pictures"), "*.jpg");

    ImageButton[] arrIbs = new ImageButton[files.Length];

    for (int i = 0; i < files.Length; i++)
    {

        arrIbs[i] = new ImageButton();
        arrIbs[i].ID = "imgbtn" + Convert.ToString(i);
        arrIbs[i].ImageUrl = "~/Gallery/Pictures/pic" + i.ToString() + ".jpg";

        ThumbNailSize(ref arrIbs[i]);
        //arrIbs[i].BorderStyle = BorderStyle.Inset;

        arrIbs[i].AlternateText = System.IO.Path.GetFileName(Convert.ToString(files[i]));
        arrIbs[i].PostBackUrl = "default.aspx?img=" + "pic" + i.ToString();

        pnlThumbs.Controls.Add(arrIbs[i]);

    }


}
public ImageButton ThumbNailSize(ref ImageButton imgBtn)
{
    //Create Image with ImageButton path, and determine image size
    System.Drawing.Image img =
        System.Drawing.Image.FromFile(Server.MapPath(imgBtn.ImageUrl));

    if (img.Height > img.Width)
    {
        //Direction is Verticle
        imgBtn.Height = 140;
        imgBtn.Width = 90;

        return imgBtn;

    }
    else
    {
        //Direction is Horizontal
        imgBtn.Height = 110;
        imgBtn.Width = 130;
        return imgBtn;
    }


}
A: 

You need to scale the image according to its original size. Just setting the size will definately cause scaling issues.

Have a look at this link

C#: Resize An Image While Maintaining Aspect Ratio and Maximum Height

astander
+2  A: 

This function will proportionally resize a Size structure. Just provide it the maximum height/width and it will return a size that fits within that rectangle.

/// <summary>
/// Proportionally resizes a Size structure.
/// </summary>
/// <param name="sz"></param>
/// <param name="maxWidth"></param>
/// <param name="maxHeight"></param>
/// <returns></returns>
public static Size Resize(Size sz, int maxWidth, int maxHeight)
{
    int height = sz.Height;
    int width = sz.Width;

    double actualRatio = (double)width / (double)height;
    double maxRatio = (double)maxWidth / (double)maxHeight;
    double resizeRatio;

    if (actualRatio > maxRatio)
        // width is the determinate side.
        resizeRatio = (double)maxWidth / (double)width;
    else
        // height is the determinate side.
        resizeRatio = (double)maxHeight / (double)height;

    width = (int)(width * resizeRatio);
    height = (int)(height * resizeRatio);

    return new Size(width, height);
}
Brian