使用C#调整透明图像 [英] Resize transparent images using C#

查看:140
本文介绍了使用C#调整透明图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有秘密配方,以调整大小透明图像(主要是GIF格式)的没有的任何质量损失 - ?什么那么

Does anyone have the secret formula to resizing transparent images (mainly GIFs) without ANY quality loss - what so ever?

我已经试过了一堆东西,最近我得到的还不够好。

I've tried a bunch of stuff, the closest I get is not good enough.

看看我的主要图像:

http://www.thewallcompany.dk/test/main.gif

然后缩放后的图像:

<一个href="http://www.thewallcompany.dk/test/ScaledImage.gif">http://www.thewallcompany.dk/test/ScaledImage.gif

//Internal resize for indexed colored images
void IndexedRezise(int xSize, int ySize)
{
  BitmapData sourceData;
  BitmapData targetData;

  AdjustSizes(ref xSize, ref ySize);

  scaledBitmap = new Bitmap(xSize, ySize, bitmap.PixelFormat);
  scaledBitmap.Palette = bitmap.Palette;
  sourceData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
    ImageLockMode.ReadOnly, bitmap.PixelFormat);
  try
  {
    targetData = scaledBitmap.LockBits(new Rectangle(0, 0, xSize, ySize),
      ImageLockMode.WriteOnly, scaledBitmap.PixelFormat);
    try
    {
      xFactor = (Double)bitmap.Width / (Double)scaledBitmap.Width;
      yFactor = (Double)bitmap.Height / (Double)scaledBitmap.Height;
      sourceStride = sourceData.Stride;
      sourceScan0 = sourceData.Scan0;
      int targetStride = targetData.Stride;
      System.IntPtr targetScan0 = targetData.Scan0;
      unsafe
      {
        byte* p = (byte*)(void*)targetScan0;
        int nOffset = targetStride - scaledBitmap.Width;
        int nWidth = scaledBitmap.Width;
        for (int y = 0; y < scaledBitmap.Height; ++y)
        {
          for (int x = 0; x < nWidth; ++x)
          {
            p[0] = GetSourceByteAt(x, y);
            ++p;
          }
          p += nOffset;
        }
      }
    }
    finally
    {
      scaledBitmap.UnlockBits(targetData);
    }
  }
  finally
  {
    bitmap.UnlockBits(sourceData);
  }
}

我使用上述code,做索引的大小调整。

I'm using the above code, to do the indexed resizing.

没有任何人有改善的想法?

Does anyone have improvement ideas?

推荐答案

如果有在preserving文件类型没有要求缩放我建议以下办法后。

If there's no requirement on preserving file type after scaling I'd recommend the following approach.

using (Image src = Image.FromFile("main.gif"))
using (Bitmap dst = new Bitmap(100, 129))
using (Graphics g = Graphics.FromImage(dst))
{
   g.SmoothingMode = SmoothingMode.AntiAlias;
   g.InterpolationMode = InterpolationMode.HighQualityBicubic;
   g.DrawImage(src, 0, 0, dst.Width, dst.Height);
   dst.Save("scale.png", ImageFormat.Png);
}

结果将具有非常好的抗混叠边

The result will have really nice anti aliased edges

  • 的也换成了一个广告移除图像窝棚图像

如果您必须导出GIF图像你在一坐; GDI +不与GIF发挥好。请参阅<一href="http://www.ben-rush.net/blog/PermaLink.aspx?guid=103ed74d-c808-47ba-b82d-6e9367714b3e&dotnet=consultant"相对=nofollow>这个博客帖子有关它的更多信息。

If you must export the image in gif you're in for a ride; GDI+ doesn't play well with gif. See this blog post about it for more information

编辑:我忘了处置该示例中的位图;它被修正

I forgot to dispose of the bitmaps in the example; it's been corrected

这篇关于使用C#调整透明图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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