dimanche 28 juin 2015

After image resize transparency is removed - gdi+

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...

Aucun commentaire:

Enregistrer un commentaire