图像上传 - 调整大小以避免内容走私攻击
阅读这篇文章后,我选择将图像大小调整 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
调整图像大小需要将其转换为全彩,并且当您这样做时,调色板级透明度会丢失。
我建议向图像添加 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.