2013-11-01 50 views
1

我的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 + "&lt;</b> uploaded."; 
     Image1.ImageUrl = @"~/" + filename; 
    } 
    else 
    { 
     Label1.Text = "No file uploaded!"; 
    } 
} 

它的工作,但它将调整大小的图像保存在目录中我想将原始图像保存在目录中并在图像控制中显示调整大小的图像。在CodePlex上

回答

1

退房Web图像调整尺寸处理器:http://webimageresizer.codeplex.com/

这是一个自定义处理程序来处理图像。

样本网址 从CodePlex项目的主页

//返回图像映射到/bla.jpg调整为100个像素相对保方面向高度 /ImageHandler.ashx宽度多久? SRC =/bla.jpg &宽度= 100

//返回图像映射到/bla.jpg调整为100个像素保留相对方面高度与宽度 /ImageHandler.ashx?src=/bla.jpg &唤起注意T = 100

//返回图像映射到/bla.jpg调整为宽度100个像素和50个像素 /ImageHandler.ashx?src=/bla.jpg &宽度的高度= 100 &高度= 50

另一种选择是使用http://imageresizing.net/

的好处是,它会注册一个处理程序来处理透明图像,这意味着,您只添加查询字符串变量原始图片的URL来处理图像。

示例网址

/images/bla.jpg?h=100 & W = 100个

+0

感谢回答JAQ我只是试图图像resizing.net感谢示例网址 –

+0

样品URL /图像/bla.jpg?h=100&w=100将无法工作,你可以编辑请 –

+0

你是什么意思,它不会工作?它出什么问题了? –

相关问题