有没有办法在存储在内存中的WPF中显示图像?

时间:2010-02-23 21:39:59

标签: wpf image controls bitmap operations

我得到的就是截图制作应用程序。 (设法序列化,感谢上帝!!!)当点击一个按钮时,通过访问处理classe的方法获取屏幕截图。现在棘手的部分是该类有另一种方法用于操作上述结果,以这种方式比调用相应的处理方法时,创建一个窗口(显示)并且位图图像应该进入显示容器中那个窗口。问题是到目前为止,我注意到在WPF中,图像控件的源不能归因于存储图像的变量。如何显示存储在该变量中的图像,而无需先保存,获取uri等。 ?

2 个答案:

答案 0 :(得分:5)

你需要从内存流中创建一个图像,许多人已经很好地记录了这一点。以下是两个可以帮助您入门的链接:

http://forums.silverlight.net/forums/p/44637/166282.aspx

http://www.wpftutorial.net/Images.html

答案 1 :(得分:2)

感谢链接slugster。我是这样做的:

MemoryStream ms = new MemoryStream();
sBmp = gBmp; //note: gBmp is a variable that stores the captured image and passes it on to the method that uses sBMP as a distribuitor of the variable holding the captured image data
//variable that holds image
sBmp.Save(ms,ImageFormat.Bmp);
//my buffer byte
byte[] buffer = ms.GetBuffer();
//Create new MemoryStream that has the contents of buffer
MemoryStream bufferPasser = new MemoryStream(buffer);
//Creates a window with parents classthatholdsthismethod and null
Edit childEdit = new Edit(this, null);
childEdit.Show();
//I create a new BitmapImage to work with
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = bufferPasser;
bitmap.EndInit();
//I set the source of the image control type as the new BitmapImage created earlier.
childEdit.imgImageCanvas.Source = bitmap;
childEdit.Activate();

我基本上将我在这些页面上找到的内容与我发现的关于如何将bmp传递给memstream的一些信息相结合。我让它100%工作:)