WPF Dispay image from relative location - wpf

I would like to display an image on my dialog which could be located in a directory relative to the one the .exe is located in, e.g.
project
- data
-- logo //<-- that's where the image is located
-bin //<-- that's where the .exe is in
A default image should be included in the .exe but on displaying the dialog, the \data\logo directory should be checked first and if an image with the given filename could be found there that one should be used for display instead of the one that is inside the .exe.
Any ideas on how to do this?
Thanks,
tabina

Use the pack URI
pack://application:,,,/ReferencedAssembly;component/data/logo/image.png

As he asks for a folder relative to the exe, i suggest he means the logo should be located inside the filesystem (e.g. c:\program files\MyApp\data\logo). So I would check if the file exists with
File.Exists("/data/logo/logo.png")
If it returns false, you could load your embedded resource.
//Edit
If you use this snippet in your Visual Studio IDE the path is located at
<Project directory>/bin/debug/data/logo

what I am doing now is
// set the logo
string path = Environment.CurrentDirectory + "\\data\\logo\\logo.gif";
var uri = new Uri(path, UriKind.Absolute);
try
{
this.myImg.Source = new BitmapImage(uri);
}
catch (Exception e)
{
this.myImg.Source = new BitmapImage(new Uri(#"pack://application:,,,/myAssemblyName;component/Images/logo.gif"));
}
This works pretty well, but what I don't like about this is that it is all written in the code-behind. I would rather have it more re-usable.
Is there a way to put it all into a template and apply it to viewboxes/images?
Thanks,
tabina

Related

Visual Studio 2010 Project - can't adjust the relative path

I have a Windows Forms project. I have a Resources folder and I wan to use the files there using relative path. Here is a printscreen of my project tree
As you may see I have folder UserControls where I have FileExplorer.cs it contains aa openFileDialog + pictureBox. I use this control in some of my forms which are in Forms folder. The case is that in Resources folder I have this T380.jpg image that I want to load by default but for now I can do it only by inserting the full path to it. Here is my code where I try to load the image:
private void FileExplorer_Load(object sender, EventArgs e)
{
pictureBox1.ImageLocation = #"ShoesUnlimitedAdmin\Resources\T380.jpg";
pictureBox1.Load();
}
I use the Load event of the user control to load my image but it only works when I set the full path to the image like C:\\... and so. How can I point to the Resources folder of the project using relative path?
If these images are small then favor adding them as resources in the executable file so you can use Properties.Resources in your code and don't have to deploy the files on the user's machine. Use Project + Properties, Resources. Click the arrow on the "Add Resource" button and select Add Existing File.
If they are big (more than a couple of megabytes) then you'll indeed want to deploy them as separate files. You can find them back by using the location of the EXE program, here's a helper method, spelled out for clarity:
public static string GetResourcePath(string filename) {
string exepath = System.Reflection.Assembly.GetEntryAssembly().Location;
string exedir = System.IO.Path.GetDirectoryName(exepath);
string resdir = System.IO.Path.Combine(exedir, "Resources");
return System.IO.Path.Combine(resdir, filename);
}

How to create Uri pointing to an local image in Silverlight?

I was wondering if it is allowed to read local images within a Silverlight client. (it is just to test a control)
var image = new BitmapImage { UriSource = new Uri("../Images/2.jpg", UriKind.Relative) };
Would this Uri be allowed and the image created? For some reason the picture doesn't show, despite getting no compiler errors that the Uri might have been wrong...
Thanks for advice,
kave
Assuming your project looks like this
- Project
- Images
- 2.jpg
- Another_Folder
- *your code file*
, that should be fine.
Remember that
../Images/2.jpg
is navigating to parent folder, then looking for an Images folder and a 2.jpg file.

Image Resources in WinForms

I want to store images in a .dll file and use them for my winform application. but Im not able to load .dll content
System.IO.Stream stream;
System.Reflection.Assembly assembly;
Image bitmap;
assembly = System.Reflection.Assembly.LoadFrom(Application.ExecutablePath);
stream = assembly.GetManifestResourceStream("global::template.Properties.Resources.test.png");
bitmap = Image.FromStream(stream);
this.background.titleImage = bitmap; // image box
The most likely problem is that the path you are specifying in GetManifestResourceStream() is not correct.
I find the following helpful to debug these issues:
Open assembly that has the resource embedded into it, in Reflector.
Open the Resources folder.
From the list of resource paths\folders, find the one with your resource in it.
If you cannot find your resource, did you mark it as an embedded resource?
Your resource path will be "Resource folder" + "." + "resource name".
e.g. "PrismDemo.Properties.Resources.resources.app.ico"
You are using the wrong name, "global::" doesn't appear in the manifest resource name. A typical name would be project.test.png if you added the resource to the project and set its Build Action to "Embedded Resource". To avoid guessing at the name, use Ildasm.exe and find the .mresource in the manifest.
However, your name suggests you added the resource in the Project + Properties, Resources tab. Good idea, that auto-generates a property name for the resource. You'd use project.Properties.Resources.test to reference it. You'll get an Image back, no need to use GetManifestResourceStream.
The project name I listed above is the default namespace name you assigned to the project. Project + Properties, Application tab, Default namespace setting.

Silverlight image: load URL dynamically?

I'm tinkering with Silverlight 2.0.
I have some images, which I currently have a static URL for the image source.
Is there a way to dynamically load the image from a URL path for the site that is hosting the control?
Alternatively, a configuration setting, stored in a single place, that holds the base path for the URL, so that each image only holds the filename?
From what I gather you aren't trying to change the image itself dynamically, but rather to correctly determine the location of the image at runtime.
I believe simply prefixing the image relative URL with "../" should get you to the root of your application, not necessarily the site as the application might not be hosted in the root of a site.
If your XAP file is located as follows:
http://somesite.foo/app1/somethingelse/clientbin/MyFoo.xap
And you where trying to link the following image:
http://somesite.foo/app1/somethingelse/images/a/boo.png
Apparently all relative URI's are relative to where the XAP file is located (ClientBin folder typically) and Silverlight appends the current Silverlight client namespace. So if you Silverlight control is in the namespace Whoppa you would need to put all your images in the clientbin/Whoppa/ directory. Not exactly convenient.
The workaround is to use absolute URIs as follows:
new Uri(App.Current.Host.Source, "../images/a/boo.png");
In the code behind or a value converter you can do
Uri uri = new Uri("http://testsvr.com/hello.jpg");
YourImage.Source = new BitmapImage(uri);
// create a new image
Image image = new Image();
// better to keep this in a global config singleton
string hostName = Application.Current.Host.Source.Host;
if (Application.Current.Host.Source.Port != 80)
hostName += ":" + Application.Current.Host.Source.Port;
// set the image source
image.Source = new BitmapImage(new Uri("http://" + hostName + "/cute_kitten112.jpg", UriKind.Absolute));
http://www.silverlightexamples.net/post/How-to-Get-Files-From-Resources-in-Silverlight-20.aspx
using System.Windows.Resources; // StreamResourceInfo
using System.Windows.Media.Imaging; // BitmapImage
....
StreamResourceInfo sr = Application.GetResourceStream(new Uri("SilverlightApplication1;component/MyImage.png", UriKind.Relative));
BitmapImage bmp = new BitmapImage();
bmp.SetSource(sr.Stream);
SilverlightHost.Source will provide you the URL that was used to load the XAP file. You can use this to then construct a relative URL for your images.
So if for example your XAP is hosted on http://foo.bar/ClientBin/bas.xap and your images were stored in http://foo.bar/Images/ you can simply use the Source to grab the host name and protocol to construct the new URI.
img.Source = new BitmapImage(image uri) must work.
img.Source = new BitmapImage(new Uri("/images/my-image.jpg", UriKind.Relative)); will properly resolve to the root of the Silverlight application where as "../images/my-image.jpg" will not.
This is only true in the code-behind when dynamically setting the source of the image. You cannot use this notation (the "/" to designate the root) in the XAML (go fiquire, hope they fix that)
The below code worked for me only when the image is included in the project as a resource file:
img.Source = new BitmapImage(new Uri("/images/my-image.jpg", UriKind.Relative));
I am unable to access URL from absolute URLs. Not even Flickr's farm URL for images.

silverlight...hyperlinkbutton to file using relative path

I am trying to use a hyperlinkbutton in silverlight to enable the user to download a word document. I don't care if a file save as box appears or if the word doc opens in a new browser. I get the error "cannot navigate to locations relative to a page." I've seen it posted that you can do this with the absolute path (www.domain.com/filename.doc) but there's got to be a way to make this relative (/docs/filename.doc). Anyone know how?
Slightly easier:
Uri myAbsoluteUri = new Uri(HtmlPage.Document.DocumentUri, myRelativePath);
The HyperlinkButton only works with absolute URLS, so you should fixup your URLs at runtime:
uriCurrent = System.Windows.Browser.HtmlPage.Document.DocumentUri;
string current = uriCurrent.OriginalString;
int iLastSlash = current.LastIndexOf('/') + 1;
current = current.Remove(iLastSlash, current.Length - iLastSlash);
from Silverlight.net forums.

Resources