locked
how to create a BitmapImage by URI

    Question

  • Hi greate master:

    I want to create a BitmapImage by Uri, the image is on  folder "C:\\Users\\abbeyant\\Pictures\\". the first and the second are works well, but the third is always running into +ImageFailed.

    Who can help to tell the method to create Uri on address "C:\\Users\\abbeyant\\Pictures\\7990257_2.jpg".  (comments:Please not suggest me  to use IRandomAccessStream, Because i will cost more time and memory than Uri method.)

    BitmapImage sBImage = new BitmapImage(new Uri(this.BaseUri, @"Images\all_exif_info.JPG"));
    BitmapImage sBImage = new BitmapImage(new Uri("http://ww4.sinaimg.cn/bmiddle/5a05224fgw1ds3rwk3pyvj.jpg"));

    BitmapImage sBImage = new BitmapImage(new Uri(this.BaseUri, "C:\\Users\\abbeyant\\Pictures\\7990257_2.jpg"));


    BR

    Abbeyant.

    Monday, March 18, 2013 9:44 AM

Answers

  • Use the IRandomAccessStream to load the image from a StorageFile.

    The direct access you are attempting to use fails because access is denied.

    You app does not have direct access to the Pictures library and cannot use paths directly there. The access granted by the PicturesLibrary capability is only usable through the StorageItem brokers.

    The other two BitmapImage loads succeed because the app has direct access to its own directories (either by path or with the ms-appx: protocol) and to http: (via the networking capabilities). There is no similar protocol to target the libraries.

    See Skip the path: stick to the StorageFile for more information.

    --Rob


    Tuesday, March 19, 2013 2:09 AM
    Owner

All replies

  • I don't have first hand experience because I am still working on the framework, but the following is how images are returned within the Hilo project (http://hilo.codeplex.com):

    BitmapImage^ Photo::Thumbnail::get()
    {
        assert(IsMainThread());
        IRandomAccessStream^ thumbnailStream = dynamic_cast<IRandomAccessStream^>(m_fileInfo->Thumbnail);
        if (nullptr == thumbnailStream)
        {
            m_isInvalidThumbnail = true;
            return ref new BitmapImage(ref new Uri("ms-appx:///Assets/HiloLogo.png"));
        }
        auto thumbnail = ref new BitmapImage();
        create_task(thumbnail->SetSourceAsync(thumbnailStream)).then(ObserveException<void>(m_exceptionPolicy));
        m_isInvalidThumbnail = false;
        return thumbnail;
    }
    
     

    The following help information is consistent with what you are reporting.

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.image.source.aspx

    The ImageFailed event can occur if the initial Source attribute value in XAML does not specify a valid source. You can use a relative path to reference an image that you package with the app, or an absolute URI to reference an image from a server. If you are using an image source from your app, it's common to use either the ms-appx or ms-resource schemes.


    MCAD.NET C# - http://www.Global-webnet.com/Blog

    Monday, March 18, 2013 8:06 PM
  • Thanks for your help. I tried to put the test image into app file and used below code, it works well. So I worried about the Pictures folder's image whether can use Uri read or not, if its relation about permission, i will consider other methods. (Package.appxmanifest capabilities had permission to visit pictures library, app can use IRandomAccessStream to read stream.)

     "BitmapImage sBImage = new BitmapImage(new Uri(ApplicationData.Current.LocalFolder.Path + "\\06100814563737.jpg", UriKind.RelativeOrAbsolute));"

    Tuesday, March 19, 2013 1:29 AM
  • Use the IRandomAccessStream to load the image from a StorageFile.

    The direct access you are attempting to use fails because access is denied.

    You app does not have direct access to the Pictures library and cannot use paths directly there. The access granted by the PicturesLibrary capability is only usable through the StorageItem brokers.

    The other two BitmapImage loads succeed because the app has direct access to its own directories (either by path or with the ms-appx: protocol) and to http: (via the networking capabilities). There is no similar protocol to target the libraries.

    See Skip the path: stick to the StorageFile for more information.

    --Rob


    Tuesday, March 19, 2013 2:09 AM
    Owner
  • So, you mean the Pictures folder's images only can be read by IRandomAccessStream sStream = StorageFile.OpenAsync(FileAccessMode.Read);

    But my app's branch function is like "photos app", if use StorageFile to read stream and set it into BitmapImage, it will cost more time and memory, the flipview will be block at whiles if the Image is more than 4M pixel when flick quickly.

    can you tell me the framework about "photos app", Why it works well.

    Tuesday, March 19, 2013 2:35 AM