Mike Luken
Mike Luken

Reputation: 525

C#.net image rescaling color issues

I am trying to rescale an image on the fly with c#.net. Everything appeared to be working correctly, but upon closer inspection, the colors do not look right.

The code appears to be pretty straight forward and it rescales fine, but why does the original image appear a pinker color than the scaled picture?

using (Bitmap origBitmap = new Bitmap("my_picture.jpg"))
{
    using (Bitmap outputImage = new Bitmap(1024, 768, origBitmap.PixelFormat))
    {
        outputImage.SetResolution(origBitmap.HorizontalResolution, origBitmap.VerticalResolution);
        using (Graphics g = Graphics.FromImage(outputImage))
        {
            g.Clear(Color.Black);
            g.CompositingMode = CompositingMode.SourceCopy;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(
                origBitmap,
                new Rectangle(0, 0, 1024, 768),
                new Rectangle(0, 0, origBitmap.Width, origBitmap.Height),
                GraphicsUnit.Pixel
            );

            context.Response.ContentType = "image/jpeg";
            outputImage.Save(context.Response.OutputStream, ImageFormat.Jpeg);
        }
    }
}

Attached you can see the difference in the colors. Hoping I am missing something simple?

picture_scaling_issues.jpg

Upvotes: 1

Views: 327

Answers (1)

Mike Luken
Mike Luken

Reputation: 525

Looks like I answered my own question!

using (Bitmap origBitmap = (Bitmap) Bitmap.FromFile("my_file.jpg", true))
{ … }

The "true" parameter is for "useEmbeddedColorManagement". Setting that to true fixes the problem...

Upvotes: 2

Related Questions