I have a structure containing an image in black and white:
public class Img
{
public int height;
public int width;
public byte[] matrix;
}
The values containing in matrix are 0 or 255.
What is the best way to display this image in a component using C# WPF?
I've try this :
XAML:
<Image Grid.Row="0"
Stretch="Uniform"
Source="{Binding Picture, Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"/>
C#:
public BitmapImage Picture
{
get
{
return _picture;
}
private set
{
_picture = value;
OnPropertyChanged("Picture");
}
}
public void Generate()
{
Img img = CreateImg();
Picture = LoadImage(img.width, img.height, img.matrix);
}
private BitmapImage LoadImage(int w, int h, byte[] imageData)
{
using (MemoryStream memory = new MemoryStream(imageData))
{
memory.Position = 0;
BitmapImage bitmapimage = new BitmapImage();
bitmapimage.BeginInit();
bitmapimage.StreamSource = memory;
bitmapimage.EndInit();
return bitmapimage;
}
}
But it doesn't work:
"Exception from HRESULT : 0x88982F50"