Solved

How to load a WPF image control from a resource

Posted on 2009-04-01
8
9,341 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

760 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