We don’t have printing feature in SL 3 (SL 4 has that feature though
) so here’s the workaround solution – create a bitmap file of that UIElement then Save that file to user local machine.
Code get from here http://forums.silverlight.net/forums/p/114691/446894.aspx
public static void GenerateImage(UIElement toPrint,double ActualHeight, double ActualWidth )
{
WriteableBitmap bitmap = new System.Windows.Media.Imaging.WriteableBitmap(Convert.ToInt32(ActualWidth),
Convert.ToInt32(ActualHeight));
bitmap.Render(toPrint, new TranslateTransform());
bitmap.Invalidate();
SaveFile(bitmap);
}
#region Save file
private static void SaveFile(WriteableBitmap wb)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.DefaultExt = "png";
saveFileDialog.Filter = "PNG Files (*.png)|*.png|All files (*.*)|*.*";
saveFileDialog.FilterIndex = 1;
if (saveFileDialog.ShowDialog() == true)
{
using (Stream stream = saveFileDialog.OpenFile())
{
byte[] buffer = GetBuffer(wb);
stream.Write(buffer, 0, buffer.Length);
stream.Close();
}
}
}
private static byte[] GetBuffer(WriteableBitmap bitmap)
{
int width = bitmap.PixelWidth;
int height = bitmap.PixelHeight;
MemoryStream ms = new MemoryStream();
#region BMP File Header(14 bytes)
//the magic number(2 bytes):BM
ms.WriteByte(0x42);
ms.WriteByte(0x4D);
//the size of the BMP file in bytes(4 bytes)
long len = bitmap.Pixels.Length * 4 + 0x36;
ms.WriteByte((byte)len);
ms.WriteByte((byte)(len >> 8));
ms.WriteByte((byte)(len >> 16));
ms.WriteByte((byte)(len >> 24));
//reserved(2 bytes)
ms.WriteByte(0x00);
ms.WriteByte(0x00);
//reserved(2 bytes)
ms.WriteByte(0x00);
ms.WriteByte(0x00);
//the offset(4 bytes)
ms.WriteByte(0x36);
ms.WriteByte(0x00);
ms.WriteByte(0x00);
ms.WriteByte(0x00);
#endregion
#region Bitmap Information(40 bytes:Windows V3)
//the size of this header(4 bytes)
ms.WriteByte(0x28);
ms.WriteByte(0x00);
ms.WriteByte(0x00);
ms.WriteByte(0x00);
//the bitmap width in pixels(4 bytes)
ms.WriteByte((byte)width);
ms.WriteByte((byte)(width >> 8));
ms.WriteByte((byte)(width >> 16));
ms.WriteByte((byte)(width >> 24));
//the bitmap height in pixels(4 bytes)
ms.WriteByte((byte)height);
ms.WriteByte((byte)(height >> 8));
ms.WriteByte((byte)(height >> 16));
ms.WriteByte((byte)(height >> 24));
//the number of color planes(2 bytes)
ms.WriteByte(0x01);
ms.WriteByte(0x00);
//the number of bits per pixel(2 bytes)
ms.WriteByte(0x20);
ms.WriteByte(0x00);
//the compression method(4 bytes)
ms.WriteByte(0x00);
ms.WriteByte(0x00);
ms.WriteByte(0x00);
ms.WriteByte(0x00);
//the image size(4 bytes)
ms.WriteByte(0x00);
ms.WriteByte(0x00);
ms.WriteByte(0x00);
ms.WriteByte(0x00);
//the horizontal resolution of the image(4 bytes)
ms.WriteByte(0x00);
ms.WriteByte(0x00);
ms.WriteByte(0x00);
ms.WriteByte(0x00);
//the vertical resolution of the image(4 bytes)
ms.WriteByte(0x00);
ms.WriteByte(0x00);
ms.WriteByte(0x00);
ms.WriteByte(0x00);
//the number of colors in the color palette(4 bytes)
ms.WriteByte(0x00);
ms.WriteByte(0x00);
ms.WriteByte(0x00);
ms.WriteByte(0x00);
//the number of important colors(4 bytes)
ms.WriteByte(0x00);
ms.WriteByte(0x00);
ms.WriteByte(0x00);
ms.WriteByte(0x00);
#endregion
#region Bitmap data
for (int y = height - 1; y >= 0; y--)
{
for (int x = 0; x < width; x++)
{
int pixel = bitmap.Pixels[width * y + x];
ms.WriteByte((byte)(pixel & 0xff)); //B
ms.WriteByte((byte)((pixel >> 8) & 0xff)); //G
ms.WriteByte((byte)((pixel >> 0x10) & 0xff)); //R
ms.WriteByte(0x00); //reserved
}
}
#endregion
return ms.GetBuffer();
}
#endregion
