-
-
Notifications
You must be signed in to change notification settings - Fork 434
Closed
Milestone
Description
Magick.NET version
Magick.NET-Q8-AnyCPU 14.6.0
Environment (Operating system, version and so on)
Windows 11
Description
I use Syncfusion.DocIORenderer.NET to take the first page of a PDF or Word doc and create a BMP image, I then feed this image into Magick to create a JPG thumbnail of the image. This has been working for at least a couple of years using previous versions of both packages. Upon updating to Magick.NET-Q8-AnyCPU 14.6.0 I am getting an exception:
Message:
ImageMagick.MagickCoderErrorException : compression not supported `' @ error/bmp.c/ReadBMPImage/836
Stack Trace:
NativeInstance.CheckException(IntPtr exception, IntPtr result)
NativeMagickImage.ReadStream(IMagickSettings`1 settings, ReadWriteStreamDelegate reader, SeekStreamDelegate seeker, TellStreamDelegate teller, Void* data)
NativeMagickImage.ReadStream(IMagickSettings`1 settings, ReadWriteStreamDelegate reader, SeekStreamDelegate seeker, TellStreamDelegate teller)
MagickImage.Read(Stream stream, IMagickReadSettings`1 readSettings, Boolean ping)
MagickImage.Read(Stream stream, IMagickReadSettings`1 readSettings)
MagickImage.Read(Stream stream, MagickFormat format)
MagickImage.Read(Stream stream)
MagickImage.ctor(Stream stream)
ThumbNailServiceBase.CreateThumbnail(Stream inputFile, Double originalWidth, Double originalHeight) line 11
PdfThumbnailService.GenerateThumbNail(Stream pdfFile) line 45
CreateThumbnailFromPDFTests.GivenPDF_WhenIRunTheThumbnailFunction_ThenIExpectAJPGImageThumbnail() line 18
RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
Steps to Reproduce
using PdfLibCore;
using PdfLibCore.Enums;
public class PdfThumbnailService : ThumbNailServiceBase
{
public override Stream? GenerateThumbNail(Stream pdfFile)
{
using var pdfDocument = new PdfDocument(pdfFile);
using var pdfPage = pdfDocument.Pages[0];
Func<PdfPage, PdfiumBitmap> getBitMapInOrientation = page =>
{
if (pdfPage.Orientation == PageOrientations.Normal)
{
return new PdfiumBitmap((int)pdfPage.Size.Width, (int)pdfPage.Size.Height, true);
}
else
{
return new PdfiumBitmap((int)pdfPage.Size.Height, (int)pdfPage.Size.Width, true);
}
};
using var bitmap = getBitMapInOrientation(pdfPage);
pdfPage.Render(bitmap, pdfPage.Orientation, RenderingFlags.LcdText);
int dpiX = 96, dpiY = 96;
using var stream = bitmap.AsBmpStream(dpiX, dpiY);
int width, height;
if (pdfPage.Orientation == PageOrientations.Normal)
{
width = (int)pdfPage.Size.Width;
height = (int)pdfPage.Size.Height;
}
else
{
width = (int)pdfPage.Size.Height;
height = (int)pdfPage.Size.Width;
}
return CreateThumbnail(stream, width, height);
}
}
Base class
using ImageMagick;
public abstract class ThumbNailServiceBase : IThumbNailService
{
public abstract Stream? GenerateThumbNail(Stream inputFile);
protected static Stream? CreateThumbnail(Stream inputFile, double originalWidth, double originalHeight)
{
MagickImage image = new(inputFile);
return GenerateThumb(originalWidth, originalHeight, image);
}
private static Stream GenerateThumb(double originalWidth, double originalHeight, MagickImage image)
{
// Set the desired thumbnail size
uint width = 200;
var thumbnailSize = new MagickGeometry(width, (uint)(width * (originalHeight / originalWidth)));
// Generate the thumbnail
image.Thumbnail(thumbnailSize);
MemoryStream thumbnail = new();
image.Write(thumbnail, MagickFormat.Jpg);
return thumbnail;
}
protected static Stream? CreateThumbnailFromImage(Stream inputFile)
{
inputFile.Seek(0, SeekOrigin.Begin);
using MagickImage image = new(inputFile);
uint originalWidth = image.Width;
uint originalHeight = image.Height;
return GenerateThumb(originalWidth, originalHeight, image);
}
}
Integration Test
public class CreateThumbnailFromPDFTests
{
[Fact]
public void GivenPDF_WhenIRunTheThumbnailFunction_ThenIExpectAJPGImageThumbnail()
{
// Arrange
var filePath = @"ThumbnailTests/TestFiles/PDF/Being Responsible.pdf";
using var fs = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
var pdfThumbGenerator = new PdfThumbnailService();
// Action
// EXCEPTION THROWN IN THIS METHOD CALL
using var actual = pdfThumbGenerator.GenerateThumbNail(fs);
using (FileStream fileStream = File.Create(@"ThumbnailTests/TestFiles/PDF/Being Responsible.jpg"))
{
// Ensure the stream is at the beginning (important for copying the entire stream)
actual.Seek(0, SeekOrigin.Begin);
// Copy the stream to the file
actual.CopyTo(fileStream);
}
// Assert
Assert.NotNull(actual);
}
}
Images
Metadata
Metadata
Assignees
Labels
No labels