Hi,
I am trying to convert a PDF file into a tiff image, but instead of saving the tiff directly to the filesystem I want to “save” it to a memorystream. That memorystream (which should be the tiff image) I want to further process in memory.
However I can’t seem to make a new TiffImage from the memorystream. I keep getting the error "Image loading failed - {“Cannot open an image. The image file format may be not supported at the moment.”}"
Below is the code I use (which fails on the the Image.Load(ms) statement).
Dim pdfExample = "D:\Playground\Fileconvertor test\test.pdf"
Dim pdfDocument As New Document(pdfExample)
Dim tiffDevice As New TiffDevice(New Resolution(300), New TiffSettings() With {.Compression = CompressionType.CCITT4})
Dim memoryImage As TiffImage
Using ms As New MemoryStream
tiffDevice.Process(pdfDocument, ms)
memoryImage = DirectCast(Image.Load(ms), TiffImage)
End Using
Any ideas what I am missing?
Hi Dirk,
Thanks for using our products.
I have tested the scenario using Aspose.Pdf for .NET 8.5.0 where I have saved the PDF to TIFF conversion output in MemoryStream object and then have tried instantiating a Bitmap object from MemoryStream and I am able to access the saved image contents. I have used Bitmap object just to ensure that Steam object is not closed once output is saved using TiffDevice.Process(…) method.
[VB.NET]
Dim pdfExample = "c:\pdftest\WordWrap_Annotation.pdf"
Dim pdfDocument As New Document(pdfExample)
Dim tiffDevice As New TiffDevice(New Resolution(300), New TiffSettings() With {.Compression = CompressionType.CCITT4})
Dim memoryImage As System.Drawing.Bitmap
Using ms As New MemoryStream
tiffDevice.Process(pdfDocument, ms)
memoryImage = New Bitmap(ms) ' DirectCast(Image.Load(ms), )
Console.WriteLine(memoryImage.Size.Height & "== Width ==" & memoryImage.Size.Width)
End Using
Hi,
thank you for your response.
Although this works indeed it only solves part of the problem. I now have a bitmap however, I really need an aspose tiff image for further processing (adding extra frames, etc). I don’t understand why I am able to create a bitmap from the memory stream, but not an aspose image, as one of the parameters on the load is a stream.
D.
Hi Dirk,
Thanks for your patience and sorry for the delayed response.
In my earlier attempts, I could not resolve TiffImage and Image objects so I could not create a TiffImage where an additional frame can be added. Therefore, I created Bitmap object and it worked fine. Can you please share some further details (additional code snippet) which can help us in replicating this problem using TiffImage object. We apologize for this inconvenience.
I know this is a super old post, but I just ran into the same issue. After spending a couple hours on it, I realized if I reset the stream.Position to 0, I was able to load a TiffImage up.
var stream = ConvertToStream(file);
stream.Position = 0;
var tiffImage = (TiffImage)Image.Load(stream);
@arti4179
Thank you for sharing your findings and contributing towards community.
This will certainly be helpful for other community members and will save time for many of them.