2009-11-21 92 views
1

我正在尝试制作一个简单的图片缩略图应用程序。我搜索了网络,发现了相当复杂的缩略图应用程序,简单的应用程序。我有一个很好的工作,如果我能得到ImageButton决议看起来不错。目前,一切工作正常,但按钮的resoultion是可怕的(我试过各种宽度/高度变化)。C#ImageButton图片分辨率

我只是遍历按钮图像的数组并设置它们的属性。我调用ThumbnailSize()来设置Imagebutton的宽度/高度。

现在的代码有点草率,但除此之外就是这样。我想知道是否有一种方法可以在拍摄图片时保持或增加ImageButton分辨率(800x600 +/-)并将其缩小为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; 
    } 


} 
+0

http://stackoverflow.com/questions/249587/high-quality-image-scaling-c – 2009-11-21 08:04:12

回答

2

此功能将按比例调整一个Size结构看一看。只要提供它的最大高度/宽度,它将返回适合该矩形的尺寸。

/// <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); 
}