Skip to main content
GameDev.net gamedev.net
🔒 Locked

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

Started by Dave Haylett Jan 23, 2016 at 10:51 AM 4 replies 8.8k views
Original Post
Dave Haylett
Dave Haylett

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.

Nypyren
Nypyren
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
Alpha_ProgDes
Alpha_ProgDes

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";
Beginner in Game Development?  Read here. And read here.  
Dave Haylett
Dave Haylett

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.
Nypyren
Nypyren
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...
Dave Haylett
Dave Haylett

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 :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.