Skip to content

Commit 4856eec

Browse files
committed
Change the resolution of a rendered pdf to a max value.
1 parent 678dad6 commit 4856eec

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

ComicRack.Engine/IO/Provider/Readers/Pdf/PdfiumReaderEngine.cs

+21-7
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,8 @@ public byte[] ReadByteImage(string source, ProviderImageInfo info)
4141
{
4242
using (PdfPage pdfPage = pdfDocument.Pages[info.Index])
4343
{
44-
int dpiX = 300;
45-
int dpiY = 300;
46-
47-
int pageWidth = (int)(dpiX * pdfPage.Size.Width / 72);
48-
int pageHeight = (int)(dpiY * pdfPage.Size.Height / 72);
49-
50-
using (Bitmap bitmap = new Bitmap(pageWidth, pageHeight, PixelFormat.Format24bppRgb))
44+
Size size = CalculateSize(pdfPage.Width, pdfPage.Height);
45+
using (Bitmap bitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb))
5146
{
5247
pdfPage.Render(bitmap);
5348
return bitmap.ImageToBytes(ImageFormat.Jpeg);
@@ -61,6 +56,25 @@ public byte[] ReadByteImage(string source, ProviderImageInfo info)
6156
}
6257
}
6358

59+
private Size CalculateSize(double width, double height)
60+
{
61+
//The width & height are returned in point (1/72 inch)
62+
//but PDFs created by CR will have the wrong page size. Which would mean that opening this PDF would mean the resolution would balloon up.
63+
//To prevent from the above mentioned ballooning, this will be the MAX resolution
64+
int maxWidth = 1920; //8.5in at 225dpi
65+
int maxHeight = 2540; //11in at 225dpi
66+
67+
//Calculate the width based on the max height
68+
int targeWidth = (int)((width * maxHeight) / height);
69+
//Calculate the height based on the max width
70+
int targeHeight = (int)((height * maxWidth) / width);
71+
72+
//if the page is a landscape page (width > height), use the max height, if not we use the max width
73+
Size outSize = width > height ? new Size(targeWidth, maxHeight) : new Size(maxWidth, targeHeight);
74+
75+
return outSize;
76+
}
77+
6478
public ComicInfo ReadInfo(string source)
6579
{
6680
return null;

0 commit comments

Comments
 (0)