This forum contains outdated content and is available for reading only. Please contact technical support if you have any questions.

PSD with transparent background goes black when converting to JPG

When using the following code to read / resize and output a PSD as a JPG, the transparent background is output as black. Is there a way to have this output as white?


Bitmap ModifiedBitmap = new Bitmap();
PsdReader PSD = new PsdReader(@"c:\PSDs\My.psd");
IFrame PSDFrame = PSD.LoadFrame(0);
PSDFrame.GetBitmap(ModifiedBitmap);

ModifiedBitmap.Transforms.Resize(Width, Height, InterpolationMode.Lanczos3Filter);
    
//Specify that we are going to save to JPEG
JpegEncoderOptions EncoderOptions = new JpegEncoderOptions(95, true);

//Save image to response stream
context.Response.Clear();
context.Response.ContentType = "image/jpeg";
ModifiedBitmap.Save(context.Response.OutputStream, EncoderOptions);
ModifiedBitmap.Dispose();
context.Response.End();


Thanks,
Matt
Hello Matt,

You just need to discard alpha channel and set necessary background color. You can do it as follows:
Bitmap ModifiedBitmap = new Bitmap();
PsdReader PSD = new PsdReader(@"c:\PSDs\My.psd");
IFrame PSDFrame = PSD.LoadFrame(0);
PSDFrame.GetBitmap(ModifiedBitmap);

ModifiedBitmap.Transforms.Resize(Width, Height, InterpolationMode.Lanczos3Filter);

Aurigma.GraphicsMill.Color background = System.Drawing.Color.White;
ModifiedBitmap.Channels.DiscardAlpha(background);

//Specify that we are going to save to JPEG
JpegEncoderOptions EncoderOptions = new JpegEncoderOptions(95, true);

//Save image to response stream
context.Response.Clear();
context.Response.ContentType = "image/jpeg";
ModifiedBitmap.Save(context.Response.OutputStream, EncoderOptions);
ModifiedBitmap.Dispose();
context.Response.End();