0

This image is from scanner. I am using C# WPF to show image.But my problem is that image showing is not unclear. Below is my code( .xaml and .cs) .I found 2 ways to tranfer the file into image source. But both not work well. If you use any image view sofeware to open image, the image is very clear.

.xaml

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    Loaded="WindowLoaded">
    <Grid>
        <Image Name="image"></Image>
    </Grid>
</Window>

.cs(just part of code)

    private void WindowLoaded(object sender, RoutedEventArgs e)
    {   
        string StrImage = @"ImageFilePath";
        this.image.Source = FileToImageSource(StrImage);
        //this.image.Source = FileToImageSource_2(StrImage);
    }
    //blurry
    public static BitmapImage FileToImageSource(string strFileName)
    {
        byte[] bytes = File.ReadAllBytes(strFileName);
        BitmapImage bitmap = new BitmapImage();
        bitmap.BeginInit();
        bitmap.StreamSource = new MemoryStream(bytes);
        bitmap.EndInit();
        return bitmap;
    }
    //blurry too
    public static BitmapImage FileToImageSource_2(string strFileName)
    {
        BitmapImage logo = new BitmapImage();
        logo.BeginInit();
        logo.UriSource = new Uri(strFileName);
        logo.EndInit();
        return logo;
    }

Update: i have try but failed image property : RenderOptions.BitmapScalingMode="NearestNeighbor"
Stretch="None"

blurry image vs clear image blurry image vs clear image image file

  • It is not relevant whether you create the BitmapImage from a Stream or from a URI. It is the rendering part that makes it blurry, i.e. the Image element. As a note, the FileToImageSource_2 method may consist of only a single line of code: `return new BitmapImage(new Uri(strFileName));` – Clemens Jul 30 '18 at 11:32
  • So what exactly I can do to fix this problem?about the rendering part. – user1298467 Jul 30 '18 at 12:12
  • Read the answers to the duplicate question. Try `RenderOptions.BitmapScalingMode="NearestNeighbor"`. Perhaps also set `Stretch="None"`. – Clemens Jul 30 '18 at 12:14
  • not working.....still the same – user1298467 Jul 31 '18 at 01:08
  • RenderOptions.BitmapScalingMode="HighQuality" solve this problem. – user1298467 Jul 31 '18 at 03:02

0 Answers0