上传多个图像时出现GDI+错误,然后创建缩略图

本文关键字:错误 然后 创建 略图 GDI+ 图像 | 更新日期: 2023-09-27 18:26:26

我有一个图像上传页面,当我只上传文件时,它运行得很好。

我添加了一个"创建缩略图"功能。当缩略图进程开始时,文件系统似乎对图像有一个句柄。

只有当图像超过250K时,我才会得到"未指定的GDI+错误"。当文件低于250K时,会按预期创建缩略图。

我有什么选择?这里有优雅的解决方案吗?我想要一些不生气的东西
此外,我正在使用HttpFileCollection,这样我们就可以一次上传多个图像。我试过在创建缩略图时使用.Dispose,但在我们到达这一点之前它失败了。

public void Upload_Click(object Sender, EventArgs e)
{
 string directory = Server.MapPath(@"~'images'");
  HttpFileCollection hfc = Request.Files;
  for (int i = 0; i < hfc.Count; i++)
  {
    HttpPostedFile hpf = hfc[i];
    if (hpf.ContentLength > 0)
    {
      string fileName = hpf.FileName;
      fileName = fileName.Replace(" ", "");
      hpf.SaveAs(fileName);
      createThumbnail(fileName);
    }
  }
}
private void createThumbnail(string filename)
{
  Image image = Image.FromFile(filename);
  Image thumb = image.GetThumbnailImage(100,100, () => false, IntPtr.Zero);
  thumb.Save(filename);
  image.Dispose();
  thumb.Dispose();
 }

上传多个图像时出现GDI+错误,然后创建缩略图

请告诉我这是否更好:

    public string ImageDirectory { get { return Server.MapPath(@"~'images'"); } }
    public void OnUploadClick(object sender, EventArgs e)
    {
        var files = HttpContext.Request.Files.AllKeys.AsEnumerable()
            .Select(k =>HttpContext.Request.Files[k]);
        foreach(var file in files)
        {
            if(file.ContentLength <= 0)
                continue;
            string savePath = GetFullSavePath(file);
            var dimensions = new Size(100, 100);
            CreateThumbnail(file,savePath,dimensions);
        }
    }
    private void CreateThumbnail(HttpPostedFile file,string savePath, Size dimensions)
    {
        using (var image = Image.FromStream(file.InputStream))
        {
            using (var thumb = image.GetThumbnailImage(dimensions.Width, dimensions.Height, () => false, IntPtr.Zero))
            {
                thumb.Save(savePath);
            }
        }
    }
    private string GetFullSavePath(HttpPostedFile file)
    {
        string fileName = System.IO.Path.GetFileName(file.FileName).Replace(" ", "");
        string savePath = System.IO.Path.Combine(this.ImageDirectory, fileName);
        return savePath;
    }

编辑-

前臂应该更多地遵循这种模式:

var files = HttpContext.Request.Files.AllKeys.AsEnumerable()
                .Select(k =>HttpContext.Request.Files[k]);
foreach(var file in files)
{
}

您可以尝试使用此代码来创建缩略图。

       MemoryStream ms = new MemoryStream(File.ReadAllBytes(path));
       Bitmap originalBMP = new Bitmap(ms);
       int maxWidth = 200;
       int maxHeight = 200;
       // Calculate the new image dimensions 
       int origWidth = originalBMP.Width;
       int origHeight = originalBMP.Height;
       double sngRatio = Convert.ToDouble(origWidth) / Convert.ToDouble(origHeight);
       // New dimensions
       int newWidth = 0;
       int newHeight = 0;
       try
       {
           // max 200 by 200
           if ((origWidth <= maxWidth && origHeight <= maxHeight) || origWidth <= maxWidth)
           {
               newWidth = origWidth;
               newHeight = origHeight;
           }
           else
           {
               // Width longer (shrink width)
               newWidth = 200;
               newHeight = Convert.ToInt32(Convert.ToDouble(newWidth) / sngRatio);
           }
           // Create a new bitmap which will hold the previous resized bitmap 
           Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);
           // Create a graphic based on the new bitmap 
           Graphics oGraphics = Graphics.FromImage(newBMP);
           // Set the properties for the new graphic file 
           oGraphics.SmoothingMode = SmoothingMode.AntiAlias;
           oGraphics.InterpolationMode = InterpolationMode.High;
           // Draw the new graphic based on the resized bitmap 
           oGraphics.CompositingQuality = CompositingQuality.HighSpeed;
           oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);
           // Save the new graphic file to the server 
           EncoderParameters p = new EncoderParameters(1);
           p.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, 70);     // Percent Compression
           MemoryStream savedBmp = new MemoryStream();
           newBMP.Save(savedBmp, ImageCodecInfo.GetImageEncoders()[1], p);
                          // Once finished with the bitmap objects, we deallocate them. 
           originalBMP.Dispose();
           newBMP.Dispose();
           oGraphics.Dispose();
           savedBmp.Dispose();

当然还有更多的工作,但它确实给了你更大的控制权。

相关文章: