darthmaule2
Topic Author
Posts: 52
Joined: 23 Oct 2014, 19:54

How to load and display a local image with managed SDK?

07 Jan 2019, 19:08

I'm trying to load a local bmp file as a texture so I can display it in an <Image> element in a view.
string basePath = @"C:\samples";
NoesisApp.LocalTextureProvider localTextureProvider = new NoesisApp.LocalTextureProvider(basePath);
Texture texture = localTextureProvider.LoadTexture("test.bmp");
TextureSource textureSource = new TextureSource(texture);
But the above code results in: texture == null

Tags:
 
User avatar
jsantos
Site Admin
Posts: 2342
Joined: 20 Jan 2012, 17:18
Contact:

Re: How to load and display a local image with managed SDK?

07 Jan 2019, 23:37

I recommend separating the providers from the code that creates UI elements.

So, for the providers I recommend implementing a new FileTextureProvider to provide streams to the files. This is similar to our LocalTextureProvider or EmbeddedTextureProvider. The null you are getting is because we are not able to find the file, I am not sure if that's related to using a path starting with 'C:\'. Probably, I will have a look at it as soon as possible. But meanwhile, I recommend implementing yours because it is a very simple task, similar to the XamlProvider you already implemented.

For loading the images in the XAML, I think the better way is just writing the filenames in the XAML itself, but if you want to do it by code, you can use BitmapImage like this:
BitmapImage source = new BitmapImage(new Uri("image.png"));
That code will send a request to the texture provider.
 
darthmaule2
Topic Author
Posts: 52
Joined: 23 Oct 2014, 19:54

Re: How to load and display a local image with managed SDK?

08 Jan 2019, 14:40

I tried this (and many variations of it) first but it crashes, claiming it can't find the image. I also tried using the BitmapImage provide by System.Windows.Media.Imaging with no luck.
Noesis.BitmapImage source = new Noesis.BitmapImage(new Uri("C:\test\image.bmp").AbsolutePath);
Perhaps this is the same issue with the provider not looking at the C: drive. I need to do this in code because we have a "file manager" which provides a view of the file system and allows the user to navigate pretty much anywhere on their local drive, a mapped network drive or an attached USB drive.

I will try to implement a FileTextureProvider next.
 
User avatar
jsantos
Site Admin
Posts: 2342
Joined: 20 Jan 2012, 17:18
Contact:

Re: How to load and display a local image with managed SDK?

08 Jan 2019, 14:47

yes, try the FileTextureProvider please, I think it will work that way.
 
darthmaule2
Topic Author
Posts: 52
Joined: 23 Oct 2014, 19:54

Re: How to load and display a local image with managed SDK?

08 Jan 2019, 16:35

So I have a very simple FileTextureProvider based class working:
    
    public class FileSystemTextureProvider : FileTextureProvider
    {
        public FileSystemTextureProvider()
        {
        }

        public override Stream OpenStream(string path)
        {
            return new FileStream(path, FileMode.Open, FileAccess.Read);
        }
    }
    
Questions:
1.) If I open a Noesis.BitmapImage on a background thread (which triggers this OpenStream call), will all the processing be done in that background thread? (If a user is scrolling through a large directory, I will be loading/unloading thumbnails constantly, which can't happen on the UI thread.)
2.) How/when does this stream get closed?
 
User avatar
jsantos
Site Admin
Posts: 2342
Joined: 20 Jan 2012, 17:18
Contact:

Re: How to load and display a local image with managed SDK?

08 Jan 2019, 16:50

Note that you can only use Noesis from one thread (your main thread, the UI thread, whatever) so Noesis.BitmapImage cannot be created in a background thread. Regarding requests to the texture provider. You can observe that the stream is requested at least twice:
  • From the main/UI thread, just to scan the header and get the dimensions of the texture, for the layout.
  • From the render thread, to create the device texture to be used by the renderer.
In both cases, the Stream should be disposed, so you probably shouldn't care about its lifetime. There is an internal cache of textures to avoid loading and unloading thumbnails at render time. But in case you need more control, you can reimplement the class TextureProvider (this is the parent of FileTextureProvider) and create the textures yourself in the device.

Who is online

Users browsing this forum: No registered users and 4 guests