C# 在上传C之前调整图像大小并优化图像#

C# 在上传C之前调整图像大小并优化图像#,c#,image,model-view-controller,file-upload,image-resizing,C#,Image,Model View Controller,File Upload,Image Resizing,我正在使用C#,MVC5,我正在从我的web应用程序上载图像,但我意识到我有性能问题,因为我没有优化它们,我需要修复它们,这对保持质量很重要。 下面你可以看到报告的结果为什么很慢 我怎么做 我用下面的代码将文件保存到本地路径中 string imgpathvalue = ConfigurationManager.AppSettings["RestaurantPath"]; string path = System.IO.Path.Combine(Server.MapPa

我正在使用C#,MVC5,我正在从我的web应用程序上载图像,但我意识到我有性能问题,因为我没有优化它们,我需要修复它们,这对保持质量很重要。 下面你可以看到报告的结果为什么很慢

我怎么做

我用下面的代码将文件保存到本地路径中

string imgpathvalue = ConfigurationManager.AppSettings["RestaurantPath"];
 
string path = System.IO.Path.Combine(Server.MapPath(imgpathvalue));

if (!Directory.Exists(path))
 {
     Directory.CreateDirectory(path);
 }
 string pic = System.IO.Path.GetFileName(restaurantImg.FileName.Replace(" ", "_").Replace("%", "_"));
              path = System.IO.Path.Combine(Server.MapPath(imgpathvalue), pic);
                    // file is uploaded

 restaurantImg.SaveAs(path);
我已经尝试了下面的代码,但是我得到了错误“GDI+中发生了一般错误”


您缺少一些正确调整图像大小的代码。“追加”是一个函数,它可以根据给定的宽度和高度值正确调整图像的大小(在本例中,如果可能,图像的大小将调整为120*120)

函数调用:

ResizeImage(“要调整大小的图像的路径”,
“要保存的路径调整副本的大小”,120,120);
要实现这样的函数调用,我们需要编写函数。它从
源图像路径
获取图像并创建新位图

然后,它计算调整图像大小的因子,并根据宽度或高度是否较大进行相应调整

完成后,我们从
sourceImagePath
创建一个新位图并调整其大小。最后,我们还需要处理
源图像
目标图像
,还需要处理用于不同质量设置的图形元素g

调整大小功能:

private void ResizeImage(字符串sourceImagePath、字符串destImagePath、,
int-wishImageWidth,int-wishImageHeight)
{
位图sourceImage=新位图(sourceImagePath);
位图destImage=null;
图形g=空;
int destImageWidth=0;
int-destImageHeight=0;
//计算图像因子
double faktor=(double)sourceImage.Width/(double)sourceImage.Height;
如果(faktor>=1.0)//横向
{
DestinmageWidth=wishImageWidth;
DestinmageHeight=(int)(DestinmageWidth/faktor);
}
else//Port
{
DestinmageHeight=wishImageHeight;
DestinmageWidth=(int)(DestinmageHeight*faktor);
}
尝试
{
Destinmage=新位图(源图像、DestinmageWidth、DestinmageHeight);
g=图形。从图像(目标图像);
g、 插值模式=
System.Drawing.Drawing2D.InterpolationMode.HighQuality双线性;
g、 平滑模式=
System.Drawing.Drawing2D.SmoothingMode.High Quality;
g、 PixelOffsetMode=
System.Drawing.Drawing2D.PixelOffsetMode.High Quality;
g、 合成质量=
System.Drawing.Drawing2D.CompositingQuality.High Quality;
g、 DrawImage(sourceImage,0,0,DestinmageWidth,DestinmageHeight);
//确保该文件不存在。
if(File.Exists(destImagePath)){
//如果它确实删除了旧版本。
文件删除(DestinmagePath);
}
destImage.Save(destImagePath);
}
捕获(例外情况除外)
{
System.Diagnostics.Debug.WriteLine(“***错误提示:”+ex.Message)
}
最后
{
如果(g!=null){g.Dispose();g=null;}
如果(destImage!=null){destImage.Dispose();destImage=null;}
}
Dispose();
sourceImage=null;
}

您不应该在ASP.NET中使用
System.Drawing
。改用ImageMagick之类的库。ImageMagick是否调整图像大小并优化图像?你能分享一些基于我的代码的代码吗?什么宽度和高度是保持相同质量和不上浆的最佳方法?此代码不保证
位图
图形
对象将沿着所有代码路径进行处理。您应该使用
using()
块,这里不需要
catch
,并且使用
Console。WriteLine
是ASP.NET中的一个NOOP。我收到一个错误,在catch部分的GDI+中发生了一个一般错误,因为在同一路径上已经有相同的名称。我可以替换它还是应该先删除它?您可以在使用文件创建新图像之前删除该图像。如果该图像存在,请删除文件。Exists@marios我添加了代码,以确保即使在同一目录中已经存在同名文件,也可以保存该文件。
  System.Drawing.Bitmap bmpPostedImage = new System.Drawing.Bitmap(restaurantImg.InputStream);
                    System.Drawing.Image objImage = ResizeImages.ScaleImage(bmpPostedImage, 81);
                    using (var ms = new MemoryStream())
                    {
                        objImage.Save(ms, objImage.RawFormat);
                        //ResizeImages.getImage(ms.ToArray());

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