After image resize transparency is removed - gdi+

230 views Asked by At

I am reading an image from file that contains transparency area in the center (frame image type).

 Image myFrame = Image.FromFile("d:\mypngfile.png");

After i call image resize custome function:

myFrame = resizeImage(myFrame, new Size(otherbmp.Width, otherbmp.Height));

The problem is that after reszing the image it seems transparency is removed.

resize function:

 public Image resizeImage(Image imgToResize, Size size)
    {
        int sourceWidth = imgToResize.Width;
        int sourceHeight = imgToResize.Height;
        float nPercent = 0;
        float nPercentW = 0;
        float nPercentH = 0;
        nPercentW = ((float)size.Width / (float)sourceWidth);
        nPercentH = ((float)size.Height / (float)sourceHeight);
        if (nPercentH < nPercentW)
            nPercent = nPercentH;
        else
            nPercent = nPercentW;
        int destWidth = (int)(sourceWidth * nPercent);
        int destHeight = (int)(sourceHeight * nPercent);
        Bitmap b = new Bitmap(destWidth, destHeight,PixelFormat.Format32bppArgb);
        b.MakeTransparent();
        Graphics g = Graphics.FromImage((Image)b);
        g.CompositingQuality = CompositingQuality.HighQuality;
        g.CompositingMode = CompositingMode.SourceOver;
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.PixelOffsetMode = PixelOffsetMode.HighQuality;
        g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
        g.Dispose();
        return (Image)b;
    }

After i am checking the alpha pixels which doesn't work after resize (it does work before resizing and returns a value). It always return 0!

  public int GetBorderWidth(Bitmap bmp)
            {
                var hy = bmp.Height/ 2;


                while (bmp.GetPixel(0, hy).A == 255 && sz.Width < hx)
                    sz.Width++;

                 return sz.width;
             }

I can't seem to solve this issue no matter what i try...

1

There are 1 answers

0
Dror On BEST ANSWER

I found the issue, it was related that after resizing the image it seems that the border has some transparent pixels ..(like Alpha 150) so the border was not ALPHA 255.. What i did i changed the function GetBorderWidth code with

while (bmp.GetPixel(sz.Width, hy).A > 10 && sz.Width < hx) sz.Width++;

and it seems it fixed the issue. thank you all for giving me some idea of what to check.