locked
Problem resizing images w/ indexed piel formats. RRS feed

  • Question

  • User-789392182 posted

    So looking around, I have figured out that I can not create a Graphics object from an image with an indexed pixel format. I have written a try catch statement to handle this, but when the new bitmap is created, I am getting a black image for an output.  Here's the code:

     

    public void GenerateThumbNail(string sPhysicalPath,string sOrgFileName,string sThumbNailFileName,ImageFormat oFormat)
    {
    System.Drawing.Image oImg = System.Drawing.Image.FromFile(sPhysicalPath + @"\" + sOrgFileName); System.Drawing.Image oThumbNail =
    new Bitmap(this.ThumbNailSize.Width, this.ThumbNailSize.Height, oImg.PixelFormat);
    Graphics oGraphic ;

    try
    {
    oGraphic = Graphics.FromImage(oThumbNail);
    }
    catch(Exception e)
    {
    // The image is in indexed pixel format
    // Create a temp bitmap
    Bitmap bmpNew = new Bitmap(oImg.Width, oImg.Height);
    oGraphic = Graphics.FromImage(bmpNew);
    oGraphic.DrawImage(oImg,
    new Rectangle(0, 0,bmpNew.Width, bmpNew.Height),0, 0, oImg.Width, oImg.Height, GraphicsUnit.Pixel);
    oImg.Dispose();
    oImg = bmpNew;
    }
    oGraphic.CompositingQuality = CompositingQuality.HighQuality ;
    oGraphic.SmoothingMode = SmoothingMode.HighQuality ;
    oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic ;
    Rectangle oRectangle =
    new Rectangle(0, 0, this.ThumbNailSize.Width, this.ThumbNailSize.Height);
    oGraphic.DrawImage(oImg, oRectangle);
    oThumbNail.Save(sPhysicalPath + "\\" + sThumbNailFileName,oFormat);
    oImg.Dispose();
    }

    Any clues as to why this would be happening? the code works correctly for non-indexed images.  Thanks!  :)

     

    -Jason

    Monday, April 18, 2005 3:17 PM

All replies

  • User-789392182 posted

    Figured it out - here's the code should anyone have the same problem at any point

    -Jason

     

    public void GenerateThumbNail(string sPhysicalPath,string sOrgFileName,string sThumbNailFileName,ImageFormat oFormat)
    {
    System.Drawing.Image oImg = System.Drawing.Image.FromFile(sPhysicalPath + @"\" + sOrgFileName);
    System.Drawing.Image oThumbNail =
    new Bitmap(this.ThumbNailSize.Width, this.ThumbNailSize.Height, oImg.PixelFormat);
    Graphics oGraphic;
    try
    {
    oGraphic = Graphics.FromImage(oThumbNail);
    }
    catch(Exception e)
    {
    Bitmap bmpNew =
    new Bitmap(this.ThumbNailSize.Width, this.ThumbNailSize.Height);
    oGraphic = Graphics.FromImage(bmpNew);
    oThumbNail = bmpNew;
    bmpNew=
    null;
    }
    oGraphic.CompositingQuality = CompositingQuality.HighQuality ;
    oGraphic.SmoothingMode = SmoothingMode.HighQuality ;
    oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic ;
    Rectangle oRectangle =
    new Rectangle(0, 0, this.ThumbNailSize.Width, this.ThumbNailSize.Height);
    oGraphic.DrawImage(oImg, oRectangle);
    oThumbNail.Save(sPhysicalPath + "\\" + sThumbNailFileName,oFormat);
    oImg.Dispose();
    }

    Monday, April 18, 2005 4:32 PM