Solved

How to load a WPF image control from a resource

Posted on 2009-04-01
8
9,405 Views
Last Modified: 2013-12-17
I am trying to fins a relatively simple method to load an icon image into an image control in code behind within a WPF application.

So I have an icon resource called ph_ongo and would like to display the image inside an Image control within a WPF Grid.

The following code doesn't seemto do the trick.

Image img = new Image();
                BitmapImage bmp = new BitmapImage();
 
                 bmp.BeginInit();
                 bmp.UriSource = new Uri("ph_ongo", UriKind.Relative);
                 bmp.EndInit();
 
                 img.Stretch = System.Windows.Media.Stretch.Fill;
                 img.Source = bmp;

Open in new window

0
Comment
Question by:mfenske17363
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 9

Expert Comment

by:peterdungan
ID: 24040205
you need to add img to the canvas, if you aren't doing so already.
make sure the file ph_ongo is in the same folder a your xap is being placed. The default location is clientbin in the web project. You need to include the file extension for it also.
0
 

Author Comment

by:mfenske17363
ID: 24040404
This is not a web app and I want to load the icon file as a resource (ala Win Forms) and access it as an application resource not an external file.
0
 
LVL 9

Expert Comment

by:peterdungan
ID: 24040528
did you add the image to your grid
YourGrid.Children.Add(img);
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mfenske17363
ID: 24041034
Yes, like so:

                                SetColumn(img, 0);
                                SetRow(img, y);
                                this.Children.Add(img);
0
 
LVL 8

Expert Comment

by:unmeshdave
ID: 24044222
please put full code to show how are setting row columns in grid and their height width.
0
 
LVL 11

Expert Comment

by:Mihai Stancescu
ID: 24047949
Try adding the extension to the name of the icon  bmp.UriSource = new Uri("ph_ongo.ico", UriKind.Relative);


Hope this helps!

Regards,
Mishu
0
 

Author Comment

by:mfenske17363
ID: 24049496
Well, I can do this and have it work:
 
                Image img = new Image();
                BitmapImage bmp = new BitmapImage();

                img.Stretch = System.Windows.Media.Stretch.None;
                img.HorizontalAlignment = HorizontalAlignment.Left;
                img.VerticalAlignment = VerticalAlignment.Center;

                bmp.BeginInit();
                bmp.UriSource = new Uri("C:\\src\\vs2008\\projects\\Test3DSelector\\TestSelector3D\\ph_ongo.ico", UriKind.RelativeOrAbsolute);
                bmp.EndInit();

                img.Source = bmp;

                SetColumn(img, 0);
                SetRow(img, y);
                this.Children.Add(img);


but still not sure how to access as an internal resource and display.
0
 
LVL 11

Accepted Solution

by:
Mihai Stancescu earned 250 total points
ID: 24392490
To access a resource use bmp.UriSource = new Uri("ph_ongo.ico", UriKind.Relative); instead of
bmp.UriSource = new Uri("C:\\src\\vs2008\\projects\\Test3DSelector\\TestSelector3D\\ph_ongo.ico", UriKind.RelativeOrAbsolute);.

If you have images in a folder inside your project you can use it like this : new Uri("/Images/ph_ongo.ico", UriKind.Relative);

Hope this helps!

Regards,
Mishu
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question