I got about 100 images loaded in resx and was wondering if it was possible to add them to a stream and if it is how to do it.
My idea is this:
I got a combobox and from selecting an item from the combobox (same names as the images) it should load an image from resx to a picturebox.
I tried this example:
string SelectedComboboxItem = comboBox1.SelectedItem.ToString();
pictureBox1.Image = Properties.Resources.ResourceManager.GetObject(SelectedComboboxItem));
Which does not work since it can not convert object to system.drawing.image, same with .GetString & .GetType.
Or if there is any other way to do this then it would be very helpful!
You must cast object to your image type. Like so:
pictureBox1.Image = (Image)Properties.Resources.ResourceManager.GetObject("RowNameInResourceFile"));
For more examples look at this msdn page
Related
A single image can be initially loaded from an application subfolder into a Windows form to appear as the BackgroundImage by specifying name and image format by file extension.
However I can't figure out a way to support different image formats with identical names and effectively handle multiple images in my subfolder which have the same name, but different image file extensions.
I want that any supported Bitmap image called map_default(.) will be loaded initially as BackgroundImage if there is a single image with this name in the subfolder and that one image will be picked randomly if there are multiple images in the subfolder like this:
application1.exe
defaultmap(folder):
-map_default.png
-map_default.gif
-map_default.bmp
-map_default.tiff
-map_default.jpeg
I saw many methods to modify path expressions, but unfortunately there seems no way to define the name of an image and specify different file extensions for it in one path expression.
On the other side it seems that I must specify an extension for an image.
I need to know the right path expression and a way to check if there is only one image in the array or several images(in this case pick one item from the array).
How could I possibly solve this?
Well, I don't have an idea for the random picking, but I my problem is always that I don't know how to read the image without specifying an extension when I load it into the Form initially.
There are many working solutions with file selector and drag and drop, but I'm unsure what to do when I load the image automatically when the Form runs:
BackgroundImage = Image.FromFile(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), #"defaultmap\map_default.png"));
Can I use System.IO.Directory.GetFiles path expression with any extension?
Something like this.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//declare path to images directory
string path = #"C:\MyPath\images\";
//fill an array with file names.
string[] images = Directory.GetFiles(path);
if (String.IsNullOrEmpty(images[0]))
{
//set background to hex code color
}
else if (String.IsNullorEmpty(images[1]))
{
//set background to images[0]
}
else
{
//set background to random array value for images
}
}
}
If you just refuse to have a dedicated directory for images you can check for each value in the array ends with .png, .jpg, .bmp, etc.
How can I assign an image to an ImageView in Xamarin?
To do this before what I did was to add the images to "drawable" and from design mode (Designing Layout) I select the image in the "src" field, leaving something like this:
src | # drawable / splashlogo
and if I wanted to do it from code at runtime I just put:
var drawableImage = Resources.GetDrawable (Resources.GetIdentifier ("splashlogo", "drawable", PackageName));
ProfileIcon.Background = (drawableImage);
The problem with this method is that I started having memory consumption problems because the resolution of the images was very high, and now that I am using the folders intended for each screen density (mdpi, hdpi, xhdpi ...) I don't know how assign the images neither at runtime nor in the layout design.
Could someone tell me how to do it please?
as Cheesebaron said,Android will automatically pick the correct image based on the display density from your mdpi, hdpi, xhdpi folders.if you want to set Image of your mipmap in you code-behind,you could try this:
ProfileIcon.SetImageResource(Resource.Mipmap.splashlogo);
or
var drawableImage = Resources.GetDrawable(Resources.GetIdentifier("splashlogo", "mipmap", PackageName));
ProfileIcon.SetImageDrawable(drawableImage);
Nothing should change. Android will automatically pick the correct image based on the display density.
Also when assigning image from code-behind instead of in layout. Consider just passing along the Resource Id to the ImageView, instead of first loading the drawable into memory, then pass it along to the ImageView. This can simply be done like:
ProfileIcon.SetImageResource(Resource.Drawable.splashlogo);
Otherwise, if you insist on loading it from resources first, I highly suggest you put it in a using statement to dispose of the managed side:
using(var drawableImage = Resources.GetDrawable(Resources.GetIdentifier ("splashlogo", "drawable", PackageName)))
ProfileIcon.Background = (drawableImage);
An image is added to the imageList1 using this code:
imageList1.Images.Add("pic1", Image.FromFile("D:\\pic\\ha-i247.jpg"));
How can we get the full path of each image that added to the imagelist? (in this case: "D:\pic\ha-i247.jpg")
INFO: I know that the references can be kept using for example a list, I wondering about the capabilities of imageList itself.
You actually can't do that. Image list doesn't store where image came from. You can put path instead of name:
imageList1.Images.Add("D:\\pic\\ha-i247.jpg", Image.FromFile("D:\\pic\\ha-i247.jpg"));
And if you want to get it from there you can do something like this:
ImageList1.Images.Keys[0].ToString();
But it is not the best solution, better to store it outside the image list
Yes, you can do this. The Images property is of type ImageCollection, which is a dictionary of Image objects. The Image object supports the Tag object property in which the programmer can store any information that want to.
Your code could read;
Image img = Image.FromFile(fileName);
img.Tag = fileName;
imageList.Images.Add('Img1',img);
You can get the file name by using
imageList.Images['Img1'].Tag;
I want to render a html-page with the C#-Webbrowser Form.
Normally I receive the html file from another application. For simplicity I just read the html page from the hard drive into a stream and then I set the webBrowserControl to this content.
That works in general. But now I want the html file to reference to images in the imageList.
I don't want to save the images to the hard drive.
Is there any possibilty to reference images in RAM with HTML.
The common way like
<img src="C:\\pic.png"/>
is obviously not possible.
Explanation Code
Image image = Image.FromFile(src_pathfile); //normally from another application over interface
List<Image> imageList = new List<Image>();
imageList.Add(image);
Stream source = File.OpenRead("C:\\Webpage.html"); //from another application
webBrowser.DocumentStream = source;
Thank you for your help in advance.
magicbasti
You can encode the image as base-64 and store it in the <img> tag itself. There's some information about it here.
I'm using the Chart control from the DataVisualization library, and want to use image annotations on my chart. The problem is that the ImageAnnotation.Image property is a path to the image, and there doesn't appear to be an exposed Bitmap property that I could use to load the image from the Resources object like I can for any other .net control.
Is there anyway I'm overlooking to load this using embedded resources instead of reading a separate file off the disk?
I found the answer. You need to add the image to the parent Chart's NamedImage collection.
private string imageName = "myImage";
//in constructor
NamedImage namedImage = new NamedImage(imageName, Properties.Resources.myImage);
mChart.Images.Add(namedImage);
//where creating the annotation
ImageAnnotation imageAnnotation = new ImageAnnotation();
imageAnnotation.Image = imageName;