图像上传 - 调整大小以避免内容走私攻击

发布于 2025-01-03 19:26:53 字数 1043 浏览 0 评论 0原文

阅读这篇文章后,我选择将图像大小调整 1px 以阻止内容走私攻击:

图像上传- 安全问题

但由于某种原因,透明的 gif 会带有黑色背景。这是代码:

        Bitmap FullsizeImage = (Bitmap)System.Drawing.Image.FromStream(OriginalFile);

        //FullsizeImage.MakeTransparent(Color.Transparent);

        int NewWidth = FullsizeImage.Width - 1;
        int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width;

        Bitmap CroppedImage = new Bitmap(NewWidth, NewHeight);
        Graphics g = Graphics.FromImage(CroppedImage);
        g.DrawImage(FullsizeImage, new Rectangle(0, 0, FullsizeImage.Width, FullsizeImage.Height));

        CroppedImage.Save(ImagePath, FullsizeImage.RawFormat);

        g.Dispose();
        CroppedImage.Dispose();
        FullsizeImage.Dispose();

我尝试使用 FullsizeImage.MakeTransparent(Color.Transparent) 但将图像转换为 png,我想保留原始格式。

我的最终目的是避免可能的攻击,所以知道如何以不同的方式而不是调整大小来做到这一点?或者有人有一个代码可以通过保持格式来实际调整透明 gif 的大小?

Reading this post I opted for resizing an image by 1px to stop content-smuggling attacks:

Image Uploading - security issues

But for some reason transparent gifs come out with a black background. This is the code:

        Bitmap FullsizeImage = (Bitmap)System.Drawing.Image.FromStream(OriginalFile);

        //FullsizeImage.MakeTransparent(Color.Transparent);

        int NewWidth = FullsizeImage.Width - 1;
        int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width;

        Bitmap CroppedImage = new Bitmap(NewWidth, NewHeight);
        Graphics g = Graphics.FromImage(CroppedImage);
        g.DrawImage(FullsizeImage, new Rectangle(0, 0, FullsizeImage.Width, FullsizeImage.Height));

        CroppedImage.Save(ImagePath, FullsizeImage.RawFormat);

        g.Dispose();
        CroppedImage.Dispose();
        FullsizeImage.Dispose();

I tried using FullsizeImage.MakeTransparent(Color.Transparent) but that transforms the image into a png and I want to keep the original format.

My final intention is to avoid a posible attack, so any idea on how to do this in a different way rather than resizing? Or anyone has a code that actually resizes transparent gifs by keeping the format?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

情深缘浅 2025-01-10 19:26:53

调整图像大小需要将其转换为全彩,并且当您这样做时,调色板级透明度会丢失。

我建议向图像添加 1 像素边框,并使用透明颜色进行边框填充。

Resizing an image requires that it be converted to full color, and the palette-level transparency is lost when you do that.

I would suggest adding a 1-pixel border to the image, and use the transparent color for the border fill.

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