locked
How do I load an resize an image from a url and use it as an image source?

    Question

  • My app is crashing due to memory usage, so I want to resize an image from a url before using it as an image source.

    Here is my current code to generate a resized bitmap image from a url

    private static async Task<BitmapImage> LoadImageWebResize(string url)
            {
    
                var rass = RandomAccessStreamReference.CreateFromUri(new Uri(url, UriKind.Absolute));
    
                using (IRandomAccessStream stream = await rass.OpenReadAsync())
                {
                    BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
                    BitmapTransform transform = new BitmapTransform() { ScaledHeight = 80, ScaledWidth = 80 };
                    PixelDataProvider pixelData = await decoder.GetPixelDataAsync(
                        BitmapPixelFormat.Rgba8,
                        BitmapAlphaMode.Straight,
                        transform,
                        ExifOrientationMode.RespectExifOrientation,
                        ColorManagementMode.DoNotColorManage);
                    BitmapImage bImg = new BitmapImage();
                    bImg.SetSource(stream);
                    return bImg;
                    //return bitMap;
    
                }
    
            }

    It's causing a "Component cannot be found" error.

    Sunday, March 02, 2014 9:26 PM

Answers

  • Hi,

    Which code causing a "Component cannot be found" error in your app? When I create a project to resize a image, I found everything is Ok in my Computer:

    private async void Button_Click(object sender, RoutedEventArgs e)
            {
                Uri url = new Uri("ms-appx:///Assets/Logo.scale-100.png");
                var rass = RandomAccessStreamReference.CreateFromUri(url);
    
                using (IRandomAccessStream stream = await rass.OpenReadAsync())
                {
                    BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
                    BitmapTransform transform = new BitmapTransform() { ScaledHeight = 80, ScaledWidth = 80 };
                    PixelDataProvider pixelData = await decoder.GetPixelDataAsync(
                        BitmapPixelFormat.Rgba8,
                        BitmapAlphaMode.Straight,
                        transform,
                        ExifOrientationMode.RespectExifOrientation,
                        ColorManagementMode.DoNotColorManage);
                   
                    StorageFolder storageFolder = KnownFolders.PicturesLibrary;
                    StorageFile file = await storageFolder.CreateFileAsync("imgetest", CreationCollisionOption.ReplaceExisting);
                    using (var destinationStream = await file.OpenAsync(FileAccessMode.ReadWrite))
                    {
                       
                        BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, destinationStream);
                        encoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Premultiplied, 80, 80, 96, 96, pixelData.DetachPixelData());
                        await encoder.FlushAsync();
                        BitmapImage bImg = new BitmapImage();
                        bImg.SetSource(destinationStream);
                        image.Source = bImg;
                        
                    }
                   
                }
    
            }

    And I recommend you can refer to these links below to know how to decode and encode image file in windows store app:

    http://msdn.microsoft.com/en-us/library/windows/apps/jj709936.aspx

    http://msdn.microsoft.com/en-us/library/windows/apps/jj709935.aspx

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Monday, March 03, 2014 9:19 AM