logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

New Topic Post Reply
Options
Go to last post Go to first unread
cyberchen  
#1 Posted : Saturday, December 29, 2018 4:38:28 PM(UTC)
Quote
cyberchen

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);
Paul Rayman  
#2 Posted : Sunday, December 30, 2018 8:41:47 PM(UTC)
Quote
Paul Rayman

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

cyberchen  
#3 Posted : Monday, December 31, 2018 8:15:53 AM(UTC)
Quote
cyberchen

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...

Paul Rayman  
#4 Posted : Monday, December 31, 2018 7:54:58 PM(UTC)
Quote
Paul Rayman

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

cyberchen  
#5 Posted : Tuesday, January 1, 2019 6:16:09 AM(UTC)
Quote
cyberchen

Rank: Newbie

Groups: Registered
Joined: 12/29/2018(UTC)
Posts: 4

Thanks: 1 times
Originally Posted by: Paul Rayman Go to Quoted Post
Try bitmap.Image.SetResolution before saving.

This is not supported by pdfBitmap :(
Paul Rayman  
#6 Posted : Tuesday, January 1, 2019 6:20:29 AM(UTC)
Quote
Paul Rayman

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(...)

thanks 1 user thanked Paul Rayman for this useful post.
cyberchen on 1/4/2019(UTC)
cyberchen  
#7 Posted : Wednesday, January 2, 2019 1:44:13 PM(UTC)
Quote
cyberchen

Rank: Newbie

Groups: Registered
Joined: 12/29/2018(UTC)
Posts: 4

Thanks: 1 times
Originally Posted by: Paul Rayman Go to Quoted Post
(bitmap.Image as Bitmap).SetResolution(...)


W0rks! Thank you :)
Quick Reply Show Quick Reply
Users browsing this topic
New Topic Post Reply
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.