Jump to content
  • Advertisement
Sign in to follow this  
Dave Haylett

WPF - Best way to load image from local disc into WriteableBitmap

This topic is 971 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

 

I'm trying to load a PNG file from the local disc at runtime, and am getting confused with getting absolute path URIs working, and also the best way to load the image in (FileStream / ByteArray/ etc.).

 

Given an absolute path...

 

string myPath = "C:\pic.png";

 

...I want to load the image into a WriteableBitmap.

 

I keep getting "Unsupported URI syntax. Method expects a relative URI or a pack://siteoforigin:,,,/ form of absolute URI". I understand this is because the end-user's drive mapping may be different and so once cannot specify a drive path per se, but I can't find any useful examples of how to accomplish this simple task. Or is URI even the correct way of accomplishing what I want?

 

I need to load at runtime into WriteableBitmap using a method such as .FromByteArray or .FromStream. or whatever works.

 

The file is not part of the project and so I cannot use .FromResource  (this is because my function is loading half a dozen images out of 10,000 on the disc).

 

Thanks for any help.

Share this post


Link to post
Share on other sites
Advertisement
The first thing I notice is that you haven't escaped your backslash:

"C:\pic.png" in C# will become "C:pic.png" to the computer. C# should give you a compile error "Unrecognized escape sequence" for this, though. You can try "C:\\pic.png".

If the API really DOES need a URI and doesn't automatically understand file paths, then you can use the System.Uri class to convert:

http://stackoverflow.com/questions/1546419/convert-file-path-to-a-file-uri Edited by Nypyren

Share this post


Link to post
Share on other sites

If you're using C# then do this:

// Change this
string myPath = "C:\pic.png";

// to this. Notice the @
string myPath = @"C:\pic.png";

// or you can do what Nypren said and do
string myPath = "C:\\pic.png";

Share this post


Link to post
Share on other sites

Thanks folks, however I still get the error. Please see below.

 

var sri = Application.GetRemoteStream(new Uri(@"C:\pic.png", UriKind.Absolute));
 
No matter what I do, I always get the "Unsupported URI syntax. Method expects a relative URI or a pack://siteoforigin:,,,/ form of absolute URI" exception. I even get this when I deliberately put in a non-existent filename, making me think there's something pretty fundamental I don't understand about determining URIs.
 
I need to convert to URI for the WriteableBitmap line:    img_loadedScreenshot.FromStream(sri.stream);     to work.

Share this post


Link to post
Share on other sites
I've never used Application.GetRemoteStream. It's probably not what you want.

This is the minimal code I know of to load a file into a WriteableBitmap object:
 
BitmapImage bi = new BitmapImage(new Uri(@"C:\pic.png"));
WriteableBitmap eb = new WriteableBitmap(bi);
What type is img_loadedScreenshot? WriteableBitmap doesn't have FromStream for me... Edited by Nypyren

Share this post


Link to post
Share on other sites

I've never used Application.GetRemoteStream. It's probably not what you want.

This is the minimal code I know of to load a file into a WriteableBitmap object:
 

BitmapImage bi = new BitmapImage(new Uri(@"C:\pic.png"));
WriteableBitmap eb = new WriteableBitmap(bi);
What type is img_loadedScreenshot? WriteableBitmap doesn't have FromStream for me...

 

 

You, sir, are a genius. Worked first time :) Consider your post +1'd.

 

FYI I am using the WriteableBitmap Extensions, and so FromStream might be a member of that. Not that I need it now :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!