为什么调整PNG图像失去透明度? [英] Why does resizing a png image lose transparency?

查看:340
本文介绍了为什么调整PNG图像失去透明度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想如下调整大小的图像。我返回调整图像到字节[] ,这样我可以将其存储在数据库中。 PNG图像的透明度会丢失。请帮助,使这更好​​的。

 专用字节[] GetThumbNail(字符串镜像文件,流的ImageStream,
INT imageLen)
{

{
Image.GetThumbnailImageAbort imageCallBack =
新Image.GetThumbnailImageAbort(ThumbnailCallback);
位图getBitmap =新位图(镜像文件);
字节[] = returnByte新的字节[imageLen]
图像getThumbnail = getBitmap.GetThumbnailImage(160,59,
imageCallBack,IntPtr.Zero);使用(图形G = Graphics.FromImage(getThumbnail))

{
g.Smoothi​​ngMode = System.Drawing.Drawing2D.Smoothi​​ngMode.AntiAlias​​;
g.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(getThumbnail,0,0,160,59);
}使用
(MemoryStream的毫秒=新的MemoryStream())
{
getThumbnail.Save(MS,ImageFormat.Png);
getThumbnail.Save(test.png,ImageFormat.Png);
returnByte = ms.ToArray();
}
返回returnByte;
}
赶上(例外)
{
扔;
}
}


解决方案

您代码没有什么挺你认为它...



您使用GetThumbnailImage调整图像大小,那么你画缩略图到本身是相当无意义。你可能失去在第一步骤中的透明度。



创建空白位图代替,并且通过绘制其在空白位图缩放源图像



 专用字节[] GetThumbNail(字符串镜像文件){
尝试{
字节[]的结果;
使用(图片缩略图=新位图(160,59)){使用
(位图源=新位图(镜像文件)){使用
(图形G = Graphics.FromImage(缩略图)) {
g.Smoothi​​ngMode = System.Drawing.Drawing2D.Smoothi​​ngMode.AntiAlias​​;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(源,0,0,160,59);
}
}
使用(MemoryStream的毫秒=新的MemoryStream()){
thumbnail.Save(MS,ImageFormat.Png);
thumbnail.Save(test.png,ImageFormat.Png);
结果= ms.ToArray();
}
}
返回结果;
}赶上(例外){
扔;
}
}



(我删除从未用于任何一些参数这有什么做的结果,像只用于创建从未使用一个字节数组的imageLen参数。)


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

解决方案

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.)

这篇关于为什么调整PNG图像失去透明度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆