Solved

How to load a WPF image control from a resource

Posted on 2009-04-01
8
9,375 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 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