views:

46

answers:

2

I'm using the following code to resize a tif. The tif has an alpha channel set for transparency. I'm trying to resize this image and honour the transparency but at the moment it's coming out with a black background. Any ideas?

public static void ResizeImage(string OriginalImagePath, string NewImagePath, int Width, int Height)
        {
            Size NewSize = new Size(Width, Height);

            using (Image OriginalImage = Image.FromFile(OriginalImagePath))
            {
                //Graphics objects can not be created from bitmaps with an Indexed Pixel Format, use RGB instead.
                PixelFormat Format = OriginalImage.PixelFormat;
                if (Format.ToString().Contains("Indexed"))
                    Format = PixelFormat.Format24bppRgb;

                using (Bitmap NewImage = new Bitmap(NewSize.Width, NewSize.Height, OriginalImage.PixelFormat))
                {
                    using (Graphics Canvas = Graphics.FromImage(NewImage))
                    {
                        Canvas.SmoothingMode = SmoothingMode.AntiAlias;
                        Canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        Canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
                        Canvas.DrawImage(OriginalImage, new Rectangle(new Point(0, 0), NewSize));
                        NewImage.Save(NewImagePath, OriginalImage.RawFormat);
                    }
                }
            }
        }

    }
A: 
Codesleuth
Tried the changes you suggested, it's still coming though with the black background.
RubbleFord
A: 
Canvas.Clear(Color.Transparent)

before you blit.

Tergiver
Tried that with no luck.
RubbleFord
And did you set the pixel format of the new bitmap to Format32bppRgb as Codesleuth suggested? You will not get transparency with 24 bit color, there is no alpha channel.
Tergiver