3

I have a image control in WPF. I need to reduce the image size control width and height. But when i Do that , the image is not looking good. The data loss is more.

So i thought to reduce the image resolution instead of just changing the image control width and height.

can any one help me how to change the image resolution of the binded image in WPF image control

[I mean image is already binded to image control now I have to the change the resolution only]

user1307428
  • 87
  • 1
  • 5

4 Answers4

7

In .NET 4 they changed the default image scaling to a low quality one...so you can use BitmapScalingMode to switch back to the higher quality one:

<Image RenderOptions.BitmapScalingMode="HighQuality"
       Source="myimage.png"
       Width="100"
       Height="100" />

You can also combine the above with other options like the Decode options if your source image is a huge image (this just reduces memory usage in your application).

other options to prevent "blurryness" is to put UseLayoutRounding="True" on your root element (i.e. Window)....it's recommended to use this in .NET 4 rather than SnapToDevicePixels:

Community
  • 1
  • 1
Colin Smith
  • 12,375
  • 4
  • 39
  • 47
  • My GIF image is quite huge in size. How can I set the `DecodePixelWidth` and `DecodePixelHeight` with the use of WPF Animated GIF? – Casper Aug 21 '17 at 03:50
1

You can use the DecodePixelWidth property like this:

<Image Stretch="Fill">
    <Image.Source>
        <BitmapImage CacheOption="OnLoad" DecodePixelWidth="2500" UriSource="Images/image.jpg"/>
    </Image.Source>
</Image>
mlemay
  • 1,622
  • 2
  • 32
  • 53
0

1) Have a try with a ViewBox : put your image within a ViewBox.
2) Sometimes the rendering engine looses quality because of pixel alignement issues, especially on low-resolution device. Have a look to SnapsToDevicePixels property, and try to set it to true in the containing control AND / OR in the ViewBox.
3) Obviously you could write a Control that perform a resolution change but it is quite some work.

GameAlchemist
  • 18,995
  • 7
  • 36
  • 59
-1

Try this.

Image img = new Image();

var buffer = System.IO.File.ReadAllBytes(_filePath);
MemoryStream ms = new MemoryStream(buffer);

BitmapImage src = new BitmapImage();
src.BeginInit();
src.StreamSource = ms;
src.DecodePixelHeight = 200;//Your wanted image height
src.DecodePixelWidth = 300; //Your wanted image width 
src.EndInit();

img.Source = src;
이기수
  • 9
  • 2