Frage

Currently I am working on a program that lets me handle data in form of an image. Let's say I have datapoints that correspond to an image (maybe 300 x 300) and the values range from 0 to 10000 and I would like to display the image in grey values. That would be all fine, if I just split the value range (0-10000) into 256 pieces and assigned them according grey values in bitmap form (I used rgb by setting all colours to be equal).

However, the user has to be able to change the contrast and the brightness. By that I mean: Let's say the value that corresponds to black is 3000 (and obviously all values below are also black) and the values that correspond to white are 5000+. The values in between should have a linear dependence on the grey colour. The user will have two sliders, one to change the range of observable values (in this case 2000) and one to shift the range lets say from 3000-5000 to 5000-7000. The problem is actually to make the sliders work in real time. I tried to simulate the image with one rectangle for each pixel but looping through all rectangles and changing their colours is simply too slow. Does anyone have any idea, how to make it work nice and smooth?

War es hilfreich?

Lösung

First, you need to convert your array of double values to an array of byte values. Then you can write this array of byte values to a WriteableBitmap. If you don't need to repeatedly update the contents of the WriteableBitmap, you should also freeze it to increase performance:

public static ImageSource GetImage(byte[] rawImageBytes, int width, int height)
{
    int channels = 1;
    int stride = width * channels;
    var image = new WriteableBitmap(width, height, 96, 96, PixelFormats.Gray8, null);
    image.WritePixels(new Int32Rect(0, 0, width, height), rawImageBytes, stride, 0);
    image.Freeze();
    return image;
}

If you want to update the content of the WriteableBitmap, you can just call WritePixels on the same instance without freezing the object instance.

To display the WriteableBitmap in your WPF application, just assign it to the Source property of an Image control.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top