到的UIElement图像文件(WP7) [英] UIElement to image file (WP7)
问题描述
我有一个的StackPanel
,其中包括几个长方形
,我想提出一个图像文件(如PNG) 。我在Windows Phone 7开发此,大部分我在互联网上找到的信息是不适用的(我认为)到WP7。
我觉得 System.Windows.Media.Imaging
命名空间是关键,这一点,但我不知道从哪里开始。
这基本上就是我想要做的:
的StackPanel堆栈=新的StackPanel();
名单,LT;矩形> recList =新的List<矩形>();
添加一些矩形到 recList
的foreach(在recList变种X)
stack.Children.Add(X);
然后将保存的StackPanel为图像文件...
您可以使用 WriteableBitmap的
来保存图像。
WriteableBitmap的WB =新的WriteableBitmap的(堆栈,NULL);
的MemoryStream毫秒=新的MemoryStream();
wb.SaveJpeg(MS,myWidth,myHeight,0,100);
您可以修改的MemoryStream
是一个独立存储流来代替。如果你想在一个图像控件来显示上述的MemoryStream
:
BitmapImage的BMP =新的BitmapImage();
bmp.SetSource(毫秒);
image1.Source = BMP;
或者保存到独立存储:
<$使用
点$ p>(VAR isoFileStream =新IsolatedStorageFileStream(myPicture.jpg,FileMode.OpenOrCreate,IsolatedStorageFile.GetUserStoreForApplication()))
{
wb.SaveJpeg(isoFileStream, myWidth,myHeight,0,100);
}
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...
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);
}
这篇关于到的UIElement图像文件(WP7)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!