如何避免 Graphics.DrawImage 抖动?
我想在另一张图片上绘制一张图片并将其转储到 HttpResponse。我的代码如下所示:
//file name points to a gif image
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(filename);
System.Drawing.Image smallImage = System.Drawing.Image.FromFile(smallFilename);
using(Bitmap tempImage = new Bitmap(originalImage))
{
Graphics graphics = Graphics.FromImage(tempImage);
PointF ulCorner = new PointF(10.0F, 10.0F);
graphics.DrawImage(windfarmImage, ulCorner);
}
tempImage.Save(Response.OutputStream, ImageFormat.Gif);
如果我将最后一行更改为
tempImage.Save(Response.OutputStream, ImageFormat.Jpeg);
它解决了问题。但结果我必须有 png 。我可以以某种方式保留原始文件中的调色板吗?原始文件是 gif,所以应该可以得到 gif 结果,而不会丢失任何颜色,我猜。
I want to draw one picture over another and dump it to HttpResponse. My code looks like this:
//file name points to a gif image
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(filename);
System.Drawing.Image smallImage = System.Drawing.Image.FromFile(smallFilename);
using(Bitmap tempImage = new Bitmap(originalImage))
{
Graphics graphics = Graphics.FromImage(tempImage);
PointF ulCorner = new PointF(10.0F, 10.0F);
graphics.DrawImage(windfarmImage, ulCorner);
}
tempImage.Save(Response.OutputStream, ImageFormat.Gif);
If I change last line to
tempImage.Save(Response.OutputStream, ImageFormat.Jpeg);
it fixes the problem. But I have to have png as a result. Can I somehow keep palette from original file? Original file is gif, so it should be possible to have gif as a result without loosing any colors I guess.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果源图像具有大量颜色,则将文件另存为 Gif 可能会导致一些抖动,因为调色板大小会减小到 256 种颜色。
是否需要将文件另存为 Gif,或者可以尝试其他格式(例如 Jpeg 或 Png)?
If the source images have a large number of colours, then saving the file as a Gif may cause some dithering as the palette size is reduced to 256 colours.
Is it a requirement to save the file as a Gif, or could a different format be tried (such as Jpeg or Png)?