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

发布于 2024-11-26 10:04:45 字数 451 浏览 3 评论 0原文

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

1)上传时调整图像大小

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

注意:

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

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

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

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

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

6) 仅需要 JPEG

有人可以帮我解决这个问题吗?谢谢。

I need a code that will allow me to resize images, but with the following functionality:

1) resize image upon upload

2) Resize image proportionally by specifying either height or width.

Note:

  • Should be done in ASP.NET C#

For example: The function should get the a width OR a height, and resize the image proportionally for the give height OR Width. Let's say that the image is 400(w)x100(h). I want to tell the function to resize the image to a specific height, let's say 80px. The function should resize the image proportionally while setting the image height to 80px and the width accordingly. Another option is ti tell the function the width, let's say 200px, and the function should resize the image to 200px width and set the height accordingly.

3) save the image to a specific location (path).

4) Function can work with uploaded image or by specifying an image path.

5) I want to be able to choose the image quality

6) Only need this for JPEG

Can somebody please help me out with this. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花桑 2024-12-03 10:04:45

尽管您似乎应该能够复制并粘贴代码片段来执行此操作,但有如果您正在构建自己的图像调整大小系统,则需要注意大量陷阱。最好使用经过验证、测试且受支持的开源库

要直接从 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 - 已获得许可(无需担心许可问题)。

Although it seems like you should be able to copy and paste a snippet to do this, there are a ton of pitfalls you need to look out for if you're building your own image resizing system. It's better to use a proven, tested, and supported open-source library.

To resize to a file directly from HttpPostedFile, call

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

To resize an existing file, call

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

The ImageResizing.Net library is free, and MIT-licensed (no worries about licensing problems).

書生途 2024-12-03 10:04:45

最后一天,我发现了 imageresizer ,它非常棒。和良好的API。效果很好。
从 Visual studio 2010 扩展管理器下载:http://nuget.org/

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

1)。安装扩展 http://nuget.org/

在此处输入图像描述

3)。查找并安装 ImageResizing
在此处输入图像描述

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);

尝试一下!它非常好用并且易于使用。谢谢。

Last day I found imageresizer and its great. and good API. Works Great.
Downloaded from Visual studio 2010 Extension Manager: http://nuget.org/.

Easy Steps to download API in VS-2010:

1). Install Extension http://nuget.org/.

enter image description here

3). Find and Install ImageResizing
enter image description here

4).Then Code: (I m using here cropping. you can use any) Documentation on 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);

Try!!! its very awsumm and easy to use. thanks.

棒棒糖 2024-12-03 10:04:45

摘自这个 Stackoverflow 答案,我想出了

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);

Taken from this Stackoverflow answer, I come up with:

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;
    }

To resize Image specifying its maxWidth:

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

To resize Image specifying its maxHeight:

var _image = Image.FromStream(Source);
var _thumbImage = _image.Resize(maxHeight: 100);
旧人九事 2024-12-03 10:04:45

的做法:

这就是我的项目在上传文件时单击按钮

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;
        }

This is how is did in my project

On Button click while uploading file:

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;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文