使用C#调整透明图像的大小
时间:2020-03-05 18:43:57 来源:igfitidea点击:
有没有人知道改变透明图像(主要是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); } }
我正在使用上面的代码来进行索引大小调整。
有没有人有改进的想法?
解决方案
回答
我认为问题在于我们正在执行基于扫描线的调整大小,无论我们多么努力地进行调整,都会导致锯齿。良好的图像调整大小质量要求我们做更多的工作来确定调整大小后的像素覆盖的预先调整大小后的像素的平均颜色。
经营这个网站的人有一篇博客文章,讨论了几种图像大小调整算法。我们可能需要双三次图像缩放算法。
更好的图像尺寸
回答
这是我为一些利用GDI +的应用程序使用的基本调整大小功能
/// <summary> /// Resize image with GDI+ so that image is nice and clear with required size. /// </summary> /// <param name="SourceImage">Image to resize</param> /// <param name="NewHeight">New height to resize to.</param> /// <param name="NewWidth">New width to resize to.</param> /// <returns>Image object resized to new dimensions.</returns> /// <remarks></remarks> 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
回答
如果缩放后不需要保留文件类型,则建议使用以下方法。
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搭配使用效果不佳。有关更多信息,请参见此博客文章。
编辑:我忘记了在示例中的位图;它已被更正
回答
对于可能尝试使用Markus Olsson解决方案动态调整图像大小并将其写出到响应流的任何人。
这将不起作用:
Response.ContentType = "image/png"; dst.Save( Response.OutputStream, ImageFormat.Png );
但这将:
Response.ContentType = "image/png"; using (MemoryStream stream = new MemoryStream()) { dst.Save( stream, ImageFormat.Png ); stream.WriteTo( Response.OutputStream ); }