Skip to content

Commit

Permalink
Change the resolution of a rendered pdf to a max value.
Browse files Browse the repository at this point in the history
  • Loading branch information
maforget committed Mar 9, 2024
1 parent 678dad6 commit 4856eec
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions ComicRack.Engine/IO/Provider/Readers/Pdf/PdfiumReaderEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,8 @@ public byte[] ReadByteImage(string source, ProviderImageInfo info)
{
using (PdfPage pdfPage = pdfDocument.Pages[info.Index])
{
int dpiX = 300;
int dpiY = 300;

int pageWidth = (int)(dpiX * pdfPage.Size.Width / 72);
int pageHeight = (int)(dpiY * pdfPage.Size.Height / 72);

using (Bitmap bitmap = new Bitmap(pageWidth, pageHeight, PixelFormat.Format24bppRgb))
Size size = CalculateSize(pdfPage.Width, pdfPage.Height);
using (Bitmap bitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb))
{
pdfPage.Render(bitmap);
return bitmap.ImageToBytes(ImageFormat.Jpeg);
Expand All @@ -61,6 +56,25 @@ public byte[] ReadByteImage(string source, ProviderImageInfo info)
}
}

private Size CalculateSize(double width, double height)
{
//The width & height are returned in point (1/72 inch)
//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.
//To prevent from the above mentioned ballooning, this will be the MAX resolution
int maxWidth = 1920; //8.5in at 225dpi
int maxHeight = 2540; //11in at 225dpi

//Calculate the width based on the max height
int targeWidth = (int)((width * maxHeight) / height);
//Calculate the height based on the max width
int targeHeight = (int)((height * maxWidth) / width);

//if the page is a landscape page (width > height), use the max height, if not we use the max width
Size outSize = width > height ? new Size(targeWidth, maxHeight) : new Size(maxWidth, targeHeight);

return outSize;
}

public ComicInfo ReadInfo(string source)
{
return null;
Expand Down

0 comments on commit 4856eec

Please sign in to comment.