有没有一种方法在WPF中显示存在内存中的图像?

4
我得到的是一个类似于截屏应用程序的东西(终于成功序列化了,谢天谢地!)。当点击按钮时,通过访问处理类的方法来获取屏幕截图。现在棘手的部分是该类还有另一种操作上述结果的方法,以这样的方式调用相应的处理方法时,会创建(显示)一个窗口,并且位图图像应该进入该窗口中的显示容器。问题是到目前为止,我注意到在WPF中,无法将图像控件的源属性赋值给存储图像的变量。如何显示存储在该变量中的图像,而不必先保存它,获取URI等呢?
2个回答

5

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%工作:)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接