阅读这篇文章时,我选择将图片大小调整为1px,以阻止内容走私攻击:
Image Uploading - security issues
但出于某种原因,透明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大小的代码?
发布于 2012-02-10 00:24:22
调整图像大小需要将其转换为全色,执行此操作时调色板级别的透明度会丢失。
我建议在图像中添加一个1像素的边框,并使用透明颜色作为边框填充。
https://stackoverflow.com/questions/9211259
复制相似问题