9

I have a StackPanel which includes a few Rectangles that I want put to an image file (e.g. PNG). I'm developing this on Windows Phone 7 and most of the information I found on the internet wasn't applicable (I think) to WP7.

I think the System.Windows.Media.Imaging namespace is the key to this, but I'm not sure where to begin.

This is basically what I want to do:

StackPanel stack = new StackPanel();
List<Rectangle> recList = new List<Rectangle>();

add some rectangles to recList

foreach(var x in recList)
     stack.Children.Add(x);

then save the stackpanel to an image file...

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
judehall
  • 884
  • 12
  • 27

1 Answers1

4

You can use a WriteableBitmap to save the image.

WriteableBitmap wb = new WriteableBitmap(stack, null);
MemoryStream ms = new MemoryStream();

wb.SaveJpeg(ms, myWidth, myHeight, 0, 100);

You can change the MemoryStream to be an Isolated Storage stream instead. If you want to display the above MemoryStream in an Image control:

 BitmapImage bmp = new BitmapImage();
 bmp.SetSource(ms);
 image1.Source = bmp;

Or, saving to Isolated Storage:

using (var isoFileStream = new IsolatedStorageFileStream("myPicture.jpg", FileMode.OpenOrCreate, IsolatedStorageFile.GetUserStoreForApplication())) 
{                     
    wb.SaveJpeg(isoFileStream, myWidth, myHeight, 0, 100);                    
}
keyboardP
  • 68,824
  • 13
  • 156
  • 205