Load image from resources area of project in C#
The best thing is to add them as Image Resources in the Resources settings in the Project. Then you can get the image directly by doing Resources.myimage. This will get the image via a generated C# property.
If you just set the image as Embedded Resource you can get it with:
string name = "Resources.myimage.jpg"
string namespaceName = "MyCompany.MyNamespace";
string resource = namespaceName + "." + name;
Type type = typeof(MyCompany.MyNamespace.MyTypeFromSameAssemblyAsResource);
Bitmap image = new Bitmap(type.Assembly.GetManifestResourceStream(resource));
Where MyTypeFromSameAssemblyAsResource is any type that you have in your assembly.
Load image from resources
You can always use System.Resources.ResourceManager
which returns the cached ResourceManager
used by this class. Since chan1
and chan2
represent two different images, you may use System.Resources.ResourceManager.GetObject(string name)
which returns an object matching your input with the project resources
Example
object O = Resources.ResourceManager.GetObject("chan1"); //Return an object from the image chan1.png in the project
channelPic.Image = (Image)O; //Set the Image property of channelPic to the returned object as Image
Notice: Resources.ResourceManager.GetObject(string name)
may return null
if the string specified was not found in the project resources.
Thanks,
I hope you find this helpful :)
Load Image from Resources/ResourceManager
Ended up moving the resources to Properties/Resources as suggested by Hans Passant, described in the comments to the original post.
C# - Loading image from file resource in different assembly
If the images you wish to use as Content
is in another assembly
, you must copy
them to the main projects directory
.
You can use a Build event to do this:
Right click project that contains images -> Properties -> Buil Events ->
edit post build to copy images to main project directory
.
Then you have to use it as
pack://application:,,,/ContentFile.xaml
(Or)
If you need it in subfolder
pack://application:,,,/Subfolder/ContentFile.xaml
Have a look at this hfor more information http://msdn.microsoft.com/en-us/library/aa970069.aspx
How to load listview with images from project resources
This is the what you can do without implementing some custom list:
private void Form1_Load(object sender, EventArgs e)
{
ImageList imageList = new ImageList {ImageSize = new Size(200, 200)};
Image img = new Bitmap(Properties.Resources.Your_Image);
imageList.Images.Add(img);
this.listView1.View = View.LargeIcon;
this.listView1.Items.Add(new ListViewItem { ImageIndex = 0 });
this.listView1.LargeImageList = imageList;
}
If you need more advanced functionality from your listview take a look at this.
Loading PictureBox Image from resource file with path (Part 3)
The path should be something like: "Images\a.bmp"
. (Note the lack of a leading slash, and the slashes being back slashes.)
And then:
pictureBox1.Image = Image.FromFile(@"Images\a.bmp");
I just tried it to make sure, and it works. This is besides the other answer that you got - to "copy always".
Related Topics
Take String Before and After 'First' Space Character
How to Determine Browser Window Size on Server Side C#
Getting User Input and Adding It to an Array At C#
C# Web API Sending Body Data in Http Post Rest Client
How to Format Number as Money Using Regex
Extract First Element from Json
Selenium.Webdriver.Chromedriver Slow to Launch - Why
Where Are the Properties.Settings.Default Stored
How to Open in Default Browser in C#
Linq to Find All Childs Related to Parents Id
Removing Elements from Json Based on a Condition in C#
Calling a Function from Another Class in C#/.Net MVC App
Call Method in Controller from View(Cshtml)
Thread.Sleep() Without Freezing the Ui
How to Check If an Object Has Changed
How to Check If User Input Is from Barcode Scanner or Keyboard