Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
34510
WorksheetImage constructor failed with NullReferenceException
posted

"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 immediately
image.CreateOptions = BitmapCreateOptions.None;

WorksheetImage sheetImage = new WorksheetImage(image, new Infragistics.Imaging.PngImageEncoder(), new List<Infragistics.Imaging.EncodingProperty>());

Parents Reply Children
No Data