UIElement到图像文件(WP7)

本文关键字:WP7 文件 图像 UIElement | 更新日期: 2023-09-27 17:59:00

我有一个StackPanel,其中包括一些Rectangles,我想将其放入图像文件(例如PNG)中。我正在WindowsPhone7上开发这个,我在互联网上发现的大多数信息(我认为)不适用于WP7。

我认为System.Windows.Media.Imaging名称空间是关键,但我不确定从哪里开始。

这基本上就是我想做的:

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

recList 中添加一些矩形

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

然后将stackpanel保存到图像文件中。。。

UIElement到图像文件(WP7)

您可以使用WriteableBitmap来保存图像。

WriteableBitmap wb = new WriteableBitmap(stack, null);
MemoryStream ms = new MemoryStream();
wb.SaveJpeg(ms, myWidth, myHeight, 0, 100);

您可以将MemoryStream更改为独立存储流。如果您想在图像控件中显示上述MemoryStream

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

或者,保存到独立存储:

using (var isoFileStream = new IsolatedStorageFileStream("myPicture.jpg", FileMode.OpenOrCreate, IsolatedStorageFile.GetUserStoreForApplication())) 
{                     
    wb.SaveJpeg(isoFileStream, myWidth, myHeight, 0, 100);                    
}