This question already has answers here:
Change WPF window background image in C# code
(7 answers)
Closed 7 years ago.
I'd like to change the background image of my WPF application by a button. I know how to do it using WindowsForms, but in WPF I failed.
I found a solution already, but that solution would copy my background images to the output folder next to the Application.exe
This is not really a solution that I desire. I would like to have the images stored inside my application.
Can somebody explain me detailed what I need to do [how to add the images to the program, especially the resource-properties, how to access them in C#....]. It seems like I am too stupid to set it up correctly :P
Thanks in advance
Firstly, add a Folder to your Solution (Right click -> Add -> Folder), name it something like "Resources" or something useful.
Then, simply add your desired image to the folder (Right click on folder -> Add -> Existing item).
Once it's been added, if you click on the image and open the Properties window (Alt+Enter), you'll see the Build Action is set to Resource and the Copy to Output Directory is set to Do not copy.
You can reference the image in C# using the following code:
this.Background = new BitmapImage(new Uri(#"pack://application:,,,/YourApp;component/YourFolder/YourImage.png"));
Or in XAML:
<Image Source="pack://application:,,,/YourApp;component/YourFolder/YourImage.png" ... />
Or:
<Image Source="/YourApp;component/YourFolder/YourImage.png" ... />
Try this:
this.Background = new ImageBrush(new BitmapImage(new Uri(#"pack://application:,,,/Yourapp;component/yourimage.png")));
Related
This question already has answers here:
Image in WPF Button not Visible at Runtime
(11 answers)
How to load image to WPF in runtime?
(2 answers)
Closed 3 years ago.
I am trying to dynamically insert a WPF Image into a grid. I currently have some code that already does this, it iterates through a list of URLs and then takes the picture from that URL and creates an image and then inserts it into the grid at a specified location.
However, I coded this a few months ago and I have since forgotten how to do it properly. Looking back at my code, I have copied the relevant snippets into a new test method for debugging purpose. The "lite" version of the code clears all the Children and RowDefinitions of the grid, and then creates a new RowDefinition and an Image. It then inserts the image into the row.
RowDefinition def = new RowDefinition
{
Height = new GridLength(400)
};
myGrid.RowDefinitions.Add(def);
Image img = new Image
{
Source = new BitmapImage(new Uri(#"folderpath/myimage.png", UriKind.Relative)),
Margin = new Thickness(0, 5, 0, 5)
};
Grid.SetRow(img, 0);
Grid.SetColumn(img, 0);
myGrid.Children.Add(img);
But when this code is run it produces nothing. Nothing appears inside the grid.
It should be noted that inside my Visual Studio project I have a folder "folderpath" with a bunch of images that are copied upon building the project. If I add myimage.png to the project inside of VS, and run the code snippet above everything works. The image is created and placed inside the grid.
But if I do not add myimage.png to the VS project before building, and instead copy the file over to the correct directory using Windows Explorer and then run the code, it does not work. This makes me believe it may be a directory issue, but when testing Environment.CurrentDirectory it is as expected.
I am now at a loss, why does the image only appear when I've added it inside my VS project before building and not when I manually add it to the folder after the build has completed?
Thanks for your help
I have had a similar issue before with an image showing in the XML editor but not showing when the code is ran. It's actually the intended behavior.
This is because in the editor it will get the link even if it isn't included in the project. However, when you build your code it won't show anything that isn't included in the project. Best practice is to have an img or resource folder in your project where you put all your relevant resources in.
I want to add my icon as tay icon.
But this error is shown.
xaml.cs:
private System.Windows.Forms.NotifyIcon _notifyIcon;
This icon already added on resources but not working.
When I writing codes then resources file name not be shown.
See:
How can I solve this problem?
If you want to use it that way - don't add the image to the .resx file. Right click on the project -> Properties -> Resources -> Add Resource -> Add existing file...
Add the image there. If you do this you should be able to get it in the code through Properties.Resources.MyIcon
You can use Stream
Stream iconStream = Application.GetResourceStream(
new Uri( "pack://application:,,,/YourReferencedAssembly;component/YourPossibleSubFolder/YourResourceFile.ico" )).Stream;
notifyIcon.Icon = new System.Drawing.Icon(iconStream);
You just need to have an existing file, open the context menu on your folder , and then choose Add => Existing item...like this,name the folder Images
You need to change your image's property "Build Action" to Content and change the Copy to "If Newer" or "Always"
It will create a folder in bin\debug every time you build your application.
Then give your path like this #"Images\"
I have a C# project. I choose the .ico file for it in property so it automatically added the .ico into the list from the "solution explorer".
I am using a "notifyIcon", and want to change the icon programmatically.
example a notifyIcon red when program is busy and green when free.
So i know i have to add a new embed resource for the second ico. but how to access to the already existing one that is the application ico?
i would like "something like"
notifyIcon1.Icon = AppName.greenico.ico; //default app ico
notifyIcon1.Icon = AppName.redico.ico; //ico ill add as embed resource i guess
is that possible? i saw some strange ExtractIco thingy... But i am sure its possible to reference straight to something already embed ain't it?
Found a really really easy solution.
First: Change "Build" properties of the two icons in "Solution Explorer" to "Embedded Ressource".
now in your code just set the two icons as variables:
public Icon greenIco = new Icon(typeof(MainFormName), "GreenIco.ico");
public Icon redIco = new Icon(typeof(MainFormName), "RedIco.ico");
then to use them, easy so:
notifyIcon1.Icon = greenIco;
as simple as that.
Hope it'll help someone else a day.
I need to change the background image at runtime, based on which RadioButton the user clicks. I'm doing this in a WPF project in Visual Studio, and I need to put the code in the Checked event in the xaml.cs file
I have an Image control called imgBackground, with 6 images in its Source collection, which are listed in an Images folder in the Solution Explorer.
I've tried:
this.imgBackground.Source = "filename.jpg";
both with and without the quotes, and with various paths (I've tried too many different variations to list them all here) and nothing works - everything I've tried just gives an error in the editor, before I even try to build and run anything (the error given varies depending on what I'm trying at the time).
If you are using relative paths as filenames like
this.imgBackground.Source = "filename.jpg";
then these files must be in the same directory as the .exe of your program is.
One workaround would be to use absolute paths like
this.imgBackground.Source = #"C:\MyFolder\MyProject\filename.jpg";
Or, even further use the packaging mechanism of WPF or pack your images as resources into your assembly. Look at this thread.
EDIT:
For your clarification:
The Source-property demands an System.Windows.Media.ImageSource-object, which you must provide.
Do it like this:
BitmapImage bi3 = new BitmapImage();
bi3.BeginInit();
bi3.UriSource = new Uri("filename.jpg", UriKind.Relative);
bi3.EndInit();
this.imgBackground.Source = bi3;
Please refer to this documentation here.
I'm trying to use an icon that I've added as a resource as the image on a button. I know it's possible because I can do it in other projects through the designer. However, I'm trying to do this with code. I added the icon as a resource to my project by following the steps in the accepted answer to this question. The resource is named CancelButtonIcon.
Now, I'm trying to add that icon as the image on a standard button with this code:
this.CancelButton.Image = (System.Drawing.Image)Properties.Resources.CancelButtonIcon;
However, I get an error message:
Cannot convert type 'System.Drawing.Icon' to 'System.Drawing.Image'
In the code that Visual Studio automatically generates when I use the designer, it looks like this:
((System.Drawing.Image)(resources.GetObject("SaveButton.Image")));
which results from manually adding a resource through the Properties window. How can I convert this icon resource to an image so it can be used on the button? Adding it through the designer is not an option (this button is created programmatically and thus isn't present in the designer).
You can use the Icon.ToBitmap method for this purpose. Note that a Bitmap is an Image.
CancelButton.Image = Properties.Resources.CancelButtonIcon.ToBitmap();
Not sure why, but any time I tried using the accepted answer's approach, the .ToBitmap() call was giving me array index out of bounds exceptions. I solved this by doing it this way instead:
System.Drawing.Icon.FromHandle(Properties.Resources.CancelButtonIcon.Handle).ToBitmap();