有没有人有秘密配方来调整透明图像(主要是GIF)的大小而没有任何质量损失 - 这是怎么回事?
我尝试了很多东西,我得到的最接近的东西还不够好.
看看我的主要形象:
http://www.thewallcompany.dk/test/main.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); } }
我正在使用上面的代码,进行索引调整大小.
有没有人有改进的想法?
如果在缩放后没有要求保留文件类型,我建议采用以下方法.
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); }
结果将具有非常好的抗锯齿边缘
删除已被广告替换的图像小屋图像
如果您必须以gif格式导出图像,那么您就可以使用; GDI +与gif不兼容.有关更多信息,请参阅此博客文章
编辑:我忘了在示例中处理位图; 它已得到纠正
这是我用于一些利用GDI +的应用程序的基本调整大小函数
////// Resize image with GDI+ so that image is nice and clear with required size. /// /// Image to resize /// New height to resize to. /// New width to resize to. ///Image object resized to new dimensions. ///public static Image ImageResize(Image SourceImage, Int32 NewHeight, Int32 NewWidth) { System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(NewWidth, NewHeight, SourceImage.PixelFormat); if (bitmap.PixelFormat == Drawing.Imaging.PixelFormat.Format1bppIndexed | bitmap.PixelFormat == Drawing.Imaging.PixelFormat.Format4bppIndexed | bitmap.PixelFormat == Drawing.Imaging.PixelFormat.Format8bppIndexed | bitmap.PixelFormat == Drawing.Imaging.PixelFormat.Undefined | bitmap.PixelFormat == Drawing.Imaging.PixelFormat.DontCare | bitmap.PixelFormat == Drawing.Imaging.PixelFormat.Format16bppArgb1555 | bitmap.PixelFormat == Drawing.Imaging.PixelFormat.Format16bppGrayScale) { throw new NotSupportedException("Pixel format of the image is not supported."); } System.Drawing.Graphics graphicsImage = System.Drawing.Graphics.FromImage(bitmap); graphicsImage.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality; graphicsImage.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; graphicsImage.DrawImage(SourceImage, 0, 0, bitmap.Width, bitmap.Height); graphicsImage.Dispose(); return bitmap; }
如果它可以与GIF配合使用,我不记得我的头脑,但你可以尝试一下.
注意:我不能完全赞成这个功能.我从网上的其他一些样品拼凑了一些东西,并使它符合我的需求8 ^ D.