.NET Framework Developer Center > .NET Development Forums > Windows Presentation Foundation (WPF) > pack uri - using it to extract an image from resources in a WPF project
Ask a questionAsk a question
 

Answerpack uri - using it to extract an image from resources in a WPF project

  • Sunday, November 01, 2009 3:55 PMCharles O. Brown Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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

  • Wednesday, November 04, 2009 9:17 AMDanielRose Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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.
  • Sunday, November 01, 2009 5:38 PMOlaf Rabbachin Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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
  • Wednesday, November 04, 2009 8:07 AMBruce.ZhouMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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.

All Replies

  • Sunday, November 01, 2009 5:04 PMKenneth Haugland Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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
  • Sunday, November 01, 2009 5:38 PMOlaf Rabbachin Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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
  • Wednesday, November 04, 2009 8:07 AMBruce.ZhouMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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.
  • Wednesday, November 04, 2009 9:17 AMDanielRose Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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.
  • Thursday, November 26, 2009 5:17 PMOlaf Rabbachin Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    <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
  • Sunday, November 29, 2009 6:22 PMCharles O. Brown Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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.