为什么调整 png 图像大小会失去透明度?

发布于 2024-07-17 07:42:48 字数 1060 浏览 5 评论 0原文

我正在尝试按如下方式调整图像大小。 我将调整大小的图像返回到 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;
  }
}

I am trying to resize an image as follows. I return the resized image into byte[] so that I can store it in database. The transparency of png image is lost. Please help to make this better.

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;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ぃ弥猫深巷。 2024-07-24 07:42:49

您的代码并没有完全按照您的想法进行操作...

您使用 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 参数。)

Your code doesn't do quite what you think that it does...

You use the GetThumbnailImage to resize the image, then you draw the thumbnail image into itself which is rather pointless. You probably lose the transparency in the first step.

Create a blank bitmap instead, and resize the source image by drawing it on the blank bitmap.

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;
  }
}

(I removed some parameters that were never used for anything that had anything to do with the result, like the imageLen parameter that was only used to create a byte array that was never used.)

海未深 2024-07-24 07:42:49

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

Try using the .MakeTransparent() call on your bitmap object.

_失温 2024-07-24 07:42:49

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

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);
}

May be you should do something like this because this thing worked for me:

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文