我有一个 StackPanel
,其中包含一些我想放入图像文件(例如 PNG)的 Rectangles
。我在 Windows Phone 7 上开发这个,我在互联网上找到的大部分信息不适用于(我认为)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);
然后将堆栈面板保存到图像文件...
最佳答案
您可以使用WriteableBitmap
来保存图像。
WriteableBitmap wb = new WriteableBitmap(stack, null);
MemoryStream ms = new MemoryStream();
wb.SaveJpeg(ms, myWidth, myHeight, 0, 100);
您可以将 MemoryStream
更改为独立存储流。如果要在 Image 控件中显示上面的 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);
}
关于c# - UIElement 到图像文件 (WP7),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6299978/