调整图像gdi + graphics .net的大小

时间:2009-10-17 15:49:07

标签: c# .net gdi+

我有这种方法缩小我正在处理的网站的图像:

static byte[] createSmallerImage(
   BlogPhoto blogPhoto, 
   int newMaxWidth, 
   int newMaxHeight)
{
  Image img;
  using (MemoryStream originalImage = 
           new MemoryStream(blogPhoto.BlogPhotoImage))
  {
    img = Image.FromStream(originalImage);
  }

  int newWidth;
  int newHeight;
  byte[] arr;

  if (img.Width > img.Height)
  {
    if (img.Width <= newMaxWidth)
    {

      using (MemoryStream thumbStr = new MemoryStream())
      {
        img.Save(thumbStr, ImageFormat.Jpeg);
        img.Dispose();
        arr = thumbStr.ToArray();
      }
      return arr;
    }

    newWidth = newMaxWidth;
    newHeight = 
       (int)(((float)newWidth / (float)img.Width) * (float)img.Height);
  }
  else
  {
    if (img.Height <= newMaxHeight)
    {

      using (MemoryStream thumbStr = new MemoryStream())
      {
        img.Save(thumbStr, ImageFormat.Jpeg);
        img.Dispose();
        arr = thumbStr.ToArray();
      }
      return arr;
    }

    newHeight = newMaxHeight;
    newWidth = 
      (int)(((float)newHeight / (float)img.Height) * (float)img.Width);
  }

  Image thumb = new Bitmap(newWidth, newHeight);

  Graphics g = Graphics.FromImage(thumb);
  g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  g.SmoothingMode = SmoothingMode.HighQuality;
  g.PixelOffsetMode = PixelOffsetMode.HighQuality;
  g.CompositingQuality = CompositingQuality.HighQuality;

  g.DrawImage(img, 0f, 0f, (float)newWidth, (float)newHeight);


  using (MemoryStream thumbStr = new MemoryStream())
  {
    thumb.Save(thumbStr, ImageFormat.Jpeg);
    arr = thumbStr.ToArray();
  }

  g.Dispose();
  img.Dispose();

  return arr;
}

大部分时间它运行良好,但有时它会给我这个例外:GDI +中发生了一般错误。错误代码-2147467259。资料来源:“System.Drawing”。这发生在Image.Save上(...我尽量使这段代码尽可能具有防御性,但仍然没有得到什么导致这种情况。如果有人知道答案那么好,那么批评也是受欢迎的。

4 个答案:

答案 0 :(得分:5)

我个人使用这段代码,没有流(虽然我不关心perfs)来调整图片大小:

public Image resizeImage(int newWidth, int newHeight, string stPhotoPath)
 {
     Image imgPhoto = Image.FromFile(stPhotoPath); 

     int sourceWidth = imgPhoto.Width;
     int sourceHeight = imgPhoto.Height;

     //Consider vertical pics
    if (sourceWidth < sourceHeight)
    {
        int buff = newWidth;

        newWidth = newHeight;
        newHeight = buff;
    }

    int sourceX = 0, sourceY = 0, destX = 0, destY = 0;
    float nPercent = 0, nPercentW = 0, nPercentH = 0;

    nPercentW = ((float)newWidth / (float)sourceWidth);
    nPercentH = ((float)newHeight / (float)sourceHeight);
    if (nPercentH < nPercentW)
    {
        nPercent = nPercentH;
        destX = System.Convert.ToInt16((newWidth -
              (sourceWidth * nPercent)) / 2);
    }
    else
    {
        nPercent = nPercentW;
        destY = System.Convert.ToInt16((newHeight -
              (sourceHeight * nPercent)) / 2);
    }

    int destWidth = (int)(sourceWidth * nPercent);
    int destHeight = (int)(sourceHeight * nPercent);


    Bitmap bmPhoto = new Bitmap(newWidth, newHeight,
              PixelFormat.Format24bppRgb);

    bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
             imgPhoto.VerticalResolution);

    Graphics grPhoto = Graphics.FromImage(bmPhoto);
    grPhoto.Clear(Color.Black);
    grPhoto.InterpolationMode =
        InterpolationMode.HighQualityBicubic;

    grPhoto.DrawImage(imgPhoto,
        new Rectangle(destX, destY, destWidth, destHeight),
        new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
        GraphicsUnit.Pixel);

    grPhoto.Dispose();
    return bmPhoto;
}

希望这有帮助。

答案 1 :(得分:2)

查看Image.FromStream()

的文档

http://msdn.microsoft.com/en-us/library/93z9ee4x.aspx

您需要在图片的生命周期内保持流打开。保持第一个MemoryStream打开的时间更长,它应该可以工作。

答案 2 :(得分:1)

要看的一件事是blogPhoto和基础数据消失。从哪里加载?它是从流中加载的吗?该流是否在createSmallerImage之前关闭?从流关闭的流加载的图像在95%的时间内工作,并且仅偶尔抛出一般GDI +错误。

答案 3 :(得分:0)

我不知道会发生什么,但可能会有更少的MemoryStreams问题消失:

using (Image original = Image.FromStream(new MemoryStream(blogPhoto)))
{
    using (MemoryStream thumbData = new MemoryStream())
    {
        int newWidth;
        int newHeight;
        if ((original.Width <= newMaxWidth) || 
            (original.Height <= newMaxHeight))
        {
            original.Save(thumbData, ImageFormat.Jpeg);
            return thumbData.ToArray();
        }

        if (original.Width > original.Height)
        {
            newWidth = newMaxWidth;
            newHeight = (int)(((float)newWidth / 
                (float)original.Width) * (float)original.Height);
        }
        else
        {
            newHeight = newMaxHeight;
            newWidth = (int)(((float)newHeight / 
                (float)original.Height) * (float)original.Width);
        }

        //original.GetThumbnailImage(newWidth, newHeight, null, IntPtr.Zero)
        //    .Save(thumbData, ImageFormat.Jpeg);
        //return thumbData.ToArray();

        using (Image thumb = new Bitmap(newWidth, newHeight))
        {
            Graphics g = Graphics.FromImage(thumb);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.DrawImage(original, 0f, 0f, (float)newWidth, (float)newHeight);
            thumb.Save(thumbData, ImageFormat.Jpeg);
        }
    }
}