none
Scalling an Image WP8 RRS feed

  • Question

  • I have looked through tons of code but none seems to help at all. Right now I am getting an image from a website and then I am trying to scale it down to fit on the screen. I know all the math but the functions for doing this escapes me. I did see a scale transform and transform class but I can not call these guys in windows phone 8. What functions do I need to call to set the scale of the image?

                    BitmapImage bmp = new BitmapImage();
                    bmp.SetSource(args.Result);
                    img.Width = bmp.PixelWidth;
                    img.Height = bmp.PixelHeight;
                    img.Source = bmp;
    If I do not set the img.Width it will only display part of the bitmap. But I am wanting to display the full bitmap image and scale it down to 400x400.


    Matthew Montgomery

    Wednesday, May 22, 2013 12:45 AM

All replies

  • Microsoft.Phone.PictureDecoder.DecodeJpeg(fileStream, maxSize, maxSize)

    Wednesday, May 22, 2013 3:02 AM
  • Is img an Image control? By default, when an Image control displays its source, it will be automatically scaled to fit the control's height and width. All you need to do is set the Width and Height to be displayed:

        img.Width = 400;
        img.Height = 400;

    Specifically, the Image.Stretch property controls scaling. The default value is Stretch.Uniform (scale the bitmap uniformly to fit the Height and Width). The other values for stretch are Fill, UniformToFill, and None. Only 'None' will display the bitmap without scaling.

    BTW, the scale and transform classes are fully supported on Windows Phone.


    Richard Woo
    • Edited by mSpot Inc Wednesday, May 22, 2013 6:18 AM
    Wednesday, May 22, 2013 6:16 AM
  • I figured it would stretch to fit the image or scale down automatically but it does not. I am pulling the image from a website and do not know how big the image is until I get it.

    Matthew Montgomery

    Wednesday, May 22, 2013 1:33 PM
  • well I would like to try the Microsoft.Phone.PictureDecoder.DecodeJpeg(fileStream, maxSize, maxSize)

    but my code now breaks nothing has changed but now I get an error. The code breaks when creating a response.

    AsyncWaitHandle = 'asynchronousResult.AsyncWaitHandle' threw an exception of type 'System.NotSupportedException'

    I have no idea how this is happening or how to fix it any help would be greatly appreciated. :)

    try { HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

    //Dies Here

    using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult)) { using (StreamReader streamReader1 = new StreamReader(response.GetResponseStream())) { GetListOfPages2(streamReader1); } } } catch (Exception e) { Console.WriteLine(e.Message); }



    Matthew Montgomery

    Wednesday, May 22, 2013 3:19 PM
  • I figured it would stretch to fit the image or scale down automatically but it does not. I am pulling the image from a website and do not know how big the image is until I get it.

    I never have to actively manage the scaling to display with an Image control. It's supposed to insulate you from having to do that. It is typical that the original image size is unknown. Can you provide more complete code?

    Here's an example using WebClient:

                <Image x:Name="img"/>
    public partial class MainPage : PhoneApplicationPage
    {
        public MainPage()
        {
            InitializeComponent();
    
            WebClient wc = new WebClient();
            wc.OpenReadAsync(new Uri("http://cdn.slashgear.com/wp-content/uploads/2012/06/Windows-Phone-Summit-2012-slashgear_s-Photos.jpg"));
            wc.OpenReadCompleted += wc_OpenReadCompleted;
        }
    
        void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            BitmapImage bmp = new BitmapImage();
            bmp.SetSource(e.Result);
            img.Width = 400;
            img.Height = 400;
            img.Source = bmp;
        }
    }

    Or even simpler:

    Uri uri = new Uri("http://cdn.slashgear.com/wp-content/uploads/2012/06/Windows-Phone-Summit-2012-slashgear_s-Photos.jpg");
    BitmapImage bmp = new BitmapImage(uri);
    img.Width = 400;
    img.Height = 400;
    img.Source = bmp;


    Richard Woo
    Wednesday, May 22, 2013 5:32 PM