我正在尝试制作一个简单的图片缩略图应用程序。我搜索过网络,发现了相当复杂的缩略图应用程序,简单的应用程序。我有一个好的工作,如果我能让ImageButton Resolution看起来很好。目前,一切正常,但按钮的重新设置是可怕的(我尝试了各种宽度/高度变化)。
我只是遍历一个按钮Image的数组并设置它们的属性。我调用ThumbnailSize()来设置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 :(得分: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);
}
答案 1 :(得分:0)
您需要根据原始尺寸缩放图像。仅设置大小肯定会导致缩放问题。
看一下这个链接
C#: Resize An Image While Maintaining Aspect Ratio and Maximum Height