2009-11-21 2 views
1

간단한 그림 축소판 응용 프로그램을 만들려고합니다. 저는 웹을 검색하여 간단한 섬네일 앱과 상당히 복잡한 섬네일 앱을 발견했습니다. 내가 좋은 ImageButton 해상도를 얻을 수 있다면 좋은 일을했습니다. 현재 모든 것이 잘 작동하지만 버튼의 재사용은 끔찍합니다 (다양한 폭/높이 변형을 시도했습니다).C# ImageButton 그림 해상도

간단히 버튼 Image의 배열을 반복하고 속성을 설정합니다. 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 –

답변

2

비례 [크기 구조의 크기를 조정합니다이 기능에서 보라. 최대 높이/너비를 지정하면 해당 사각형 내에서 맞는 크기가 반환됩니다.

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