bug found on XImage.DrawImage

Apr 17, 2011 at 11:16 PM

I downloaded the source today to save UIElements to Stream to PDF. 

First, I create a Ximage from PNG stream:

XImage ximg = XImage.FromStream(stream); Then when I call

XGraphics.DrawImage(ximg, kvp.Value);
Has this line:
var t = other.Elements.GetObject(key);
Which throws ArgumentNullException
So I changed it to:
var t = other.Elements.GetObject<PdfSharp.Pdf.PdfItem>(key);
And now it works.
Jun 4, 2011 at 10:55 AM
Edited Jun 4, 2011 at 2:40 PM

Hi TWebster,


I want to display a logo on my PDF using SilverPDF 1.2.1269 and XImage.FromStream is the only none obsolete method, so I cannot use anything else.

My code to use an image is as follows:

                        FileStream file = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\RawoLogo.jpg", FileMode.Open);
                        int BitsLength = (int)file.Length;
                        byte[] Bits = new byte[BitsLength];
                        file.Read(Bits, 0, BitsLength);
                        XImage image = XImage.FromStream(file);
                        gfx.DrawImage(image, 200, 0);

When it gets to the line XImage image = XImage.FromStream(file) an error is generated. The file would be in a incorrect format.

I get this error no matter which image or which type of image I use.


I assume you had the same error and was able to fix this. If you have fixes this problem and if you have a DLL at hand that doesn't have this problem, could you be so kind as to email me the DLL that doesn't have this problem??

I could find a pdfImage.Merge with the line var t = other.Elements.getObject(key);, but I couldn't insert <PdfSharp.Pdf.PdfItem>, this resulted in an error.


Looking in the samples that come with the code. A relative URL is used to load a picture, however I am using an absolute URL and that doesn't work:


string LogoFile = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\RawoLogo.jpg";

 XImage xImage = FromFile(LogoFile);  


gfx.DrawImage(xImage, 200, 0);


private XImage FromFile(string path)

var i = System.Windows.Application.GetResourceStream(new Uri(path, UriKind.RelativeOrAbsolute));


return XImage.FromStream(i.Stream);



This results in an error expected relative URI but found absolute URI (and when I (as a test) add a picture to the project and try that as a relative URI than it cannot find the picture, probably because it is LightSwitch)

So no matter what I try I cannot get a picture in my PDF.


Is there a roadmap as to when a newer version of SilverPDF will come out?


Kind regards,



Clemens Linders




Jun 5, 2011 at 11:36 AM

Stupid me!!

The correct code is:

string LogoFile = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\RawoLogo.jpg";

FileStream file = new FileStream(LogoFile, FileMode.Open);


int BitsLength = (int)file.Length;    


byte[] Bits = new byte[BitsLength];


XImage image = XImage.FromStream(file);


gfx.DrawImage(image, 200, 0);

All I did was remove the line: file.Read(Bits, 0, BitsLength);

By actually reading the file, the file-pointer gets moved to the end of the file and than when XImage wants to read from the stream there is nothing left to read. An error: passed end of file, or just plain null error would have helped me to discover this sooner.

I can only hope that my mistake and posts about it is of use to others.

Kind regards,

Clemens Linders


Feb 26, 2013 at 8:38 AM
TWebster, your solution doesn't compile anymore.
So i changed the line from
var t = other.Elements.GetObject(key);
var t = other.Elements[key];

And now it works.