We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Conversion from Png to Jpeg sets the background to black

Hi Support,

I have downloaded the latest version of Aspose.Imaging DLL, but still I am having some issues. I have attached input and output files.

My requirment is to take the input image file (which can be of any format) resize it to desired dimensions and convert it to JPG.

The code I am using is..

using (Aspose.Imaging.Image ImageA = Aspose.Imaging.Image.Load(ImgStandard))

{

int NewWidthA = Convert.ToInt32(EssentialClass.GetSetting("Maximum Property Photo Width"));

ImageA.Resize(NewWidthA, ImageA.Height * NewWidthA / ImageA.Width, Aspose.Imaging.ResizeType.NearestNeighbourResample);

ImageA.Save(PathToFile + "\\" + DocID + ".jpg", new Aspose.Imaging.SaveOptions.JpegSaveOptions());

}

Please Help.....

Thanks,

ValuePRO

Hi,


Please accept my apologies for the delayed response.

I have worked with your sample image and I believe this issue is not related to the Resizing feature. Therefore I have split the existing thread into a new post to address new inquiry.

I can reproduce the similar output as of yours with my simple code as below.

C#

using (var ImageA = Aspose.Imaging.Image.Load(currentpath + “input-original.png”))
{
ImageA.Save(currentpath + “output.jpg”, new Aspose.Imaging.SaveOptions.JpegSaveOptions());
}

The reason for black background of output image is that the Jpeg format does not support Alpha Channel (transparency). So when a Png image having transparent background is saved to Jpeg format, the background is coloured black. You can avoid it by creating a new Jpeg image and drawing the original image on it. Please check the source code for your reference

C#
using (var ImageA = Aspose.Imaging.Image.Load(currentpath + "input-original.png")) { int NewWidthA = 300; int NewHeightA = ImageA.Height * NewWidthA / ImageA.Width; var jpegCreateOptions = new JpegCreateOptions(); jpegCreateOptions.Width = NewWidthA; jpegCreateOptions.Height = NewHeightA; jpegCreateOptions.Source = new StreamSource(new MemoryStream(), true); using (var ImageB = Aspose.Imaging.Image.Create(jpegCreateOptions)) { Graphics graphics = new Graphics(ImageB); graphics.Clear(Color.White); graphics.DrawImage(ImageA, ImageB.Bounds); ImageB.Flush(); ImageB.Save(currentpath + "output.jpeg"); } }