Closed vlad12244 closed 3 years ago
Hi!
If you want to change the resolution of the PNG image produced by the SaveImage
method, you need to change the value of the second parameter:
// Default resolution
double zoom = 1;
document.SaveImage(1, zoom, PixelFormats.RGB, @"blankAI\0test.png", RasterOutputFileTypes.PNG);
// Higher resolution - better quality
zoom = 2;
document.SaveImage(1, zoom, PixelFormats.RGB, @"blankAI\0test.png", RasterOutputFileTypes.PNG);
// Lower resolution - worse quality
zoom = 0.5;
document.SaveImage(1, zoom, PixelFormats.RGB, @"blankAI\0test.png", RasterOutputFileTypes.PNG);
By the way, note that the first parameter (the page number) is zero-based, so in this way you are rendering the second page of the document.
The reason why you get the exception in the other example is that you are allocating a buffer with a different size than the actual rendered image. In particular, you are doing this:
RoundedRectangle roundedBounds = document.Pages[i].Bounds.Round(1);
renderedPageSizes[i] = new RoundedSize(roundedBounds.Width / 2, roundedBounds.Height / 2);
However, the fact that you are dividing by 2 the size of the roundedBounds
rectangle probably introduces a rounding error between the size calculated by your C# code and the one calculated internally by the MuPDF library. This is because MuPDF uses some weird rounding routines, which may add or subtract a pixel somewhat unexpectedly.
The "proper" way to do this is, again, to change the value of the zoom
parameter of the Round
method, e.g.:
RoundedRectangle roundedBounds = document.Pages[i].Bounds.Round(0.5);
renderedPageSizes[i] = new RoundedSize(roundedBounds.Width, roundedBounds.Height);
After you call this method, you don't really want to change the size of the rounded rectangle you get, otherwise you risk losing the alignment with the MuPDF library.
I hope this makes sense, but let me know if anything isn't clear!
Hello. It seems to work now. Thank you for your quick reply.
Great, happy to help!
Hello
I am trying this method, but how can I Increase or decrease the image quality?
I also tried some older demo when I changed width and height:(but it gets memory leaking errors )