Solved

How to load a WPF image control from a resource

Posted on 2009-04-01
8
9,358 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
 

Author Comment

by:mfenske17363
ID: 24041034
Yes, like so:

                                SetColumn(img, 0);
                                SetRow(img, y);
                                this.Children.Add(img);
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

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!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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…

867 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now