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