【问题标题】:Why does resizing a png image lose transparency?为什么调整png图像的大小会失去透明度?
【发布时间】:2010-10-19 17:34:05
【问题描述】:

我正在尝试按如下方式调整图像大小。我将调整大小的图像返回到byte[],以便可以将其存储在数据库中。 png 图像的透明度丢失。请帮助改善这一点。

private byte[] GetThumbNail(string imageFile, Stream imageStream, 
  int imageLen)
{
  try
  {
    Image.GetThumbnailImageAbort imageCallBack = 
      new Image.GetThumbnailImageAbort(ThumbnailCallback);
    Bitmap getBitmap = new Bitmap(imageFile);
    byte[] returnByte = new byte[imageLen];
    Image getThumbnail = getBitmap.GetThumbnailImage(160, 59, 
      imageCallBack, IntPtr.Zero);
    using (Graphics g = Graphics.FromImage(getThumbnail))
    {
      g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
      g.InterpolationMode = 
        System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
      g.DrawImage(getThumbnail, 0, 0, 160, 59);
    }
    using (MemoryStream ms = new MemoryStream())
    {
      getThumbnail.Save(ms, ImageFormat.Png);
      getThumbnail.Save("test.png", ImageFormat.Png);
      returnByte = ms.ToArray();
    }
    return returnByte;
  }
  catch (Exception)
  {
    throw;
  }
}

【问题讨论】:

    标签: c# image resize png transparency


    【解决方案1】:

    尝试对您的位图对象使用.MakeTransparent() 调用。

    【讨论】:

    • 是的!!!有用!你救我!在图像调整大小或裁剪的每一步都使用 .MakeTransparent() 非常重要。创建一个新的 Image() 之后。谢谢!
    • 什么对象有这个 MakeTransparent 调用?
    • 我正在创建流和上传 blob 如何调用 .MakeTransparent() init
    【解决方案2】:

    您的代码并没有像您认为的那样做......

    您使用 GetThumbnailImage 调整图像大小,然后将缩略图图像绘制到自身中,这是毫无意义的。您可能会在第一步中失去透明度。

    改为创建一个空白位图,并通过在空白位图上绘制来调整源图像的大小。

    private byte[] GetThumbNail(string imageFile) {
      try {
        byte[] result;
        using (Image thumbnail = new Bitmap(160, 59)) {
          using (Bitmap source = new Bitmap(imageFile)) {
            using (Graphics g = Graphics.FromImage(thumbnail)) {
              g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
              g.InterpolationMode =  System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
              g.DrawImage(source, 0, 0, 160, 59);
            }
          }
          using (MemoryStream ms = new MemoryStream()) {
            thumbnail.Save(ms, ImageFormat.Png);
            thumbnail.Save("test.png", ImageFormat.Png);
            result = ms.ToArray();
          }
        }
        return result;
      } catch (Exception) {
        throw;
      }
    }
    

    (我删除了一些从未用于与结果有任何关系的参数,例如仅用于创建从未使用过的字节数组的 imageLen 参数。)

    【讨论】:

    • 这个解决方案也没有给我一个透明的图像。
    • 我对 PNG-8 和 PNG-24 图像都进行了尝试,它确实保留了透明度。您可以发布您要调整大小的图像吗?
    • 好吧,如果我查看 test.png,它确实保留了透明度。但是如果我将 byte[] 存储在数据库中并重新访问它,则尝试在我的网页上加载该图像,然后背景显示为灰色。但是当我将图像直接加载到数据库中而不调整大小时,情况并非如此。
    • 我尝试加载 test.png 而不调整到数据库的大小并显示 test.png,我发现透明度丢失了。虽然当我在 Windows 图片查看器中打开 test.png 时,它似乎具有透明度。
    • 你用的是什么浏览器? IE6 不支持 PNG-24 图像的透明度。画图根本不支持透明度。
    【解决方案3】:

    也许你应该这样做,因为这对我有用:

    String path = context.Server.MapPath("/images");
    if (!path.EndsWith("\\"))
        path += "\\";
    path += "none.png";
    
    Image img = CreateThumbnail(Image.FromFile(path));
    
    MemoryStream ms = new MemoryStream();
    img.Save(ms, ImageFormat.Png);
    ms.WriteTo(context.Response.OutputStream);
    

    private System.Drawing.Image CreateThumbnail(System.Drawing.Image i)
    {
        int dWidth = i.Width;
        int dHeight = i.Height;
        int dMaxSize = 150;
    
        if (dWidth > dMaxSize)
        {
            dHeight = (dHeight * dMaxSize) / dWidth;
            dWidth = dMaxSize;
        }
        if (dHeight > dMaxSize)
        {
            dWidth = (dWidth * dMaxSize) / dHeight;
            dHeight = dMaxSize;
        }
        return i.GetThumbnailImage(dWidth, dHeight, delegate() { return false; }, IntPtr.Zero);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-15
      • 2011-03-13
      • 1970-01-01
      • 1970-01-01
      • 2013-01-02
      • 1970-01-01
      • 2012-06-17
      • 2013-09-13
      相关资源
      最近更新 更多