我试过这段代码:
private void CreateAnimatedGif(string FileName1 , string FileName2)
{
Bitmap file1 = new Bitmap(FileName1);
Bitmap file2 = new Bitmap(FileName2);
Bitmap bitmap = new Bitmap(file1.Width + file2.Width, Math.Max(file1.Height, file2.Height));
using (Graphics g = Graphics.FromImage(bitmap))
{
g.DrawImage(file1, 0, 0);
g.DrawImage(file2, file1.Width, 0);
}
bitmap.Save(@"d:\test.gif", System.Drawing.Imaging.ImageFormat.Gif);
}
总的来说,它正在发挥作用。但结果还不够好。
第一张图片,因为代码试图让它在相同的尺寸下,我看到底部有一些黑色空间。
第二张图片比第一张图片大。第二张图片在右边。所以我需要它会使左图像与第二个图像的大小/分辨率相同。
如何修复此代码?
这是将两者结合后的新图像结果的示例。为什么它不如我想要的那样好:
答案 0 :(得分:1)
您可以调整左侧图片的大小并设置一些图形属性以获得更好的质量并尝试don't lose the quality:
using (Graphics g = Graphics.FromImage(bitmap))
{
//high quality rendering and interpolation mode
g.SmoothingMode = SmoothingMode.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
//resize the left image
g.DrawImage(file1, new Rectangle(0, 0, file1.Width, file2.Height));
g.DrawImage(file2, file1.Width, 0);
}
结果是:
或者如果你想按比例调整它的高度,只需使用:
//calculate the new width proportionally to the new height it will have
int newWidth = file1.Width + file1.Width / (file2.Height / (file2.Height - file1.Height));
Bitmap bitmap = new Bitmap(newWidth + file2.Width, Math.Max(file1.Height, file2.Height));
using (Graphics g = Graphics.FromImage(bitmap))
{
//high quality rendering and interpolation mode
g.SmoothingMode = SmoothingMode.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
//resize the left image
g.DrawImage( file1, new Rectangle( 0, 0, newWidth, file2.Height ) );
g.DrawImage(file2, newWidth, 0);
}
事实上结果更好: