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

Resize and optimize Image before upload C#

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

我该怎么做?

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

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

您缺少一些正确调整图像大小的代码。 Appending 是一个函数,可以根据您为其提供的宽度和高度值正确调整图像大小(在此示例中,如果可能,图像将调整为 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, 0, 0, destImageWidth, 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;
}