When reading from a file stream image get flipped -90

272 Views Asked by At

I'm trying to load an image in a local path to a Silverlight image control. This is an out of browser application. When I load the image using a stream it's always flipped upside down.

Any idea why? This happens for large images only.

FileName here gives the full path to local file system, like C:\imageFullPath\image.jpg.

Please help. Following is the code.

------ In Model side------------------------

public BitmapImage DisplayImage
{
    get
    {
        if (!string.IsNullOrWhiteSpace(FileName))
        {
            var b = new BitmapImage();
            b.SetSource(System.IO.File.OpenRead(FileName));                     
            return b;
        }
        return null;
    }
}

------ In Silverlight View side------------------------

<Image Source="{Binding DisplayImage, Mode=TwoWay}" Width="450" Height="250" Margin="0,0,0,0"/>

What i see when debugging

enter image description here

1

There are 1 best solutions below

1
On

BMP are normally stored "upside-down" with the pixels in reverse order from bottom to top, but can use a negative height in the file header to indicate the image is stored top-to-bottom, so perhaps you have such an image.

Does this happen with all images, or just that one?

You could try changing the way the image is loaded, using a Uri in the constructor instead of calling SetSource() with a Stream, as it may account for the header and read the file differently:

var b = new BitmapImage(new Uri(FileName));                    
return b;

Or, if that doesn't help, you can apply a transform to flip every image, but that doesn't solve the underlying problem:

<Image Source="{Binding DisplayImage, Mode=TwoWay}" Width="450" Height="250" Margin="0,0,0,0">
    <Image.RenderTransform>
        <ScaleTransform ScaleY="-1" />
    </Image.RenderTransform>
</Image>