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;
Related
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 change the background of a button manually in my WPF app.
I have an image imported into my resources and I want to do:
MyButton.Background = MyProject.Properties.Resources.myImage;
But I get the error:
cannot implicitly convert system.drawing.bitmap to media.brush
How can I do this??
You should read about brushes first here.
And then use ImageBrush, something like this:
MyButton.Background = new ImageBrush(...);
(or, maybe, put the brush into resources...)
UPDATE
You can find how to create imageSource from bitmap easilly. for example, here. Like:
var bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(MyProject.Properties.Resources.myImage.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
MyButton.Background = new ImageBrush(bitmapSource);
In a WPF application, you do usually not add image resources as you did in WinForms.
Instead you add the image file directly to your Visual Studio project, just like any other file. If there are multiple images, it may make sense to put them in a subfolder of the project (e.g. called "images"). The Build Action of that file has to be set to Resource (which is the default for image files).
Now you can create a BitmapImage from a Pack URI to that file.
Finally you create an ImageBrush from the BitmapImage to set the Background property.
var uri = new Uri("pack://application:,,,/images/myImage.jpg");
var image = new BitmapImage(uri);
MyButton.Background = new ImageBrush(image);
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
I have a problem trying to show some images on my winform.
On one form, I have an wpf container, which has a WPF control that has no problem to load images from an external exe (that have the images as resources), which references the dll that contains the form, with the wpf container, that shows them.
Now, I want to add another winform, and I need to show there, the same images that are shown using the wpf container, but I cannot add a wpf container to this form, because I need to show the images on a combobox.
How can I load this images using URI pack, or how I turn this uri to something that I can use from my winform.
example uri.
pack://application:,,,/myPack;component/Images/image.png
What you want to do is read the image data for use in Winforms, so you need direct access to the embedded resource image file, which can be done thusly:
Uri uri = new Uri("pack://application:,,,/myPack;component/Images/image.png", UriKind.RelativeOrAbsolute);
StreamResourceInfo info = Application.GetContentStream(uri);
System.Drawing.Image myImage = System.Drawing.Image.FromStream(info.Stream);
Edit: If you get an exception about invalid port, make sure you've registered the pack scheme, which you can do merely by referencing it. So put this line of code before the above:
string s = System.IO.Packaging.PackUriHelper.UriSchemePack;