pack uri - using it to extract an image from resources in a WPF project
- WPF Application named TestWPF
I do not know why the following syntax is not working (Tried many permutations)
1. Dim bmpimg As New BitmapImage(New Uri("pack://application:,,,/TestWPF;component/Resources/bw4.jpg", UriKind.RelativeOrAbsolute))2. Dim bmpimg As New BitmapImage(New Uri("pack://application:,,,/bw4.jpg"))
3. 'Dim bmpimg As New BitmapImage(New Uri("pack://application:,,,/TestWPF;component/Images/bw4.jpg", UriKind.RelativeOrAbsolute))
The exception is always the same - can not locate resource "x" - where "x" is anything following
application:,,,/ or TestWPF;component/ - is sems as if the Namespace;component is not needed
since it never complains when it is left out of the syntax and never includes it as part of "x".
Searching other comments I have seen that the following syntax is supposed to work:
pack://application:,,,/filename.extension
All of my resources were added using Visual Studio 2008 under the resources section of My Project, and were added under images, and in the project they are in a folder called Resources (automatically created by VS).
I have tried "x" as: (thinking that maybe these are created folders in the application assembly)
bw4.jpg
Resources/bw4.jpg
Images/bw4.jpg
Resources/Images/bw4.jpg
Answers
- Besides what Olaf said, make sure that the Build Action of the image is actually set to Resource. I find that often VS sets it to Embedded Resource, which doesn't work.
- Marked As Answer byCharles O. Brown Sunday, November 29, 2009 6:10 PM
- Hi Charles,
in WPF-projects, I have never used the Resources to provide images for my projects. I actually don't know as to whether this is even feasible.
FWIW - when using resources, i.e. in the "WinForms way", this allowed to provide your (image-) resources as strongly typed objects. Since images under WPF are really assigned using URIs, this probably can't work at all.
What you should do: create a folder (i.e. named "Resources" or "Images") underneath your solution's root-directory and simply drag and drop your images into this folder. When doing so, VisualStudio will set the appropriate properties for you, i.e. setting their Build Action to Resource and Copy To OutputDirectory to Do not copy . Then, when using these images from either XAML or code, refer to them as "/Name of your folder/Name of your image.whatever". As long as you have your images in the same assembly, this will do. You'll only need the pack -syntax if the compiler is to actually take them from a different assembly, i.e. if you need to use images from on project in your solution in another one in the same solution.
Here's what your code could look like (there's more than one possible approach), if your folder was named "Resources":
dim bmpimg as new BitmapImage(new uri("/Resources/bw4.jpg", UriKind.Relative))
Note the leading slash, which indicates that you're refering to the solution's root directory rather than the directory in which the file resides from which you're doing this. This way you can have i.e. a window use this resource regardless of where the window.XAML is located, i.e several levels deep in your solution's folder hierarchy.
Cheers,
Olaf- Marked As Answer byBruce.ZhouMSFT, ModeratorFriday, November 27, 2009 11:37 PM
- Hi Charles,
I would suggest you read this blog to have a better understanding with Pack Uri.
Best regards,
Bruce Zhou
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Marked As Answer byBruce.ZhouMSFT, ModeratorFriday, November 27, 2009 11:37 PM
All Replies
- if you add the picture directly to your project... Ia the same way you add a class...(Drag and drop) You should be able to write
Dim bmp as BitmapSource
bmp.Itemssource = new BitmapImage(new URi(bw4.jpg)
Kenneth - Hi Charles,
in WPF-projects, I have never used the Resources to provide images for my projects. I actually don't know as to whether this is even feasible.
FWIW - when using resources, i.e. in the "WinForms way", this allowed to provide your (image-) resources as strongly typed objects. Since images under WPF are really assigned using URIs, this probably can't work at all.
What you should do: create a folder (i.e. named "Resources" or "Images") underneath your solution's root-directory and simply drag and drop your images into this folder. When doing so, VisualStudio will set the appropriate properties for you, i.e. setting their Build Action to Resource and Copy To OutputDirectory to Do not copy . Then, when using these images from either XAML or code, refer to them as "/Name of your folder/Name of your image.whatever". As long as you have your images in the same assembly, this will do. You'll only need the pack -syntax if the compiler is to actually take them from a different assembly, i.e. if you need to use images from on project in your solution in another one in the same solution.
Here's what your code could look like (there's more than one possible approach), if your folder was named "Resources":
dim bmpimg as new BitmapImage(new uri("/Resources/bw4.jpg", UriKind.Relative))
Note the leading slash, which indicates that you're refering to the solution's root directory rather than the directory in which the file resides from which you're doing this. This way you can have i.e. a window use this resource regardless of where the window.XAML is located, i.e several levels deep in your solution's folder hierarchy.
Cheers,
Olaf- Marked As Answer byBruce.ZhouMSFT, ModeratorFriday, November 27, 2009 11:37 PM
- Hi Charles,
I would suggest you read this blog to have a better understanding with Pack Uri.
Best regards,
Bruce Zhou
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Marked As Answer byBruce.ZhouMSFT, ModeratorFriday, November 27, 2009 11:37 PM
- Besides what Olaf said, make sure that the Build Action of the image is actually set to Resource. I find that often VS sets it to Embedded Resource, which doesn't work.
- Marked As Answer byCharles O. Brown Sunday, November 29, 2009 6:10 PM
- <bump>
Hi Charles,
if any of the replies in this thread help you, please mark them as answers so that the question can be closed.
Cheers,
Olaf - I am sorry for not marking this before, but since Microsoft changed how Outlook, and Oulook Express retrieve or do not retrieve E-Mail from Hotmail I hardly use this E-Mail Address.
I actually found the real reason in Matthew MacDonald's book "Pro WPF with VB 2008" which was that Visual Studio does not set-up resources correctly. Actually, most of the Pack statements I tried work fine if the resourse Build Action is set to Resource.
I am a bit annoyed that VS 2008 does not do resources properly because my first try would have worked instead of having to spend hours testing and then disturbing all of you. Thank you all for your help.


