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

Free Support Forum - aspose.com

Embedding GIF image in document

Hi,

I have some code that embeds images like jpg, tiff, and .bmp files into a word document and saving it as a pdf. However, I need to do gif files as well, and in the line of code where it says image.GetFrameCount, I get a GDI drawing exception. Any ideas?

Thanks

Code:

// Create Aspose.Words.Document and DocumentBuilder.

MemoryStream streamImage = new MemoryStream();

// The builder makes it simple to add content to the document.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

// Read the image from file, ensure it is disposed.

if (_strFileName != "" || _strFileName != null)

{

using (Image image = Image.FromFile(_strFileName))

{

// Get the number of frames in the image.

int framesCount = image.GetFrameCount(FrameDimension.Page);

// Loop through all frames.

for (int frameIdx = 0; frameIdx < framesCount; frameIdx++)

{

// Insert a section break before each new page, in case of a multi-frame TIFF.

if (frameIdx != 0)

builder.InsertBreak(BreakType.SectionBreakNewPage);

// Select active frame.

image.SelectActiveFrame(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.

PageSetup ps = 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);

}

}

// Save the document to PDF.

doc.Save(streamImage, SaveFormat.Pdf);

}

return streamImage;

Exception details:

+ [System.Runtime.InteropServices.ExternalException] {"A generic error occurred in GDI+."} System.Runtime.InteropServices.ExternalException
StackTrace " at System.Drawing.Image.GetFrameCount(FrameDimension dimension)\r\n at BarxCommentary.FetchEmailProcessor.WordDocumentUtility.ConvertImageToPdf() in

Hi

Thank you for additional information. You can modify the code like shown below to resolve the problem:

///

/// Converts an image to PDF using Aspose.Words for .NET.

///

/// File name of input image file.

/// Output PDF file name.

public static void ConvertImageToPdf(string inputFileName, string outputFileName)

{

// Create Aspose.Words.Document and DocumentBuilder.

// The builder makes it simple to add content to the document.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

// Read the image from file, ensure it is disposed.

using (Image image = Image.FromFile(inputFileName))

{

// Get the number of frames in the image.

int framesCount = 1;

try

{

framesCount = image.GetFrameCount(FrameDimension.Page);

}

catch

{

// do nothing.

}

// Loop through all frames.

for (int frameIdx = 0; frameIdx < framesCount; frameIdx++)

{

// Insert a section break before each new page, in case of a multi-frame TIFF.

if (frameIdx != 0)

builder.InsertBreak(BreakType.SectionBreakNewPage);

// Select active frame.

image.SelectActiveFrame(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.

PageSetup ps = 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);

}

}

// Save the document to PDF.

doc.Save(outputFileName);

}

Hope this helps.

Best regards,