Patagames Software Support Forum
»
Pdfium.Net SDK
»
Common Questions
»
How can i set the Rendering Resolution (DPI) when Render to Image
Rank: Newbie
Groups: Registered
Joined: 12/29/2018(UTC) Posts: 4
Thanks: 1 times
|
In other Libs its possible to tell the Render Method what DPI Res. shoud be used. How can i do it here? I whant to Render to a Tiff via a bitmap, i think bitmap.SetResolution(dpi, dpi); alone will not do the job :( Here is my Rendering code: Code:
int targetWidth = (int)(page.Width / 72 * dpi );
int targetHeight = (int)(page.Height / 72 * dpi );
using (var bitmap = new Bitmap(targetWidth, targetHeight))
{
bitmap.SetResolution(dpi, dpi);
using (var g = Graphics.FromImage(bitmap))
{
g.FillRectangle(new SolidBrush(Color.White), 0, 0, targetWidth, targetHeight);
using (var pdfBitmap = new PdfBitmap(targetWidth, targetHeight, true))
{
page.Render(pdfBitmap, 0, 0, targetWidth, targetHeight, PageRotate.Normal, RenderFlags.FPDF_ANNOT);
g.DrawImageUnscaled(pdfBitmap.Image, 0, 0);
}
}
bitmap.Save(temppath + filename, System.Drawing.Imaging.ImageFormat.Tiff);
|
|
|
|
Rank: Administration
Groups: Administrators
Joined: 1/5/2016(UTC) Posts: 1,115
Thanks: 8 times Was thanked: 130 time(s) in 127 post(s)
|
The DPI (the dots per inch) does not work that way. When we talking about the raster images, the DPI does not make any sense without a target device. Inside the PDF all coordinates are given in points. One point has a fixed size - 1/72 of an inch. Rendering is always in pixels. Pixels have a certain size which depends on the device (monitor for example). Typically it is 1/96 of an inch. Therefore, the calculation of the image size in pixels in your code is made according to the formula Code:int targetWidth = (int)(page.Width / 72 * dpi );
int targetHeight = (int)(page.Height / 72 * dpi );
For example, if dpi is equal to monitor DPI (96), then you receive the true size of the document on this monitor. The bitmap.SetResolution simply writes the DPI values (horizontal and vertical) inside the image metadata. Due to this, it is possible to find out the original size of the document in inches by dividing pixels to this DPI. That is all when it comes to DPI and raster images. Edited by user Sunday, December 30, 2018 9:53:29 PM(UTC)
| Reason: Not specified
|
|
|
|
Rank: Newbie
Groups: Registered
Joined: 12/29/2018(UTC) Posts: 4
Thanks: 1 times
|
Ok, then the right calculation would be: Code:
int dpi = 300; // DPI
int targetWidth = (int)(page.Width / 72 * dpi);
int targetHeight = (int)(page.Height / 72 * dpi);
but the rendered Tiff is still 96dpi and the Filesize is far to small. ... can you maybe provide alternative code snipped to render something real? For example: I use this Code to Render a A3 at 300DPI. Code:
int dpi = 300;
int targetWidth = (int)(page.Width / 72 * dpi);
int targetHeight = (int)(page.Height / 72 * dpi);
using (var bitmap = new PdfBitmap(targetWidth, targetHeight, true))
{
//Fill background
bitmap.FillRect(0, 0, targetWidth, targetHeight, Color.White);
//Render contents in a page to a drawing surface specified by a coordinate pair, a width, and a height.
page.Render(bitmap, 0, 0, targetWidth, targetHeight,
Patagames.Pdf.Enums.PageRotate.Normal,
Patagames.Pdf.Enums.RenderFlags.FPDF_ANNOT);
bitmap.Image.Save(temppath + filename, System.Drawing.Imaging.ImageFormat.Tiff);
}
What i get is a TIFF with 3508 x 4961 Pixels (A3 @ 300dpi, thats good..). But the Filesize is only 1.2 Mb. But it should be around 50Mb. This is stsrting to driving me crazy.... Edited by user Monday, December 31, 2018 3:16:34 PM(UTC)
| Reason: this drives me crazy...
|
|
|
|
Rank: Administration
Groups: Administrators
Joined: 1/5/2016(UTC) Posts: 1,115
Thanks: 8 times Was thanked: 130 time(s) in 127 post(s)
|
Try bitmap.Image.SetResolution before saving. Edited by user Monday, December 31, 2018 7:56:08 PM(UTC)
| Reason: Not specified
|
|
|
|
Rank: Newbie
Groups: Registered
Joined: 12/29/2018(UTC) Posts: 4
Thanks: 1 times
|
Originally Posted by: Paul Rayman  Try bitmap.Image.SetResolution before saving. This is not supported by pdfBitmap :(
|
|
|
|
Rank: Administration
Groups: Administrators
Joined: 1/5/2016(UTC) Posts: 1,115
Thanks: 8 times Was thanked: 130 time(s) in 127 post(s)
|
Code:
(bitmap.Image as Bitmap).SetResolution(...)
|
 1 user thanked Paul Rayman for this useful post.
|
|
|
Rank: Newbie
Groups: Registered
Joined: 12/29/2018(UTC) Posts: 4
Thanks: 1 times
|
Originally Posted by: Paul Rayman  (bitmap.Image as Bitmap).SetResolution(...) W0rks! Thank you :)
|
|
|
|
Patagames Software Support Forum
»
Pdfium.Net SDK
»
Common Questions
»
How can i set the Rendering Resolution (DPI) when Render to Image
Forum Jump
You can post new topics in this forum.
You can reply to topics in this forum.
You can delete your posts in this forum.
You can edit your posts in this forum.
You cannot create polls in this forum.
You can vote in polls in this forum.
Important Information:
The Patagames Software Support Forum uses cookies. By continuing to browse this site, you are agreeing to our use of cookies.
More Details
Close