"I'm trying to insert a png image from my project resources into an Excel worksheet. When I pass the image to the Infragistics WorksheetImage object I run into a NullReferenceException."This is because the image probably hasn't been actually loaded yet. When you create a BitmapImage, the default behavior is to delay the creation/loading of the image so when the WorksheetImage tries to perform encoding operations on your bitmap it fails. To remedy this you need to change the way the bitmap is loaded by modifying the CreateOptions property.BitmapImage image = new BitmapImage(new Uri("Assets/myimage.png", UriKind.Relative));// Load it immediatelyimage.CreateOptions = BitmapCreateOptions.None;WorksheetImage sheetImage = new WorksheetImage(image, new Infragistics.Imaging.PngImageEncoder(), new List<Infragistics.Imaging.EncodingProperty>());
I am currently having this exact same problem. Does anyone have a solution to this ?
Regards,
Cliff.
Hello Cliff,
The solution for this NullReferenceException was to load the image immediately by changing the BitmapCreateOptions. Did that not resolve your issue? If not I'm going to need a little background on what your code is currently doing. More specifically how it is trying to use the image.
Hi Rob,
Thank you for replying back to my post.
I am changing the CreateOptions to BitmapCreateOptions.None without any success.
What I am trying to accomplish is have the company corporate logo at the top left corner of each worksheet. The image (a png file) is part of the Silverlight client resource.I am currently using version 13.1 (InfragisticsSL5.Documents.Excel.v13.1).Here is the latest code that I tried (giving me the NullReferenceException when executing the WorkSheetImage constructor) :
BitmapImage logo_img = new BitmapImage();
logo_img = new BitmapImage(new Uri("/SMM;component/Images/SMMLogo.png", UriKind.RelativeOrAbsolute));
logo_img.CreateOptions = BitmapCreateOptions.None;
WorksheetImage image = new WorksheetImage(logo_img, new PngImageEncoder(), new List<Infragistics.Imaging.EncodingProperty>());
Rect cellA1Bounds = workSheet.Rows[0].Cells[0].GetBoundsInTwips();
image.SetBoundsInTwips(workSheet, cellA1Bounds, PositioningOptions.None);
Thank you for all your help. Much appreciated.
Hi Cliff,
Your code doesn't look that much different than mine but I guess something happened between when I created this thread and now because even my original code isn't working. The reason for the exception is still the same though. The image just hasn't been fully created yet by the time the encoder gets to it, even with CreateOptions set to None. It requires a bit more time to fully load it. There are a couple of ways to deal with this.
1.) Store an instance of the image some time before you plan on using it. Maybe at application startup for example. Execute the code you have for creating the BitmapImage and set the CreateOptions to None as before and store this BitmapImage until you use it later to export to Excel.
2.) Keep the code you have now but instead of creating the WorksheetImage immediately after creating the BitmapImage, wait until the BitmapImage.ImageOpened event has fired. Once this event fires, it's safe to pass the image to the WorksheetImage constructor.
Let me know if you have any questions on this.
That did the trick. I created the BitmapImage in the constructor on the class, so by the time in gets to creating the worksheet and the Worksheetimage, the image if open/loaded.
Thanks for your help.
You are very welcome. I'm glad you got it working.