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

Creating PDF from XImage not working in 1.50 Beta
https://forum.pdfsharp.net/viewtopic.php?f=3&t=3078
Page 1 of 1

Author:  johnzered [ Fri Mar 20, 2015 1:06 pm ]
Post subject:  Creating PDF from XImage not working in 1.50 Beta

Using this code I'm able to create a pdf using version 1.32

Code:
var doc = new PdfDocument();
var p = doc.AddPage();
p.Size = PageSize.A4;
using (XImage xImg = Image.FromFile(@"c:\temp\test.jpg"))
{
    using (XGraphics xGfx = XGraphics.FromPdfPage(p))
    {
        xGfx.DrawImage(xImg, new XRect(0.0, 197.875, 595, 446.25), new XRect(0, 0, p.Width, p.Height), XGraphicsUnit.Point);
    }
    p.Close();

}
doc.Save(@"c:\temp\" + Guid.NewGuid().ToString() + ".pdf");
doc.Close();


But in version 1.50 Beta it ends with the following exception:
Code:
Could not find file 'C:\temp\testproject\bin\Debug\edff2edc-82ca-4b1b-9e28-3dba72f52865'.

   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
   at System.IO.File.OpenRead(String path)
   at PdfSharp.Pdf.Advanced.PdfImage.InitializeJpeg()
   at PdfSharp.Pdf.Advanced.PdfImage..ctor(PdfDocument document, XImage image)
   at PdfSharp.Pdf.Advanced.PdfImageTable.GetImage(XImage image)
   at PdfSharp.Pdf.PdfPage.GetImageName(XImage image)
   at PdfSharp.Drawing.Pdf.XGraphicsPdfRenderer.GetImageName(XImage image)
   at PdfSharp.Drawing.Pdf.XGraphicsPdfRenderer.Realize(XImage image)
   at PdfSharp.Drawing.Pdf.XGraphicsPdfRenderer.DrawImage(XImage image, XRect destRect, XRect srcRect, XGraphicsUnit srcUnit)
   at PdfSharp.Drawing.XGraphics.DrawImage(XImage image, XRect destRect, XRect srcRect, XGraphicsUnit srcUnit)

Author:  () => true [ Sat Mar 21, 2015 9:02 pm ]
Post subject:  Re: Creating PDF from XImage not working in 1.50 Beta

johnzered wrote:
But in version 1.50 Beta it ends with the following exception[...]
WPF build? GDI+ build? Core build?
Does it happen with all JPEG images or just with some?

Author:  () => true [ Sun Mar 22, 2015 9:58 am ]
Post subject:  Re: Creating PDF from XImage not working in 1.50 Beta

Probably it will work using "XImage xImg = XImage.FromFile" instead of "XImage xImg = Image.FromFile".

Both versions should work, but the former will work better anyway as it gives PDFsharp more control.

Author:  johnzered [ Mon Mar 23, 2015 8:00 am ]
Post subject:  Re: Creating PDF from XImage not working in 1.50 Beta

() => true wrote:
johnzered wrote:
But in version 1.50 Beta it ends with the following exception[...]
WPF build? GDI+ build? Core build?
Does it happen with all JPEG images or just with some?

GDI+ build. The Core build can only get image from stream, I think? When I tested the core build using a stream it did work.

() => true wrote:
Probably it will work using "XImage xImg = XImage.FromFile" instead of "XImage xImg = Image.FromFile".

Both versions should work, but the former will work better anyway as it gives PDFsharp more control.

Yes using XImage.FromFile works. But using the same image and Image.FromFile worked fine in 1.32 so something has changed. Just wanted to point that out.

I've included an image giving me problems but I do not think it's image specific.

Attachments:
test2.jpg
test2.jpg [ 21.32 KiB | Viewed 11401 times ]

Author:  () => true [ Thu Mar 26, 2015 9:45 pm ]
Post subject:  Re: Creating PDF from XImage not working in 1.50 Beta

johnzered wrote:
But using the same image and Image.FromFile worked fine in 1.32 so something has changed. Just wanted to point that out.
The image handling routines in PDFsharp worked fine under Windows XP, but some JPEG files caused problems under Windows 7 and later. Thanks, M$!

Thank you, John, for your feedback. I was able to replicate the problem - and I hope I fixed it. Will forward my fix to the PDFsharp team.
Image.FromFile will work with the next (beta) version of PDFsharp 1.50 (don't ask me for an ETA), but XImage.FromFile is the recommended way.

Author:  johnzered [ Fri Oct 02, 2015 10:39 am ]
Post subject:  Re: Creating PDF from XImage not working in 1.50 Beta

() => true wrote:
johnzered wrote:
But using the same image and Image.FromFile worked fine in 1.32 so something has changed. Just wanted to point that out.
The image handling routines in PDFsharp worked fine under Windows XP, but some JPEG files caused problems under Windows 7 and later. Thanks, M$!

Thank you, John, for your feedback. I was able to replicate the problem - and I hope I fixed it. Will forward my fix to the PDFsharp team.
Image.FromFile will work with the next (beta) version of PDFsharp 1.50 (don't ask me for an ETA), but XImage.FromFile is the recommended way.


Thank you, it seems to be working now with beta 2. I do still have som issues using these two methods:
XImage xImg1 = XImage.FromGdiPlusImage(myImage)
XImage xImg2 = Image.FromStream(myMemoryStream)

I only have one problematic image and I'm not sure if I can share it as it is a private document/image.

As you said you have better control when using XImage.FromStream etc. why not just remove the possibilty to create an XImage instance from an Image?

Author:  Thomas Hoevel [ Mon Oct 05, 2015 10:04 am ]
Post subject:  Re: Creating PDF from XImage not working in 1.50 Beta

johnzered wrote:
As you said you have better control when using XImage.FromStream etc. why not just remove the possibilty to create an XImage instance from an Image?
AFAIK it works for all images under XP and for the vast majority of images under later Windows versions.

If we no longer support Image, people who just have an Image will have to save that Image to a stream - and they will get the same problem: works under XP, works for most images under later OS versions.

Maybe we'll mark those functions as deprecated, pointing out the risk that comes with them.

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