我的网络表单中有一个图像控件。我将其宽度设置为 100 像素,高度设置为 100 像素。但是如果有人上传比例为 100 * 120 的图像。我希望它裁剪或调整大小并设置 100 * 100。我尝试设置最大宽度但没有成功,我尝试了使用代码的位图方法
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!";
}
}
它有效,但它将调整大小的图像保存在目录中我想将原始图像保存在目录中并在图像控件中显示调整大小的图像。