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;
Related
So I built a WPF user control as part of a larger solution. I'm building another app where this user control would work nice. I added a reference to the DLL from the new app, wired it up and compiled. The window loads and I can see the image that it's telling me it can't find. After the main window displays and the user control is populated, an exception is thrown saying...
System.IO.IOException: 'Cannot locate resource 'resources/nosortnofilter.png'.'
The user control is a DataGrid with some extensions added to it. The column it threw on was "id". As you can see in the image, the red arrow shows the nosortnofilter.png image being displayed in all columns. So why is it throwing this exception?
The line of code it throws on is here.
If ImageName = "" Then ImageName = "NoSortNoFilter"
img.Source = New BitmapImage(New Uri("pack://application:,,,/Resources/" & ImageName & ".png"))
So it all looks good from my perspective. Hoping someone can see what I'm not seeing.
EDIT: Found a solution. This works. But it still doesn't answer the questions why the original pack:// formatted URI only worked with the original solution.
img.Source = New BitmapImage(New Uri($"Resources/{ImageName}.png", UriKind.Relative))
EDIT: Thanks to rfmodulator for giving me the correct URI for DLL's.
img.Source = New BitmapImage(New Uri("pack://application:,,,/AdvancedSortFilterDataGrid;component/Resources/" & ImageName & ".png"))
This is the URI of a resource in the Application's assembly:
pack://application:,,,/Resources/[RESOURCENAME]
To get a resource in a DLL, the URI looks more like this:
pack://application:,,,/[DLLNAME];component/Resources/[RESOURCENAME]
For details, see Pack URIs in WPF.
Hi I am trying to build an app using xamarin forms PCL. I am implementing image gallery in which I have used a default image. All the images are on blob. I want to download image and cache that Image in device and as soon as download is complete I need to replace my default image with it.
And on loading app next time download image only if it is not present in cache.
I dont get any plugin for image caching and loading image from cache.
I have seen a plugin named FFPLUGIN but it didnt work.
Any idea how I can implement this? IMAGE CACHING
You could use the built in ImageCaching in Xamarin Forms shown here:
https://developer.xamarin.com/guides/xamarin-forms/working-with/images/#Downloaded_Image_Caching
Downloaded Image Caching
UriImageSource also supports caching of
downloaded images, configured through the following properties:
CachingEnabled - Whether caching is enabled ( true by default).
CacheValidity - A TimeSpan that defines how long the image will be
stored locally. Caching is enabled by default and will store the image
locally for 24 hours. To disable caching for a particular image,
instantiate the image source like this:
Image.Source = new UriImageSource {CachingEnabled = false,
Uri="http://server.com/image"}; To set a specific cache period (for
example, 5 days) instantiate the image source like this:
webImage.Source = new UriImageSource {
Uri = new Uri("https://xamarin.com/content/images/pages/forms/example-app.png"),
CachingEnabled = true,
CacheValidity = new TimeSpan(5,0,0,0) };
Built-in caching makes it very easy to support scenarios like scrolling lists of images, where
you can set (or bind) an image in each cell and let the built-in cache
take care of re-loading the image when the cell is scrolled back into
view.
I was wondering how you, on WP8, can navigate to / open an image?
Suppose I just saved an image to my Save Pictures folder, I now want to navigate the user to that image so they can share it on facebook.
Lets say I have:
MediaLibrary lib = new MediaLibary();
Picture p = library.SavePicture("foo.jpg", imgStream);
How can I navigate to p as if the user had clicked it from the picture library?
A simple method for navigate into image is related to Efficient image manipulation in C#. I think that you need an framework for precessing like AForge.NET.
You could always try passing the url of the image as a string to a page that has nothing on it except an "Image". Then as soon as you navigate to the page you open up the image from the url(url can be for an isolatedstorage file) and set it as the "Image" on the page.
Hi there
I am working with visual web developer and would a) like to know how I could programatcally add a picture to the website, instead of going to Website and then add existing item. The idea I have is to upload the picture using the file upload control but then I want to have it added so that it can be accessed using a gridview.
b)I would also like to know how to size the picture using C# code obviously so that is displayed at the correct size.
best regards
arian
here is a full project with source code to manipulate images, including resize.
http://www.codeproject.com/KB/web-image/ASPImaging1.aspx
And a sample code only for resize
http://weblogs.asp.net/gunnarpeipman/archive/2009/04/02/resizing-images-without-loss-of-quality.aspx
You can use GetThumbnailImage to easily create a smaller verson of the uploaded image. The code looks something like (it's free typed without a compiler, so there may be some errors):
System.Drawing.Image pic = new System.Drawing.Bitmap(sourceFilename);
System.Drawing.Image thumb = pic.GetThumbnailImage(targetXSize,targetYSize,
new System.Drawing.Image.GetThumbnailImageAbort(this.GetThumbnailImageAbort),
IntPtr.Zero);
thumb.Save(thumbPathName);
I believe the Image implements IDisposable, so you need to remember to Dispose of them when you're done.
The abort parameter can be a function that simply does this (can't remember off the top of my head when it gets called):
bool GetThumbnailImageAbort() {
return false;
}
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;