2009-11-21 76 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); 
}