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

问题描述

我正在使用C#,MVC5,并且正在从Web应用程序上载图像,但是我意识到我存在性能问题,因为我没有对其进行优化,因此需要对其进行修复,这对于保持质量至关重要。 您可以在下面查看报告结果为何缓慢的原因。

enter image description here

我该怎么办?

我使用以下代码文件保存到本地路径中。

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 +中发生一般错误”。

  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,newWidth,newHeight);
            }
            return newImage;
        }

解决方法

您缺少一些代码来正确调整图像大小。追加功能可以根据您指定的宽度和高度值正确调整图像的大小(在本示例中,图像的大小可能会调整为120 * 120)。

函数调用:

ResizeImage("Path to the Image you want to resize","Path you want to save resizes copy into",120,120);

要进行这样的函数调用,我们需要编写函数。它将从sourceImagePath获取图像并创建一个新的位图。

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

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

调整大小功能:

private void ResizeImage(string sourceImagePath,string destImagePath,int wishImageWidth,int wishImageHeight)
{
    Bitmap sourceImage = new Bitmap(sourceImagePath);
    Bitmap destImage = null;
    Graphics g = null;
    int destImageWidth = 0;
    int destImageHeight = 0;

    // Calculate factor of image
    double faktor = (double) sourceImage.Width / (double) sourceImage.Height;

    if (faktor >= 1.0) // Landscape
    {
        destImageWidth = wishImageWidth;
        destImageHeight = (int) (destImageWidth / faktor);
    }
    else // Port
    {
        destImageHeight = wishImageHeight;
        destImageWidth = (int) (destImageHeight * faktor);
    }

    try
    {
        destImage = new Bitmap(sourceImage,destImageWidth,destImageHeight);
        g = Graphics.FromImage(destImage);
        g.InterpolationMode = 
            System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
        g.SmoothingMode = 
            System.Drawing.Drawing2D.SmoothingMode.HighQuality; 
        g.PixelOffsetMode = 
            System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; 
        g.CompositingQuality = 
            System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        g.DrawImage(sourceImage,destImageHeight);
        // Making sure that the file doesn't already exists.
        if (File.Exists(destImagePath)) {
            // If it does delete the old version.
            File.Delete(destImagePath);
        }
        destImage.Save(destImagePath);
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine("*** ERROR-Terror: " + ex.Message)
    }
    finally
    {
        if (g != null) { g.Dispose(); g = null; }
        if (destImage != null) { destImage.Dispose(); destImage = null; }
    }

    sourceImage.Dispose();
    sourceImage = null;
}