Free Support Forum - aspose.com

Access denied for image

Hi guys,


I need to convert the first page of a Word-Document to a thumbnail image.

Here is the code I am using for creating an image of the first page:

public static Image CreateThumbnailFromDOC(string docPath,string thumbnailPath)
{
// Create jpg of first page
Aspose.Words.Document wordDocument = new Aspose.Words.Document(docPath);

ImageSaveOptions saveOptions = new ImageSaveOptions(SaveFormat.Jpeg);
saveOptions.Resolution = 96;

wordDocument.Save(thumbnailPath, saveOptions);

// Resize jpg
Image imgOriginal = Image.FromFile(thumbnailPath);
Image imgResized = ResizeImage(imgOriginal, new Size(300, 400), true);

// Exception occurs on the next line !!!
imgResized.Save(thumbnailPath, ImageFormat.Jpeg);

return imgResized;
}

Everything works until the red marked line. Seems like the jpg file is still locked from the wordDocument.Save() method.

What can I do?

Hi Ingmar,


Thanks for your inquiry. Perhaps, you are using an older version of Aspose.Words; as with Aspose.Words v11.10.0, I am unable to reproduce this problem on my side. I would suggest you please upgrade to the latest version of Aspose.Words i.e. v11.10.0 and let us know how it goes on your side. I hope, this will help.

Moreover, I suggest you to use Image.GetThumbnailImage method to generate the thumbnail as shown in following code snippet.

<span lang=“EN-GB” style=“font-size:10.0pt;font-family:“Courier New”;color:#2B91AF;mso-font-kerning:
0pt;mso-no-proof:yes”>Document<span lang=“EN-GB” style=“font-size:10.0pt;
font-family:“Courier New”;mso-font-kerning:0pt;mso-no-proof:yes”> wordDocument
= new Document(MyDir

  • “in.doc”);<o:p></o:p>

ImageSaveOptions saveOptions = new ImageSaveOptions(SaveFormat.Jpeg);

saveOptions.Resolution = 96;

saveOptions.PageCount = 1;

saveOptions.PageIndex = 0;

wordDocument.Save(MyDir + "out.jpg", saveOptions);

//// Resize jpg

Image imgOriginal = Image.FromFile(MyDir + "out.jpg");

//Image imgResized = ResizeImage(imgOriginal, new Size(300, 400), true);

Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);

Image myThumbnail = imgOriginal.GetThumbnailImage(300, 400, myCallback, IntPtr.Zero);

// Exception occurs on the next line !!!

myThumbnail.Save(MyDir + "thumbnail.jpg", ImageFormat.Jpeg);


If you still face problem, please attach your input Word document here for testing. I will investigate the issue on my side and provide you more information.

Hi Tahir,



and thanks for your reply and support.



Well, your snippet works, because you are saving the wordDocument to out.jpg, but the thumbnail to thumbnail.jpg. Since thumbnail.jpg doesn’t exist at this point, it is not locked.



If you try and do it my way though, I believe you should get an exception as well. See how i adapted your code:



public static void Test(string inFilePath, string outFilePath)

{

Document wordDocument = new Document(inFilePath);



ImageSaveOptions saveOptions = new ImageSaveOptions(SaveFormat.Jpeg);

saveOptions.Resolution = 96;

saveOptions.PageCount = 1;

saveOptions.PageIndex = 0;



wordDocument.Save(outFilePath, saveOptions);



//// Resize jpg

Image imgOriginal = Image.FromFile(outFilePath);

Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);

Image myThumbnail = imgOriginal.GetThumbnailImage(300, 400, myCallback, IntPtr.Zero);



// Exception occurs on the next line: “A generic error occurred in GDI+”

try

{

myThumbnail.Save(outFilePath, ImageFormat.Jpeg);

}

catch (Exception ex)

{

throw;

}

}



This has nothing to do with a certain Word-Document. On my side this fails with any *.doc.



Any chance this happens because my method is static?



Oh and yes, I am using Aspose.Words 11.10.0.0.



Thanks

Ingmar





Hi Ingmar,


Thanks for your inquiry. In this case, you can save the image to stream by using Aspose.Words and then generate the thumbnail as shown in following code snippet. Hope this helps you. Please let us know if you have any more queries.

<span lang=“NL” style=“font-size:
10.0pt;font-family:“Courier New”;color:#2B91AF;mso-font-kerning:0pt;mso-ansi-language:
NL;mso-no-proof:yes”>Document<span lang=“NL” style=“font-size:10.0pt;
font-family:“Courier New”;mso-font-kerning:0pt;mso-ansi-language:NL;mso-no-proof:
yes”> wordDocument = new Document(MyDir + “input_1.docx”);<o:p></o:p>

ImageSaveOptions saveOptions = new ImageSaveOptions(SaveFormat.Jpeg);

saveOptions.Resolution = 96;

saveOptions.PageCount = 1;

saveOptions.PageIndex = 0;

MemoryStream stream = new MemoryStream();

wordDocument.Save(stream, saveOptions);

//// Resize jpg

Image imgOriginal = Image.FromStream(stream);

Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);

Image myThumbnail = imgOriginal.GetThumbnailImage(300, 400, myCallback, IntPtr.Zero);

// Exception occurs on the next line: "A generic error occurred in GDI+"

try

{

myThumbnail.Save(MyDir + "out.jpg", ImageFormat.Jpeg);

}

catch (Exception ex)

{

throw;

}


Yes, this works. Good idea to use streams here.

Thanks Tahir!

Hi Ingmar,


Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.