0

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?

Community
  • 1
  • 1
user441365
  • 3,934
  • 11
  • 43
  • 62

1 Answers1

0

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.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622