PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Unable to draw image using PDFSharp-GDI
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3470
Page 1 of 1

Author:  aravindan [ Mon Oct 10, 2016 8:30 pm ]
Post subject:  Unable to draw image using PDFSharp-GDI

In our application, we have been using PDFSharp 1.2.1269.0 build and everything works perfectly fine. Since the new PDFSharp 1.5 supports iref streams, we thought of using PDFSharp 1.5 (GDI) beta in our windows application. Unfortunately, with the new code, none of our images are getting printed. We have the below code to draw the images on the PDF, but it doesn't work.

Code:
var pdfGraphics = XGraphics.FromPdfPage(page);
MemoryStream memory = new MemoryStream();
bmp1.Save(memory, jpgEncoder, myEncoderParameters);
Image img = Image.FromStream(memory);
pdfGraphics.DrawImage((Bitmap)img, 0, 0);


It produces a blank PDF file, which when opened, gives a out of memory error. The same code is working perfectly fine withe PDFSharp 1.2.1269.0 build. Can you let me know if something needs to be changed in our code to make it work?

Let me know if you require additional details.

Regards,
Aravind

Author:  Thomas Hoevel [ Tue Oct 11, 2016 12:36 pm ]
Post subject:  Re: Unable to draw image using PDFSharp-GDI

Hi!

Maybe calling "XImage.FromStream(memory);" instead of "Image.FromStream(memory);" will already resolve the issue.

Code:
var img = XImage.FromStream(memory);
pdfGraphics.DrawImage(img, 0, 0);


aravindan wrote:
Let me know if you require additional details.
If I get a complete solution that allows me to replicate the issue in the debugger, I will investigate this issue.

See also:
viewtopic.php?p=2094#p2094

Author:  aravindan [ Tue Oct 11, 2016 2:17 pm ]
Post subject:  Re: Unable to draw image using PDFSharp-GDI

Thank you very much Thomas. That resolved the issue. I am able to print it successfully. Will get back to you, if I encounter any other issues.

Author:  aravindan [ Wed Oct 12, 2016 7:52 pm ]
Post subject:  Re: Unable to draw image using PDFSharp-GDI

Hi,

This time, the DrawString is printing the text upside down. This is the code we have (which has been working all along with the PDFSharp 1.2.1269.0 build. After upgrading to PDFSharp 1.5 GDI, it is printing upside down.

Code:
            var page = document.AddPage();
            var gfx = XGraphics.FromPdfPage(page, XPageDirection.Upwards);
            XFont font = new XFont("Courier New", 12);
            XRect newRect = new XRect(16.9422, 740.974, 243.3568, 15.639);
            XStringFormat newStringFormat = new XStringFormat();
            newStringFormat.Alignment = XStringAlignment.Near;
            newStringFormat.LineAlignment = XLineAlignment.Near;
           
            string fntName = font.Name;

            double fntSize = 10;
            font = new XFont(fntName, fntSize);

            gfx.DrawString("Test Text", font, XBrushes.Black, newRect, newStringFormat);
            XGraphicsState state = gfx.Save();
            gfx.Restore(state);
            gfx.Dispose();


Can you please let us know why it is not working correctly?

Author:  Thomas Hoevel [ Thu Oct 13, 2016 8:37 am ]
Post subject:  Re: Unable to draw image using PDFSharp-GDI

aravindan wrote:
Can you please let us know why it is not working correctly?
Most likely it is working correctly.

The default value is "XPageDirection.Downwards". Call XGraphics.FromPdfPage() with just one parameter and it will work with both versions of PDFsharp.

Author:  aravindan [ Thu Oct 13, 2016 2:20 pm ]
Post subject:  Re: Unable to draw image using PDFSharp-GDI

Thanks for your response Thomas. If I change the direction to Downwards, or simply leave it blank, the text is getting printed correctly, but it is getting printed from bottom to top. The first line appears at the bottom of the page and so on.

By the way, this worked perfectly fine with the earlier version of PDFSharp. Any suggestions?

Regards,
Aravind

Author:  Thomas Hoevel [ Thu Oct 13, 2016 2:30 pm ]
Post subject:  Re: Unable to draw image using PDFSharp-GDI

Most programs work with the old version and the new version without any problems.

Maybe there is something outside your code snippet that causes the problem.
(0, 0) always was the top-left corner IIRC. Something has changed "under the hood" but normally user code is not affected.

Author:  aravindan [ Thu Oct 13, 2016 4:07 pm ]
Post subject:  Re: Unable to draw image using PDFSharp-GDI

I am not too sure about that Thomas. When the XPageDirection is Upwards, it is printing from top to bottom, but each and every letter is upside down. But when I change the XPageDirection to Downwards, or remove it altogether, it is printing from bottom to top and each letter is proper. I don't quite get the fact that it could be because of some other factor.

Author:  () => true [ Fri Oct 14, 2016 9:18 am ]
Post subject:  Re: Unable to draw image using PDFSharp-GDI

Maybe we need a VS solution that allows us to replicate the issue.


We created the Issue Submission Template for that purpose.
Starting from the Hello World sample you can add code from your project until the problem occurs.

Maybe you can solve the problem yourself when you know which step causes the problem.
Or send us the complete solution and we can investigate the issue.

Author:  aravindan [ Thu Oct 27, 2016 9:35 pm ]
Post subject:  Re: Unable to draw image using PDFSharp-GDI

Thanks for your response. We were able to figure out the problem, as it was our code which was wrong. Now, we are trying to draw images into PDF, and here is the code we have. Can you please let me know what is wrong with this? After this, if we open the PDF, we are getting the error "There was an error while reading a stream.".

Code:
            Bitmap i =(Bitmap) Bitmap.FromFile(@"D:\Development\0.jpg", true);
            PdfDocument document = new PdfDocument();
            PdfPage newPage = document.AddPage();
            XRect imgRect = new XRect(0, 0.947815, 611.441, 791.052185);
            newPage.Orientation = PageOrientation.Portrait;
            XGraphics gfx;
            XColor back;
            XSolidBrush brush;
            gfx = XGraphics.FromPdfPage(newPage, XPageDirection.Downwards);
            back = XColors.White;
            back.A = 0.4;
            brush = new XSolidBrush(back);
            XImage img = XImage.FromGdiPlusImage(i);
            XFont font = new XFont("Arial", 12);
            gfx.DrawImage(img, imgRect.X, imgRect.Y, imgRect.Width, imgRect.Height);

Author:  TH-Soft [ Sat Oct 29, 2016 1:21 pm ]
Post subject:  Re: Unable to draw image using PDFSharp-GDI

Hi!

Same problem as your first post.
Avoid "XImage.FromGdiPlusImage" and use "XImage.FromFile" instead of "Image.FromFile".

"XImage.FromGdiPlusImage" worked fine with PDFsharp 1.32 and Windows XP, but somewhere after XP MS broke something.

Author:  aravindan [ Mon Oct 31, 2016 3:02 pm ]
Post subject:  Re: Unable to draw image using PDFSharp-GDI

Thank you very much Thomas. I changed it to FromStream instead of FromGDIPlusImage and it works fine.

Author:  hchurch [ Wed Mar 01, 2017 4:30 pm ]
Post subject:  Re: Unable to draw image using PDFSharp-GDI

Hi,

I am having the same issue as OP but in vb.net. Can you help?
Similar to OP, my code was working before I started using PdfSharp version 1.5 beta.
I am trying to reference a png logo image that is a project resource.

Code:
Dim logoimage As XImage = XImage.FromGdiPlusImage(My.Resources.logo)
gfx.DrawImage(logoimage, 60, 37, 40, 19)


I saw the comments about using image.fromstream(memory) instead but was unable to get the memory stream working.
Any help would be much appreciated.

Thanks,
Heath

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/