Is there a way to display an image in WPF stored in memory?

What I got is something like a screenshot application. (I managed to serialize, thank God !!!). When a button is pressed, a screenshot is taken by accessing the processing manipulation method. now the tricky part is that the class has another way to work with the above result in such a way that when you call the appropriate processing method, a window is created (shown), and the bitmap must go to the display container in that window. The problem is that so far I have noticed that in WPF, the image control source cannot be assigned to a variable that stores the image. How can I display the image stored in this variable without first saving it, get uri, etc.

+3
source share
2 answers

You need to create an image from a memory stream, this has been well documented by many people. Here are two links that can get you started:

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

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

+5
source

thanks for the slugster links. Here is how I did it:

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();

Basically, I combined what I found on these pages with some information that I found on how to pass BMP to memstream. I got this to work 100% :)

+2
source

Source: https://habr.com/ru/post/1734147/


All Articles