Free Support Forum -

Problem with GIF file conversion


I’ve found a particular format of GIF that Aspose.Words does not seem to handle. I am using Aspose.Words to create a document in memory that holds images of various formats (BMP, GIF, PNG, TIF) and then saves it out as a PDF.

All the other image files I tried work fine, but this particular GIF throws ‘A generic error in GDI+’ error.

I’ve worked around this by converting the GIF to a BMP (using Aspose.Imaging) and then placing the BMP file into the Word document.

The GIF is attached.

Cheers, Bruce.

Hi Bruce,

Thanks for your inquiry. I have tested the scenario with following code snippet while using latest version ofAspose.Words for .NET and have not faced any exception.

Document doc = new Document();<o:p></o:p>

DocumentBuilder builder = new DocumentBuilder(doc);<o:p></o:p>

builder.InsertImage(MyDir + “in.gif”);<o:p></o:p>

doc.Save(MyDir + “out.pdf”);

It would be great if you please share some more information about the creation of document by Aspose.Words along with your code snippet.
I will investigate the issue on my side and provide you more information.

Hi Tahir,

This is the function that I am using to export images as PDFs. Using this function the GIF that I sent you causes the GDI+ error to occur.

Cheers, Bruce.

Private Function ConvertImageFile(ByVal inputFileName As String, ByVal outputFileName As String) As Boolean
Dim bReturn As Boolean = False

' Set the licence
Dim licenceAspose As New Aspose.Words.License

' Create Aspose.Words.Document and DocumentBuilder.
' The builder makes it simple to add content to the document.
Dim doc As New Aspose.Words.Document()

Dim builder As New DocumentBuilder(doc)

' Read the image from file, ensure it is disposed.
Using image As System.Drawing.Image = System.Drawing.Image.FromFile(inputFileName)
' Get the number of frames in the image.
Dim framesCount As Integer = image.GetFrameCount(Imaging.FrameDimension.Page)

' Loop through all frames.
For frameIdx As Integer = 0 To framesCount - 1
' Insert a section break before each new page, in case of a multi-frame TIFF.
If frameIdx <> 0 Then
End If

' Select active frame.
image.SelectActiveFrame(Imaging.FrameDimension.Page, frameIdx)

' We want the size of the page to be the same as the size of the image.
' Convert pixels to points to size the page to the actual image size.
Dim ps As PageSetup = builder.PageSetup
ps.PageWidth = ConvertUtil.PixelToPoint(image.Width, image.HorizontalResolution)
ps.PageHeight = ConvertUtil.PixelToPoint(image.Height, image.VerticalResolution)

' Insert the image into the document and position it at the top left corner of the page.
builder.InsertImage(image, RelativeHorizontalPosition.Page, 0, RelativeVerticalPosition.Page, 0, ps.PageWidth, ps.PageHeight, WrapType.None)
Next frameIdx
End Using

' Save the document to PDF.
bReturn = True

Catch ex As Exception
MessageBox.Show("ConvertImageFile: " & ex.Message)
bReturn = False

' Force garbage collection
End Try

Return bReturn
End Functionv

Hi Bruce,

Thanks for sharing the details. Please use the code snippet share at following documentation link for your kind reference.

Please modify following commented line of code with highlighted lines code. This will solve your problem.

'Dim framesCount As Integer = image.GetFrameCount(Imaging.FrameDimension.Page)

<span lang=“EN-GB” style=“font-size:
10.0pt;line-height:115%;font-family:“Courier New”;color:blue;background:yellow;
mso-highlight:yellow;mso-font-kerning:0pt;mso-no-proof:yes”>Dim<span lang=“EN-GB” style=“font-size:10.0pt;line-height:115%;font-family:“Courier New”;
dimension As New

Dim framesCount As Integer = image.GetFrameCount(dimension)