通过指定高度或宽度在ASP.NET C#中按比例调整图像大小

时间:2011-07-27 17:15:58

标签: c# asp.net image resize imageresizer

我需要一个允许我调整图像大小的代码,但具有以下功能:

1)上传后调整图片大小

2)通过指定高度或宽度按比例调整图像大小。

注意:

  • 应该在ASP.NET C#
  • 中完成

例如:函数应获得宽度或高度,并按比例调整图像的高度或宽度。假设图像是400(w)x100(h)。我想告诉函数将图像调整到特定高度,比方说80px。该功能应按比例调整图像大小,同时将图像高度设置为80px和宽度。另一种选择是告诉函数宽度,比方说200px,函数应该将图像大小调整为200px宽度并相应地设置高度。

3)将图像保存到特定位置(路径)。

4)功能可以使用上传的图像或指定图像路径。

5)我希望能够选择图像质量

6)仅需要JPEG

请有人帮我解决这个问题。感谢。

5 个答案:

答案 0 :(得分:5)

最后一天,我发现了imageresizer并且很棒。和良好的API。效果很好。 从Visual Studio 2010 Extension Manager下载:http://nuget.org/

在VS-2010中下载API的简单步骤:

1)。安装分机http://nuget.org/

enter image description here

3)。查找并安装ImageResizing
enter image description here

4)。然后代码:(我在这里使用裁剪。你可以使用任何)imageresizing.net上的文档

string uploadFolder = Server.MapPath(Request.ApplicationPath + "images/");
FileUpload1.SaveAs(uploadFolder + FileUpload1.FileName);


//The resizing settings can specify any of 30 commands.. See http://imageresizing.net for details.
ResizeSettings resizeCropSettings = new ResizeSettings("width=200&height=200&format=jpg&crop=auto");

//Generate a filename (GUIDs are safest).
string fileName = Path.Combine(uploadFolder, System.Guid.NewGuid().ToString());

//Let the image builder add the correct extension based on the output file type (which may differ).
fileName = ImageBuilder.Current.Build(uploadFolder + FileUpload1.FileName, fileName, resizeCropSettings, false, true);

尝试!!!它非常棒且易于使用。感谢。

答案 1 :(得分:4)

虽然您似乎应该能够复制并粘贴代码段来执行此操作,there are a ton of pitfalls you need to look out for if you're building your own image resizing system。最好使用经过验证,测试和支持的open-source library

要直接从HttpPostedFile调整文件大小,请调用

ImageBuilder.Current.Build(httpPostedFile, "img.jpg", new ResizeSettings("width=200&quality=90"));

要调整现有文件的大小,请调用

ImageBuilder.Current.Build("orig.jpg", "img.jpg", new ResizeSettings("width=200&quality=90"));

ImageResizing.Net库是免费的,并且是MIT许可的(不用担心许可问题)。

答案 2 :(得分:3)

取自Stackoverflow answer,我想出了:

public static Image Resize(this Image image, int maxWidth = 0, int maxHeight = 0)
    {
        if (maxWidth == 0)
            maxWidth = image.Width;
        if (maxHeight == 0)
            maxHeight = image.Height;

        var ratioX = (double)maxWidth / image.Width;
        var ratioY = (double)maxHeight / image.Height;
        var ratio = Math.Min(ratioX, ratioY);

        var newWidth = (int)(image.Width * ratio);
        var newHeight = (int)(image.Height * ratio);

        var newImage = new Bitmap(newWidth, newHeight);
        Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);
        return newImage;
    }

调整图像大小以指定其maxWidth:

var _image = Image.FromStream(Source);
var _thumbImage = _image.Resize(100);

调整图像大小,指定其maxHeight:

var _image = Image.FromStream(Source);
var _thumbImage = _image.Resize(maxHeight: 100);

答案 3 :(得分:0)

我用于图像大小调整的代码: http://sietch.net/ViewNewsItem.aspx?NewsItemID=105

答案 4 :(得分:0)

这就是我项目中的表现

在上传文件时点击按钮

System.Drawing.Bitmap bmpPostedImage = new System.Drawing.Bitmap(File1.PostedFile.InputStream);
     System.Drawing.Image objImage = ScaleImage(bmpPostedImage, 81);
     objImage.Save(SaveLocation,ImageFormat.Png);
     lblmsg.Text = "The file has been uploaded.";

public static System.Drawing.Image ScaleImage(System.Drawing.Image image, int maxHeight)
        {
            var ratio = (double)maxHeight / image.Height;

            var newWidth = (int)(image.Width * ratio);
            var newHeight = (int)(image.Height * ratio);

            var newImage = new Bitmap(newWidth, newHeight);
            using (var g = Graphics.FromImage(newImage))
            {
                g.DrawImage(image, 0, 0, newWidth, newHeight);
            }
            return newImage;
        }

更多详情 Click here

https://codepedia.info/how-to-resize-image-while-uploading-in-asp-net-using-c/