Printing problem in Silverlight 4.0 RC - loading images in code behind

Posted by Jacek Ciereszko on Geeks with Blogs See other posts from Geeks with Blogs or by Jacek Ciereszko
Published on Sat, 20 Mar 2010 22:06:09 GMT Indexed on 2010/03/20 23:11 UTC
Read the original article Hit count: 459

Filed under:

Few days ago I faced a problem with printing in new Silverlight 4 RC. When you try to dynamically load image (in code behind) and print it, it doesn't work. Paper sheet is blank.

Problem

XAML file:

<Image x:Name="image" Stretch="None" />

XAML.cs:

image.Source = new BitmapImage(new Uri(imageUri, UriKind.RelativeOrAbsolute)); 

Print:

var pd = new PrintDocument();
  pd.PrintPage += (s, args) =>
    {
      args.PageVisual = image;
    };
  pd.Print();

Result:

Blank paper.

 

Solution

What you need to do, is forced Silverlight engine to load that image before printing start. To accomplish that I proposed simply checking PixelWith value. Your code will ask about PixelWidth of image so it will have to be loaded.

XAML.cs:

BitmapImage bImage = new BitmapImage(new Uri(imageUri, UriKind.RelativeOrAbsolute));
image.Source = bImage;
InitControl(imageUri, movieUri, isLeft);

int w = bImage.PixelWidth;
int h = bImage.PixelHeight;

 

DONE!

 

Jacek Ciereszko

 

© Geeks with Blogs or respective owner