我的webform中有一个图像控件。我设置了它的宽度100px和高度100px。但如果有人上载比的图像100 * 120,我希望它裁剪或调整和设置100 * 100。我试图设置最大宽度但不工作的,我试着用代码位法如何在asp.net中裁剪或调整图像大小以设置高宽比
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string filename = FileUpload1.FileName;
string directory = Server.MapPath("~/");
Bitmap originalBMP = new Bitmap(FileUpload1.FileContent);
float origWidth = originalBMP.Width;
float origHeight = originalBMP.Height;
float sngRatio = origWidth/origHeight;
float newWidth = 100;
float newHeight = newWidth/sngRatio;
Bitmap newBMP = new Bitmap(originalBMP,Convert.ToInt32(newWidth),Convert.ToInt32(newHeight));
Graphics oGraphics = Graphics.FromImage(newBMP);
oGraphics.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
oGraphics.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);
newBMP.Save(directory + filename);
originalBMP = null;
newBMP = null;
oGraphics = null;
Label1.Text = "File <b style='color: red;'>" + filename + "<</b> uploaded.";
Image1.ImageUrl = @"~/" + filename;
}
else
{
Label1.Text = "No file uploaded!";
}
}
它的工作,但它将调整大小的图像保存在目录中我想将原始图像保存在目录中并在图像控制中显示调整大小的图像。在CodePlex上
感谢回答JAQ我只是试图图像resizing.net感谢示例网址 –
样品URL /图像/bla.jpg?h=100&w=100将无法工作,你可以编辑请 –
你是什么意思,它不会工作?它出什么问题了? –