18

I have enter image description here

I need enter image description here

XAML:

<Image Height="500"
       MouseLeftButtonDown="image_MouseLeftButtonDown"
       MouseRightButtonDown="image_MouseRightButtonDown"
       Name="image"
       Stretch="Fill"
       Width="500" />`

C#:

  wbmap = new WriteableBitmap(50, 50, 500, 500, PixelFormats.Indexed8, palette);
  wbmap.WritePixels(new Int32Rect(0, 0, wbmap.PixelWidth, wbmap.PixelHeight), pixels, wbmap.PixelWidth * wbmap.Format.BitsPerPixel / 8, 0);
  image.Source = wbmap;
Fredrik Hedblad
  • 83,499
  • 23
  • 264
  • 266
Eugene Gluhotorenko
  • 3,094
  • 2
  • 33
  • 52
  • 1
    Please give more details!. How did you get that Blur in the first image? Was that generated in XAML? – Jobi Joy Feb 01 '11 at 19:43

3 Answers3

30

As tkerwin mentioned, change the BitmapScalingMode to NearestNeighbor in you XAML Image code:

RenderOptions.BitmapScalingMode="NearestNeighbor"
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
  • 1
    I tried to use the code above. It works fine! But I can not find in MSDN where Image.RenderOptions is described, there is no info on the page http://msdn.microsoft.com/en-us/library/system.windows.controls.image.aspx . Where the doc can be found? Thanks – sergtk Aug 07 '12 at 18:13
  • 2
    @sergdev - The `RenderOptions` class can be found [here](http://msdn.microsoft.com/en-us/library/ms635510) – SwDevMan81 Aug 07 '12 at 18:23
  • 1
    I see, I saw that page, thanks. I try to clarify: where is relation between Image and RenderOptions class described? I thought that RenderOptions is a member of Image, but this is not the case. Just can not realize how to undestand this from MSDN. – sergtk Aug 07 '12 at 18:34
  • 1
    @sergdev - There isnt a relationship per se, the `RenderOptions` is a static class (all by itself). – SwDevMan81 Aug 07 '12 at 19:08
  • @sergdev it looks ".BitmapScalingMode" is a DependencyProperty -- so it can be assigned to any DependencyObject. (You could assign it to all sorts of things that don't make any sense -- like Rectangles, for example. -- for the objects that don't recognize the property, it just gets ignored.) – BrainSlugs83 Dec 18 '13 at 22:08
  • Now it has to be RenderOptions.SetBitmapScalingMode(this,BitmapScalingMode.NearestNeighbor); – Gamestarplayer41 Nov 13 '20 at 12:02
19

Perhaps you need to change the bitmap scaling mode to nearest neighbor.

Add RenderOptions.BitmapScalingMode="NearestNeighbor" to your Image tag.

tkerwin
  • 9,559
  • 1
  • 31
  • 47
  • 6
    I don't understand how this answer is considered to have less of a code example than the accepted answer. That's kind of messed up. :-/ – BrainSlugs83 Dec 18 '13 at 23:01
0

Increase resolution / scale without anti-aliasing.

Whats happening is WPF is scaling the image but "averaging" the pixels, rather than doing a more blocky scale.

See this post:

Resize bitmap like in MS Paint

Community
  • 1
  • 1
Will Charczuk
  • 919
  • 1
  • 7
  • 17