1
votes

I found that my problem lies in a method I used to resize the picture: Here's the code:

 private WriteableBitmap ResizeImage(BitmapImage original, double destWidth, double destHeight)
        {
            Image image = new Image()
            {
                Source = original,             
                Stretch = Stretch.UniformToFill
            };
            image.UpdateLayout();
            int origWidth = original.PixelWidth;
            int origHeight = original.PixelHeight;
            ScaleTransform st = new ScaleTransform();                
            st.ScaleX = destWidth / (double)origWidth;
            st.ScaleY = destHeight / (double) origHeight;
            WriteableBitmap result = new WriteableBitmap((int)destWidth, (int)destHeight);
            result.Render(image, st);
            result.Invalidate();
            return result;
        }

I tested my code under 2 circumstances:

  1. pass picture from "Camera Roll"
  2. pass picture from other albums

My code will work on "Camera Roll" pictures, but it will return me a whole black result for the pictures loaded from other albums.

In either of these circumstances, despite the bitmap is whole black or not, this method will return me the bitmaps correct width and height.

For these two scenario, I'm using the same method to load pictures, but why only pictures from camera roll can be displayed but those who come from other albums cannot?

I know the WriteableBitmapEx library has the method to do resize perfectly. But I just curious about why my method won't work? Could any one help me with that?

1
You should be able to change the source of your Image control. There may be something wrong with your code. What do you mean by "this method doesn't work", nothing is displayed, the old picture is still displayed, an exception is thrown, ... ? - Kevin Gosse
@KooKiz thx for your reply. I meant the Image container will be a blank, no exception been thrown. I'll update my question with some code. - dumbfingers

1 Answers

0
votes

Try the following:

private static BitmapSource ResizeImage(BitmapImage original, int destWidth, int destHeight)
{
    if (original == null) return null;
    if (destWidth == original.PixelWidth && destHeight == original.PixelHeight) return original;
    return new TransformedBitmap(original, new ScaleTransform((double)destWidth / original.PixelWidth, (double)destHeight / original.PixelHeight));
}

I'm not sure if TransformedBitmap is available on Windows Phone...


st.ScaleX = destWidth / origWidth;
st.ScaleY = destHeight / origHeight;

For instance, if destWidth=150 and origWidth=300 then st.ScaleX will be 0.0 (not 0.5).