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

Free Support Forum - aspose.com

Png conversion with transparency

hi, i’m using this code to convert variuos images format into png (and then i put png in pdf file)

Aspose.Imaging.ImageOptions.PngOptions OptionsI = new Aspose.Imaging.ImageOptions.PngOptions();
OptionsI.TransparentColor = new Aspose.Imaging.TransparentColorSetting(Aspose.Imaging.Color.Transparent);
Aspose.Imaging.ResolutionSetting oResI = new Aspose.Imaging.ResolutionSetting(Convert.ToDouble(m_iResolution), Convert.ToDouble(m_iResolution));
OptionsI.ResolutionSettings = oResI;
((Aspose.Imaging.Image)m_oDocument).Save(oMTempStream, OptionsI);</i>

but i’m experiencing some errors with images with transparency in it
If i convert a png file with transparency in image (another png) it will respect transparent areas, and resultant pdf has white color as transparent, but when i try with some sample GIF or TIFF (in attachment - i don’t have a photoshop sample to proofing), transparent color - in png and pdf - is rendered as black!

With the same input images i have another strange error when createing PDF file with this code:
<i>Aspose.Pdf.Document oPdf = new Aspose.Pdf.Document();
Aspose.Pdf.Page oPage = oPdf.Pages.Add();

if (((Aspose.Imaging.Image)m_oDocument).Width > ((Aspose.Imaging.Image)m_oDocument).Height)
{

double iW = oPage.PageInfo.Width;
double iH = oPage.PageInfo.Height;
oPage.PageInfo.Width = iH;
oPage.PageInfo.Height = iW;
}

oPage.PageInfo.Margin.Bottom = 0;
oPage.PageInfo.Margin.Top = 0;
oPage.PageInfo.Margin.Left = 0;
oPage.PageInfo.Margin.Right = 0;

oPage.MediaBox = new Aspose.Pdf.Rectangle(0, 0, ((Aspose.Imaging.Image)m_oDocument).Width, ((Aspose.Imaging.Image)m_oDocument).Height);
oPage.CropBox = new Aspose.Pdf.Rectangle(0, 0, ((Aspose.Imaging.Image)m_oDocument).Width, ((Aspose.Imaging.Image)m_oDocument).Height);

Aspose.Pdf.Image oImage = new Aspose.Pdf.Image();
oPage.Paragraphs.Add(oImage);
oImage.ImageStream = oMTempStream;
oPdf.Save(_oOutputStream);

The Pdf file “stretch the images” in it’s pages… i can’t understand: for transparency and for pdf page dimensions i have no problems with others files.

What can i do to manage this?
Thank’s

Hi,


Thank you for contacting Aspose support.

We have evaluated your presented scenario with your provided sample images. The problem you are facing with transparency of the PNG images while converting other formats such as GIF & TIFF is caused due to the difference in the color modes. Please set the PngOptions.ColorType property to PngColorType.TruecolorWithAlpha in order to get the desired transparency.

Please check the below provided code snippet,

C#

Aspose.Imaging.ImageOptions.PngOptions OptionsI = new Aspose.Imaging.ImageOptions.PngOptions();
OptionsI.ColorType = PngColorType.TruecolorWithAlpha;
OptionsI.TransparentColor = new Aspose.Imaging.TransparentColorSetting(Aspose.Imaging.Color.Transparent);
OptionsI.ResolutionSettings = new Aspose.Imaging.ResolutionSetting(Convert.ToDouble(72), Convert.ToDouble(96));

Regarding the other part of your inquiry, I have moved this thread to Aspose.Total support forum for better coverage of the inquiry, and have requested my colleagues from the Aspose.Pdf team to share their feedback.

Please feel free to write back in case you face any difficulty.

Hi,


I would like to answer from Aspose.Pdf perspective. The reasons the image in resultant PDF is being stretched is because the source image is of small dimensions and you are using following code line to set the CropBox for PDF page.

oPage.CropBox = new Aspose.Pdf.Rectangle(0, 0, ((Aspose.Imaging.Image)m_oDocument).Width, ((Aspose.Imaging.Image)m_oDocument).Height);

If you comment out this code line, the image in PDF will appear with its actual dimensions. See attached PDF file. In case I have not properly understood this problem, please share some further details.

[C#]

//Create
an Image object and load an existing file using the file path
<o:p></o:p>

using (Aspose.Imaging.Image m_oDocument = Aspose.Imaging.Image.Load(@"C:\pdftest\samples\transbugs.gif"))

{

Aspose.Imaging.ImageOptions.PngOptions OptionsI = new Aspose.Imaging.ImageOptions.PngOptions();

OptionsI.ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.TruecolorWithAlpha;

OptionsI.TransparentColor = new Aspose.Imaging.TransparentColorSetting(Aspose.Imaging.Color.Transparent);

OptionsI.ResolutionSettings = new Aspose.Imaging.ResolutionSetting(Convert.ToDouble(72), Convert.ToDouble(96));

//Convert.ToDouble(m_iResolution));

OptionsI.ResolutionSettings = new Aspose.Imaging.ResolutionSetting(320, 223);

MemoryStream tempStream = new MemoryStream();

//((Aspose.Imaging.Image)m_oDocument).Save(oMTempStream, OptionsI);

//image.Save(tempStream);

((Aspose.Imaging.Image)m_oDocument).Save(tempStream, OptionsI);

m_oDocument.Save(@"C:\pdftest\samples\transbugs_updated.gif");

Aspose.Pdf.Document oPdf = new Aspose.Pdf.Document();

Aspose.Pdf.Page oPage = oPdf.Pages.Add();

if (((Aspose.Imaging.Image)m_oDocument).Width > ((Aspose.Imaging.Image)m_oDocument).Height)

{

//doc.PageInfo.IsLandscape = true;

double iW = oPage.PageInfo.Width;

double iH = oPage.PageInfo.Height;

oPage.PageInfo.Width = iH;

oPage.PageInfo.Height = iW;

}

oPage.PageInfo.Margin.Bottom = 0;

oPage.PageInfo.Margin.Top = 0;

oPage.PageInfo.Margin.Left = 0;

oPage.PageInfo.Margin.Right = 0;

oPage.MediaBox = new Aspose.Pdf.Rectangle(0, 0, ((Aspose.Imaging.Image)m_oDocument).Width, ((Aspose.Imaging.Image)m_oDocument).Height);

// oPage.CropBox = new Aspose.Pdf.Rectangle(0, 0, ((Aspose.Imaging.Image)m_oDocument).Width, ((Aspose.Imaging.Image)m_oDocument).Height);

Aspose.Pdf.Image oImage = new Aspose.Pdf.Image();

oPage.Paragraphs.Add(oImage);

oImage.ImageStream = tempStream;

oPdf.Save("c:/pdftest/PDFFileOutput.pdf");

}

Thank’s

For stretching i have to consider images small and large: so i need to introduce some considerations in my code for every case.
Using some if … else considering images width and heigth and creating mediabox and cropbox accordingly, works pretty good and resulting pdf has no blank space or stretched images

Hi,


Thanks for the acknowledgement.

We are glad to hear that your problem is resolved. Please continue using our API’s and in the event of any further query, please feel free to contact.