Tifferan
Tifferan

Reputation: 11

Convert a color array into a readable Image for WPF C#

I have the following issue: I want write a basic Program that renders a code created image. I have problems displaying the Image though. I save my Image in a three dimensional array like this: Image[x-Pos,y-Pos, RGB-value]. Now my question is how can i convert this into an object that can be displayed in a WPF-Image object on my GUI? Yours Tifferan

Upvotes: 0

Views: 847

Answers (1)

Clemens
Clemens

Reputation: 128076

Convert your data to a byte array that contains all pixels consecutively from the top left to the bottom right corner. Then create a BitmapSource from the data like this:

int width = ...
int height = ...
var format = PixelFormats.Rgb24;
var stride = (width * format.BitsPerPixel + 7) / 8;
var pixels = new byte[stride * height];

// copy data to pixels array
...

var bitmap = BitmapSource.Create(
    width, height, 96, 96, format, null, pixels, stride);

That said, it doesn't seem to make sense to store image data in a three-dimensional array. I could understand a two-dimensional array, where the dimensions would be the x and y coordinates of a pixel, and the array value would be the pixel (e.g. RGB) value.

Upvotes: 2

Related Questions