1

I have an application c# wpf and I use Image Bitmap.

I have a Byte [] who contains value (0 or 255) used to draw an image in gray scale.

Example :

int heigth = 5;
int width = 5;
Byte[] image = new Byte[]
    { 255,   0, 255,   0, 255,
        0, 255, 255, 255,   0,
      255, 255,   0, 255, 250,
        0, 255, 255, 255,   0,
      255,   0, 255,   0, 255};

Corresponds to the image

Image pattern

To display this image in my application I did that :

XAML :

<Border Style="{StaticResource BorderStyle}">
    <Image Source="{Binding BlockPicture, Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"  />
</Border>

C# :

private BitmapSource _blockPicture;
public BitmapSource BlockPicture
{
    get
    {
        return _blockPicture;
    }
    private set
    {
        _blockPicture = value;
        OnPropertyChanged("BlockPicture");
    }
}

private BitmapSource LoadImage(int w, int h, byte[] matrix)
{
    if (matrix == null || matrix.Length == 0)
        return null;

    var format = PixelFormats.Gray8;
    var stride = (w * format.BitsPerPixel + 7) / 8;
    return BitmapSource.Create(w, h, 96, 96, format, null, matrix, stride);
}

BlockPicture = LoadImage(width, heigth , image);

It's work fine but when the image is displayed, it's very ugly and diffuse.

Displayed image

What can I do to have a nice picture clean and sharp ?

0

1 Answer 1

4

You can specify the BitmapScalingMode:

<Image RenderOptions.BitmapScalingMode="NearestNeighbor" />
Sign up to request clarification or add additional context in comments.

1 Comment

That's exactly what I want. Thank you !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.