Resource image can be referenced in Xaml but not in code, why? - c#

Excuse the novice question, but I am just learning WPF.
I have an image that I've set the build action to be "Resource"
If I set it as the Window Icon in the XAML it works just fine.
like this:
Window ... Icon="Images/MyIco.png"
But, if I try to set it as the source for an Image, I get an exception the resource is not found:
Uri uri = new Uri("pack://application:,,,/Images/MyIco.png"); // This does not work
img.Source = BitmapFrame.Create(uri);
What am I doing wrong in the code above?

Make sure your path is correct, if true, u can try this code to load your image
//tmp is your path
BitmapImage img = new BitmapImage(new Uri(tmp, UriKind.Relative));
img.CreateOptions = BitmapCreateOptions.None;
img.ImageOpened += img_ImageOpened;
void img_ImageOpened(object sender, RoutedEventArgs e)
{
WriteableBitmap wbm = new WriteableBitmap((BitmapImage)sender);
}
Ex: my path is "/Assets/sizes/background/bg_02.png"

Related

I'm lost at how to configure my code so that , when i click the radio button a certain image appears

I have to create a small pizza delivery program in C# using Visual Studio 2019 Community Edition :
It has 3 Radio Buttons ( for 3 different pizza's), A Textblock for text ( to show the ingredients), and an image to show the type of pizza.
So I want that when I click on one of those 3 Radio Buttons, a certain text & image appears.
The Radio Buttons and TextBlock is ok, but the image I'm lost at how to properly configure it using the code behind.
Can anyone check the code, and maybe enlighten me?
https://ideone.com/e.js/MjEBnd
private void radiobtnHawai_Checked(object sender, RoutedEventArgs e)
{
string Hawai = "Ananas, Kip, Kaas";
lbl1.Text = Hawai;
Image Hawai.jpg = new Image();
BitmapImage bi3 = new BitmapImage();
bi3.BeginInit();
bi3.UriSource = new Uri("smiley_stackpanel.PNG", UriKind.Relative);
bi3.EndInit();
Hawai.jpg.Stretch = Stretch.Fill;
Hawai.jpg.Source = bi3;
}
Here is an Extra Image : https://i.stack.imgur.com/iiHRn.png
Thanks in Advance,
If more information is needed, please don't hesitate to ask.
Suppose you have a image control defined like this
<Image Name="ImageViewer1" Height="400" Width="400" />
On your radio checked command , you have to assign the source of "ImageViewer1".
For example, in your case it will look like this
private void radiobtnHawai_Checked(object sender, RoutedEventArgs e)
{
string Hawai = "Ananas, Kip, Kaas";
lbl1.Text = Hawai;
BitmapImage bi3 = new BitmapImage();
bi3.BeginInit();
bi3.UriSource = new Uri("smiley_stackpanel.PNG", UriKind.Relative);
bi3.EndInit();
ImageViewer1.Stretch = Stretch.Fill;
ImageViewer1.Source = bi3;
}
Also make sure that, smiley_stackpanel.PNG, exists in your Environment.CurrentDirectory.
By specifying relative path it will look images from the path where your exe resides

Setting the source of WPF Image control behind code

Recently I've been strugling with setting the Setting the source of WPF Image control inside code.
I've tried something like:
Image.Source = new BitmapImage(new Uri(#"/hrc.Hands;component/Images/BlueFolder.png", UriKind.RelativeOrAbsolute));
But it doesn't seem to work and thorws Object reference not set to an instance of an object exception. Here's the screenshot of the path to my image inside hierachy:
You can use..
var imageSource = new Uri(#"pack://application:,,,/" + Assembly.GetExecutingAssembly().GetName().Name + ";component/" + "Images/BlueFolder.png", UriKind.Absolute);
Image.Source = imageSource;
Also, make sure that BlueFolder.png is set as a Resource. Right click the image, and select Properties to do this.

How to set the background image of a panel from a resource file in C#?

I want to change the background image of a panel in a C# Windows Forms application. The image I want to set as the background is located in a resource folder in Solution Explorer. How do I use it in code?
I tried this:
panel1.BackgroundImage = Properties.Resources.Chalkboard;
But it didn't work.
I tried the same code like you did, and it works fine when I hit a button.
private void pnlBgBtn_Click(object sender, EventArgs e)
{
panel1.BackgroundImage = Properties.Resources.image;
}
The name 'image' in 'Properties.Resources.image' should be the name you gave to the image.
The right name of the image should be the name shown in your project properties under project-proje.
The properties.Resources class doesn't return every resourse as an image so you have to apply a cast to Image like this
panel1.BackgroundImage = (Image)(Properties.Resourses.Chalkboard);
You can try this out:
Bitmap bmp = new Bitmap(System.Reflection.Assembly.GetEntryAssembly().
GetManifestResourceStream("MyProject.Resources.myimage.png"));
panel1.BackgroundImage = bmp;
If you want to set the background image of a panel on page load then you have to write this code:
private void panel1_Paint(object sender, PaintEventArgs e)
{
Assembly asm = Assembly.GetExecutingAssembly();
Bitmap backgroundImage = new Bitmap(asm.GetManifestResourceStream("Image913.jpg"));
e.Graphics.DrawImage(
backgroundImage,
this.ClientRectangle,
new Rectangle(0, 0, backgroundImage.Width, backgroundImage.Height),
GraphicsUnit.Pixel);
}
If you want set the image except the panel, load use this code:
Bitmap bmp = new Bitmap(System.Reflection.Assembly.GetEntryAssembly().
GetManifestResourceStream("MyProject.Resources.photo0018.jpg.png"));
panel1.BackgroundImage = bmp;
You can create an icon resource in Properties project folder. When you opened Properties click on Resources.resx and there Add Resource->Add New Icon menu items. This will create an icon. You can also load an icon from an existing file into the resource, in this case the icon will be built in your executable. So, when your icon added as a resource it will be given some name.

WPF - How to get the URI Source of an Image?

I want to get the URI Source of an image that has been created in XAML. The source of the image is a BitmapFrame. I know you can use BitmapImage.UriSource for BitmapImages, but what about BitmapFrames?
To get the BitmapFrame I use:
private void imgs_Loaded(object sender, MouseEventArgs e)
{
Image img = (Image)sender;
BitmapFrame frame = (BitmapFrame)img.Source;
//How to get frame's UriSource?
}

Adding newly created image object to window

Image myImage3 = new Image();
BitmapImage bi3 = new BitmapImage();
bi3.BeginInit();
bi3.UriSource = new Uri(#"C:\Users\User\Desktop\Awesomeness\PicProgram\Program\bin\Debug\flower.png", UriKind.Relative);
bi3.EndInit();
myImage3.Stretch = Stretch.Fill;
myImage3.Source = bi3;
I was wondering how I get myImage3 added to the window.
I've tried this.content.add.myImage3 or this.add(myImage3) and other variations none seem to work.
Any suggestions?
Thanks
Seems like a basic wpf usage question, although doing something that is simple in xaml can get a bit complex in code behind.....
You have to add your image to some panel or content control in your window, not the window itself. So if there is a grid in your window, give the grid a name like m_grid, then do m_grid.children.add(myImage3).

Categories