如何避免 Graphics.DrawImage 抖动?

发布于 2024-08-04 11:00:49 字数 709 浏览 2 评论 0原文

我想在另一张图片上绘制一张图片并将其转储到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝影如岚 2024-08-11 11:00:49

如果源图像具有大量颜色,则将文件另存为 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)?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文