I want to add or edit my C# project resources from within the code. For example I have a string called myString in project resources. Now I want to change the value of this string:
MyProject.Properties.Resources.myString = "NewStringValue";
But the compiler gives an error that this property is read only. I also want to add an image by browsing the image and adding it to the project resources.
Any idea how I can do this?
Thanks in advance.
P.S: I am using Windows Form.
Project -> Properties -> Resources -> open the resx file -> select images. There you can see the images in your project.
You can add new images by dragging them into that area.
This code will store the chosen images and load the last stored image at startup.
You may have to make it Observable or do something to make all the PictureBoxes update their image.
public class DefaultPicture
{
private static string settings = "picture.settings";
private System.Drawing.Bitmap image = new Bitmap(settings);
public Bitmap Image
{
get
{
return this.image;
}
set
{
this.image = value;
this.image.Save(settings);
}
}
}
Related
This question already has answers here:
Set same icon for all my Forms
(8 answers)
Closed 1 year ago.
I am working on a project where my requirement is that i have to use a same icon on all my windows forms . I have atleast 50 windows forms and many more had to be added . So is there a way to set a default icon for all windows forms rathee than doing manually on each form page .
you have multiple solution for this problem
best idea would be to inherit from a common base-Form that sets the Icon in the constructor.
from : Form or : System.Windows.Forms.Form to : MyCustomForm
and then just change MyCustomForm icon with write this line in MyCustomForm Cunstrunctor
this.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
then you just change project icon from project properties > Application > Icon and Manifest > browse for a *.ico file and add it there.
this approach can change other property in all from for example font,size,anchor,...
If you have many forms like:
public class MyAppForm1 : Form {
...
}
Then instead of deriving from Form, then you create an intermediate MyIconForm:
public class MyIconForm : Form {
public MyIconForm() : base() {
this.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
}
}
Then you just have to update all your forms to:
public class MyAppForm1 : MyIconForm {
...
}
The short answer is no, there is no central location where you can set the icon to be used by all forms in your project. You have to do it yourself. There are a bunch of methods you can use, including using reflection to set the backing field for Form.DefaultIcon during program initialization.
First step: get an icon.
There are a couple of options here. You can load an icon from a file, a resource or an embedded resource (yes, two different types of resource). Or the application icon, but you said you don't have one.
For standard resources:
Open project properties
Click Add Resource, Add Existing File and browse to your icon.
Icon is added to Properties.Resources with a property named after the file.
For content files (distributed with the application):
Add icon file to project (Add Existing Item)
Set file's Build Action property to Content.
Set file's Copy to Output Directory to Copy Always or Copy if newer.
Load icon using new Icon(iconFilename).
For embedded resources:
Add icon file to project (Add Existing Item)
Set file's Build Action to Embedded Resource.
Use Assembly.CurrentAssembly.GetManifestResourceStream to open the resource as a stream.
Of the three I'd choose standard resource for most things.
Making it work 1: Form Constructor
Add the appropriate loading code to every form's constructor:
public partial class Form1 : Form
{
public Form1()
{
// Using standard resource method
Icon = Properties.Resources.FormIcon;
InitializeComponent();
}
}
Making it work 2: Base Class
This is a fairly simple option, assuming you have control over the source for all of your forms and are OK with going through all of them to change their base class. The base class simply sets the form's icon during construction:
public class DefaultIconForm : Form
{
// Using content file method
private static readonly Icon _defaultIcon = new Icon("FormIcon.ico");
public DefaultIconForm()
{
Icon = _defaultIcon;
}
}
public partial class Form1 : DefaultIconForm
{
public Form1()
{
InitializeComponent();
}
}
You'll need to change all of your forms and remember to inherit from DefaultIconForm for all future forms.
Making it work 3: modify Form.DefaultIcon
This one is a slightly nasty trick that relies on reflection and could fail on you at some point, but it means not having to change any other code in your application.
Open your Program.cs file and add this method to the Program class:
private static void SetDefaultFormIcon()
{
var field = typeof(Form).GetField("defaultIcon", BindingFlags.Static | BindingFlags.NonPublic);
// And for completeness, this is the Embedded Resource method
using (var stream = typeof(Program).Assembly.GetManifestResourceStream($"{Application.ProductName}.FormIcon.ico"))
{
var ico = new Icon(stream);
field?.SetValue(null, ico);
}
}
Now call SetDefaultFormIcon() from main() to initialize.
As written it works on both .NET Framework and .NET 5 WinForms applications. There's no guarantee that the defaultIcon hidden static field won't change in future, so be prepared for it to break at some point.
I am able to display image from Drawable folder by using
<image source="live.png"/>
But i don't know how to get image from other folder i create in Resource
Can somebody help me?
Android is very picky about where you can put images, so your best bet is to store your images in the common project. In this example I assume a standard Xamarin.Forms solution with the following projects: Foo, Foo.Android, Foo.IOS and Foo.UWP. Yours will obviously have different names, so you'll have to substitute the Foos...
All the following code will go into the common code project, Foo.
First, create a new folder called Extensions (just to keep your code tidy) and add the following class to it:
[ContentProperty(nameof(Source))]
public class ImageResourceExtension : IMarkupExtension
{
public string Source { get; set; }
public object ProvideValue(IServiceProvider serviceProvider)
{
if (Source == null)
{
return null;
}
var imageSource = ImageSource.FromResource(Source);
return imageSource;
}
}
Now add the namespace for this class to your markup:
xmlns:extensions="clr-namespace:Foo.Extensions"
Next, create a folder for your images, again in your common project, NOT in your Android project. You can create subfolders as well. Add your images and make sure that the build action for each image is set to Embedded Resource.
Now you can reference those images in your XAML like this:
<Image Source="{extensions:ImageResource Foo.Images.Subfolder.Bar.png}">
Note that you need to supply the full path of the image, including the project name (Foo in this case) and that folders are separated by dots, not slashes.
I am adding a file called citybase.png to my drawable folder when I set the src property of my ImageView to #drawable/citybase the image is not showing up in the design view. Inspecting the Resource.designer.cs class I realize that no reference is being generated for any files I add to the drawable folder(except for the default Icon of course).
public partial class Drawable
{
// aapt resource value: 0x7f020000
public const int Icon = 2130837504;
static Drawable()
{
global::Android.Runtime.ResourceIdManager.UpdateIdValues();
}
private Drawable()
{
}
}
I tried rebuilding and cleaning the project but that does not work. How can I fix this problem? (Xamarin Studio 4.2.3)
The drawable folder name should be in lowercase (in visual studio it always create with name 'Drawable' when creating project), also avoid using such symbols like '#', '#' and so on.
I have the following code skeleton for a Grasshopper component I am making. Grasshopper 3D is a plugin for Rhino 3D, a piece of architecture software. It's a graphical programming language. Anyways, below is a sample Abstract Class, in which I am adding a Bitmap icon to the component.
namespace HM_SettingsForm
{
public class HM_Settings : GH_Component
{
// Misc code
protected override Bitmap Icon
{
get
{
return HM_SettingsForm.Properties.Resources.heatmap;
}
}
// Misc code
}
}
With that said, I am getting the following error.
Here is my Resources folder:
In my case, I was getting the same Error. What I was doing was I had been adding the image to Resources folder. It was adding it, right. but not the definition. So then I double clicked to the Resources.resx in Properties window. (not the resources folder) then I dragged and droped the image into the Resources.resx window. So That image is copied to resources folder and its definition as well .
Hope it helps
Wow I am silly. I overlooked that I used HM_SettingsForm twice.
Simply doing: return Properties.Resources.heatmap; worked.
Got the same problem, resolved it by referencing the Image (in my case a ToolStripMenuImage) from this:
this.tsm.Image = global::ASIM_Formatieren.Properties.Resources.icon_help;
to this
this.tsm.Image = (System.Drawing.Bitmap)Properties.Resources.ResourceManager.GetObject("icon_help");
There is a Form with a ToolStrip. This ToolStrip contains a ToolStripButton. I want to assign an image to this button:
this.btnSaveFile.Image = Bitmap.FromFile("C:\\Work\\Icons\\png\\save.png");
It works only if there is save.png on specified path. Otherwise, I get an FileNotFound Exception.
If I created a Form via Form Designer, Visual Studio would create a code like this:
this.toolStripButton9.Image = ((System.Drawing.Image) (resources.GetObject("toolStripButton9.Image")));
toolStripButton9.Image here is not a real name. Visual Studio takse my file save.png and transform it into toolStripButton9.Image.
But I create a form programmatically, without Designer. And my question is how to assign an image to the ToolStripBotton programmatically?
I tried to add the image to the project, but it didn't help much. I have no idea how to make Visual Studio grab it and embed into my executable so that I wouldn't need this file on specified location.
In MSDN, I only see the solution like that:
this.toolStripButton1.Image = Bitmap.FromFile("c:\\NewItem.bmp");
But it doesnt' work as I told above. I understand there is a simple solution but don't see it. Could you please give me a hint?
In Visual Studio, Open your "Properties" folder in the solution explorer, then open the Resources.resx file and add a existing image file as resource. You can then use it programmatically via the Resource static class:
Image x = Resources.MyResourceImage;
A full example of the code I suggest:
using System.Windows.Forms;
using Testapplication.Properties;
namespace Testapplication {
public class Class1 {
public Class1() {
Form MyForm = new Form();
ToolStrip MyToolStrip = new ToolStrip();
MyForm.Controls.Add(MyToolStrip);
ToolStripButton MyButton = new ToolStripButton();
MyToolStrip.Items.Add(MyButton);
MyButton.Image = Resources.MyResourceImage;
MyForm.Show();
}
}
}
Don't forget to add a using to YourApps' Properties namespace. Your Resources.resx (.cs) file resides in that namespace and is used to provide strong-types object references like images. In your case, replace "MyResourceImage" with "save" (omit the quotes).
ps. A glance at the most important part of my Resources.designer.cs file:
internal static System.Drawing.Bitmap MyResourceImage {
get {
object obj = ResourceManager.GetObject("MyResourceImage", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
So you mean setting image from an embedded resource?
string res = "MyAssembly.Resources.toolStripButton9.Image";
Stream s = this.GetType().Assembly.GetManifestResourceStream( res );
Icon icon = Icon.FromStream( s );
Use Webleeuws answer if it works, way easier than this :P