我有一个读取图像、调整大小并将其定位在新背景上的例程(全新的位图,只是设置了大小)。
这一切都非常完美,但现在我想减小它输出的PNG文件的大小——如果我输出JPEG文件,我会得到我期望的 ~4K 左右的文件大小,但我的 PNG 文件的大小超过 30K .
我知道我永远无法使用 PNG 达到 JPEG 级别,但我认为我可以做得更好。
当我将输出的 PNG 加载到Fireworks中时,我注意到背景和调整大小的图像仍然是单独的图层。在 Fireworks 中展平 PNG 可将文件大小减少 5 到 10K。
那么,首先有没有办法以编程方式在输出时展平 PNG?
其次,还有什么其他人可以推荐来减小 PNG 的大小吗?
我正在使用 PNG 文件,因为我希望将背景保持为透明。
代码:
private static void ResizeImage(String ImageInPath, int MaxWidth, int MaxHeight, String ImageOutPath, Boolean PadImage, Color MyColour)
{
Bitmap MyImage = new Bitmap(ImageInPath);
Bitmap MyResizedImage = null;
int XPosition = 0;
int YPosition = 0;
float Ratio = MyImage.Width / (float)MyImage.Height;
int MyImageHeight = MyImage.Height;
int MyImageWidth = MyImage.Width;
if (MyImage.Width > MyImage.Height)
{
if (MyImage.Width > MaxWidth)
MyResizedImage = new Bitmap(MyImage, new Size(MaxWidth, (int)Math.Round(MaxWidth /
Ratio, 0)));
YPosition = (MaxHeight / 2) - (MyResizedImage.Height / 2);
}
else if (MyImage.Height > MyImage.Width)
{
if (MyImage.Height > MaxHeight)
MyResizedImage = new Bitmap(MyImage, new Size((int)Math.Round(MaxWidth * Ratio,
0), MaxHeight));
XPosition = (MaxWidth / 2) - (MyResizedImage.Width / 2);
}
if (PadImage)
{
Bitmap MyUnderlay = new Bitmap(MaxWidth, MaxHeight);
var Canvas = Graphics.FromImage(MyUnderlay);
Canvas.Clear(MyColour);
Canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
Canvas.DrawImage(MyResizedImage, XPosition, YPosition);
Canvas.Save();
if (MyColour == Color.Transparent)
{
MyUnderlay.Save(ImageOutPath + ".png", ImageFormat.Png);
}
else
{
MyUnderlay.Save(ImageOutPath, ImageFormat.Jpeg);
}
Canvas.Dispose();
MyUnderlay.Dispose();
}
else
{
MyResizedImage.Save(ImageOutPath, ImageFormat.Jpeg);
}
MyResizedImage.Dispose();
MyImage.Dispose();
}