Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to load a WPF image control from a resource

Posted on 2009-04-01
8
Medium Priority
?
9,457 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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 1000 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

610 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