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

Notification

Icon
Error

Options
Go to last post Go to first unread
Guest  
#1 Posted : Saturday, August 19, 2017 9:20:09 AM(UTC)
Guest

Rank: Guest

Groups: Guests
Joined: 1/5/2016(UTC)
Posts: 162

Was thanked: 5 time(s) in 5 post(s)
So there is an issue after i convert PDF file to a TIF file.

When i try to save the pdf to a high dpi tiff, small white lines get added to the rendered text.

Above is the source PDF, below is the tiff result:

https://www.dropbox.com/...issuedifference.png?dl=0


here is some of the code i use for the conversion. (dpi is 300)
Code:

                        var bitmap = new Bitmap(pixelsWidth, pixelsHeight);
                        bitmap.SetResolution(dpi, dpi);
                        using (var graphics = Graphics.FromImage(bitmap))
                        {
                            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                            graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                            graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                            graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
                            var rect = new Rectangle(0, 0, pixelsWidth, pixelsHeight);
                            graphics.FillRectangle(Brushes.White, rect);
                            page.Render(graphics, 0, 0, pixelsWidth, pixelsHeight,
                                Patagames.Pdf.Enums.PageRotate.Normal,
                                Patagames.Pdf.Enums.RenderFlags.FPDF_NO_CATCH | Patagames.Pdf.Enums.RenderFlags.FPDF_LCD_TEXT);
                            //check and create output folders
                            if (!Directory.Exists(jobFolder)) Directory.CreateDirectory(jobFolder);

                            string filePart = (i+1).ToString("D3");
                            var path = (sheetType == SheetType.Tiff) ? Path.Combine(jobFolder, $@"{filePart}.tif") : Path.Combine(jobFolder, $@"{i + 1}.jpg");
                            string mimeType = sheetType == SheetType.Tiff ? "image/tiff" : "image/jpeg";
                            EncoderValue encoder = sheetType == SheetType.Tiff ? EncoderValue.CompressionCCITT4 : EncoderValue.CompressionNone;

                            ImageCodecInfo myImageCodecInfo = null;
                            ImageCodecInfo[] encoders;
                            encoders = ImageCodecInfo.GetImageEncoders();
                            for (int j = 0; j < encoders.Length; ++j)
                            {
                                if (encoders[j].MimeType == mimeType)
                                    myImageCodecInfo = encoders[j];
                            }
                            System.Drawing.Imaging.Encoder myEncoder;
                            myEncoder = System.Drawing.Imaging.Encoder.Compression;
                            EncoderParameters myEncoderParameters;
                            myEncoderParameters = new EncoderParameters(1);
                            EncoderParameter myEncoderParameter;
                            myEncoderParameter = new EncoderParameter(myEncoder, (long)encoder);
                            myEncoderParameters.Param[0] = myEncoderParameter;
                            bitmap.Save(path, myImageCodecInfo, myEncoderParameters);
Paul Rayman  
#2 Posted : Monday, August 21, 2017 6:41:50 AM(UTC)
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 1,138

Thanks: 10 times
Was thanked: 133 time(s) in 130 post(s)
Is this issue reproduced when you saving rendered image as png or jpg (without any ImageCodecInfo)?
Could you please provide the source pdf?

Edited by user Monday, August 21, 2017 6:42:32 AM(UTC)  | Reason: Not specified

Guest  
#3 Posted : Monday, August 21, 2017 7:52:39 AM(UTC)
Guest

Rank: Guest

Groups: Guests
Joined: 1/5/2016(UTC)
Posts: 162

Was thanked: 5 time(s) in 5 post(s)
Originally Posted by: Paul Rayman Go to Quoted Post
Is this issue reproduced when you saving rendered image as png or jpg (without any ImageCodecInfo)?
Could you please provide the source pdf?


Yes, it happens when you save as jpg.

you don't need to save the image to make it happen. if you get the bitmap and put it in an PictureBox, same problem occurs.

Here is the source pdf:

https://www.dropbox.com/...0B5504ad1_plans.pdf?dl=0

Paul Rayman  
#4 Posted : Monday, August 21, 2017 8:45:54 AM(UTC)
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 1,138

Thanks: 10 times
Was thanked: 133 time(s) in 130 post(s)
Your code can't be compiled, so I have tested your PDF document with code below.

There is no any problems with rendering.
Anyway, the following code shows the recommended way to rendering PDF.

Code:

float dpi = 300;
using (var doc = PdfDocument.Load(@"d:\9\01-0B5504ad1_plans.pdf"))
{
	int i = 0;
	foreach(var page in doc.Pages)
	{
		int w = (int)(page.Width * dpi / 72);
		int h = (int)(page.Height * dpi / 72);
		using (var bitmap = new PdfBitmap(w, h, true))
		{
			bitmap.FillRect(0, 0, w, h, Color.White);
			page.Render(bitmap, 0, 0, w, h, PageRotate.Normal, RenderFlags.FPDF_ANNOT);
			bitmap.Image.Save(string.Format(@"d:\9\output\01-0B5504ad1_plans_{0}.png", ++i), ImageFormat.Png);
		}
		page.Dispose();
	}
}

Edited by user Monday, August 21, 2017 8:50:22 AM(UTC)  | Reason: Not specified

Guest  
#5 Posted : Monday, August 21, 2017 9:17:32 AM(UTC)
Guest

Rank: Guest

Groups: Guests
Joined: 1/5/2016(UTC)
Posts: 162

Was thanked: 5 time(s) in 5 post(s)
the output file doesn't seem to have a 300dpi resolution.



when i try to put any kind of compression it crashes. here is an example for TIFF CCITT4.
Note: There is no dispose, because i dispose it manually later.

Code:

        private int DPI = 300;
        public Image GetPdfImage(string tempFilename, int pageNumber = 0)
        {
            PdfCommon.Initialize(PDFIUMKEY);
            FileInfo fileInfo = new FileInfo(tempFilename);
            Image image = null;
            using (var doc = PdfDocument.Load(fileInfo.FullName))
            {
                //Enumerate all pages sequentially in a given document
                TotalPages = doc.Pages.Count;
                if (doc.Pages.Count > 0)
                {
                    var page = doc.Pages[pageNumber];
                    //Gets page width and height measured in points. One point is 1/72 inch (around 0.3528 mm)
                    int pixelsWidth = (int)(page.Width * DPI / 72);
                    int pixelsHeight = (int)(page.Height * DPI / 72);



                    var bitmap = new PdfBitmap(pixelsWidth, pixelsHeight, true);
                    bitmap.FillRect(0, 0, pixelsWidth, pixelsHeight, Color.White);
                    page.Render(bitmap, 0, 0, pixelsWidth, pixelsHeight,
                            Patagames.Pdf.Enums.PageRotate.Normal,
                            Patagames.Pdf.Enums.RenderFlags.FPDF_ANNOT);


                    ImageCodecInfo myImageCodecInfo = null;
                    ImageCodecInfo[] encoders;
                    encoders = ImageCodecInfo.GetImageEncoders();
                    for (int j = 0; j < encoders.Length; ++j)
                    {
                        if (encoders[j].MimeType == "image/tiff")
                            myImageCodecInfo = encoders[j];
                    }
                    System.Drawing.Imaging.Encoder myEncoder;
                    myEncoder = System.Drawing.Imaging.Encoder.Quality;
                    EncoderParameters myEncoderParameters;
                    myEncoderParameters = new EncoderParameters(1);
                    EncoderParameter myEncoderParameter;
                    myEncoderParameter = new EncoderParameter(myEncoder, (long)EncoderValue.CompressionCCITT4);
                    myEncoderParameters.Param[0] = myEncoderParameter;

                    Image.Save("output.tiff", myImageCodecInfo, myEncoderParameters);

                    //This.Image = bitmap.Image;  <-- i dispose it manually someplace else
                    page.Dispose();
                }
            }
            PdfCommon.Release();
            return image;
        }


btw, thanks a lot for the quick responses.

Edited by user Monday, August 21, 2017 9:18:40 AM(UTC)  | Reason: forgot to add my thanks

Paul Rayman  
#6 Posted : Monday, August 21, 2017 7:52:57 PM(UTC)
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 1,138

Thanks: 10 times
Was thanked: 133 time(s) in 130 post(s)
Where exactly does it crash, and with what exception?
Could you please provide stack trace.
Users browsing this topic
Guest (4)
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.