locked
Converting a image to PNG format- but problem with black background RRS feed

  • Question

  • Hello,

    I am re-sizing an image and saving it in PNG format. User can upload an image and it is resized and saved as png.

    I have been able to successfully resize the image and store it as jpeg. But, when I am trying to save the image as a png, I am getting black background around the image when a user uploads any kind of image. Could someone please suggest me if I am doing something wrong in the code.

    
    
    
    
    private Bitmap ResizeImage(Bitmap imgPhoto, int Width, int Height)
      {
        int sourceWidth = imgPhoto.Width;
        int sourceHeight = imgPhoto.Height;
        int sourceX = 0;
        int sourceY = 0;
        int destX = 0;
        int destY = 0;
    
        float nPercent = 0;
        float nPercentW = 0;
        float nPercentH = 0;
    
        nPercentW = ((float)Width / (float)sourceWidth);
        nPercentH = ((float)Height / (float)sourceHeight);
        if (nPercentH < nPercentW)
        {
          nPercent = nPercentH;
          destX = System.Convert.ToInt16((Width -
                 (sourceWidth * nPercent)) / 2);
        }
        else
        {
          nPercent = nPercentW;
          destY = System.Convert.ToInt16((Height -
                 (sourceHeight * nPercent)) / 2);
        }
    
        int destWidth = (int)(sourceWidth * nPercent);
        int destHeight = (int)(sourceHeight * nPercent);
    
        Bitmap bmPhoto = new Bitmap(Width, Height,
                 PixelFormat.Format24bppRgb);
        bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
                 imgPhoto.VerticalResolution);
    
        Graphics grPhoto = Graphics.FromImage(bmPhoto);
        //change the color of the image padding
        //grPhoto.Clear(Color.White);
        grPhoto.Clear(Color.Transparent);
    
        grPhoto.InterpolationMode =
            InterpolationMode.HighQualityBicubic;
    
        grPhoto.DrawImage(imgPhoto,
          new Rectangle(destX, destY, destWidth, destHeight),
          new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
          GraphicsUnit.Pixel);
    
        grPhoto.Dispose();
    
        return bmPhoto;
    
      }
    
    
    Saving function
    
    string convertedImage = filenameimage + ".png";
    
    String path = HttpContext.Current.Request.PhysicalApplicationPath + "resizedimages\\";
    
    uploadedimage.Save(path + convertedImage, System.Drawing.Imaging.ImageFormat.Png);
    
    
    
    

    Thursday, August 18, 2011 8:57 PM

Answers

  • I don't have a direct solution, but most likely it is because PNG's have the alpha channel pre-multiplied.  I see that the bitmap you create is 24 bits per pixel, and in order to have an alpha channel, it needs to be 32 bits per pixel.  Experiment with that.
    MCP
    • Proposed as answer by Thorsten Gudera Friday, August 19, 2011 12:47 AM
    • Marked as answer by smksu Friday, August 19, 2011 1:26 PM
    Thursday, August 18, 2011 9:06 PM
  • Hi,

    webJose is right IMHO. You need a 32bpp Image for getting the Alpha-Channel for the png. Otherwise your grPhoto.Clear(Color.Transparent); statement will *not* clear it transparent.

    So you only need to change the line:

    Bitmap bmPhoto = new Bitmap(Width, Height,
           PixelFormat.Format24bppRgb);
    

    to:

          Bitmap bmPhoto = new Bitmap(Width, Height,
               PixelFormat.Format32bppArgb);

    and it should work correctly.

    Regards,

      Thorsten


    • Marked as answer by smksu Friday, August 19, 2011 1:26 PM
    Friday, August 19, 2011 12:47 AM

All replies

  • I don't have a direct solution, but most likely it is because PNG's have the alpha channel pre-multiplied.  I see that the bitmap you create is 24 bits per pixel, and in order to have an alpha channel, it needs to be 32 bits per pixel.  Experiment with that.
    MCP
    • Proposed as answer by Thorsten Gudera Friday, August 19, 2011 12:47 AM
    • Marked as answer by smksu Friday, August 19, 2011 1:26 PM
    Thursday, August 18, 2011 9:06 PM
  • Hi,

    webJose is right IMHO. You need a 32bpp Image for getting the Alpha-Channel for the png. Otherwise your grPhoto.Clear(Color.Transparent); statement will *not* clear it transparent.

    So you only need to change the line:

    Bitmap bmPhoto = new Bitmap(Width, Height,
           PixelFormat.Format24bppRgb);
    

    to:

          Bitmap bmPhoto = new Bitmap(Width, Height,
               PixelFormat.Format32bppArgb);

    and it should work correctly.

    Regards,

      Thorsten


    • Marked as answer by smksu Friday, August 19, 2011 1:26 PM
    Friday, August 19, 2011 12:47 AM
  • Hello Guys,

    Thank you for your prompt response. It did really work. Thank you!

    Friday, August 19, 2011 1:26 PM