I have a image-path that must be inserted into following code:
Uri uri = new Uri(#"C:\Users\user\Desktop\dir\private- name\Easy\Easy_v\Easy_v\Attempts\image.jpg");
BitmapImage img = new BitmapImage(uri);
When I need to change this path, I'm trying to get path with a function I've written, but when I inserted the full path into the uri, this character: \ has been replaced with /
Any way to get the correct image path, and replace it with the one above?
Uri class has a property called
uri.LocalPath
this is the fullpath name expressed according to the local system preferences
of course you can apply the normal Path methods to this property
Console.WriteLine(Path.GetDirectoryName(uri.LocalPath));
See MSDN docs
Related
I tried to create a URI like this
Uri uri = new Uri("file:///Pages/Menu/Page.xaml");
So I have a WPF project and its path is like this: C:/Programing/MyProjectName/
Now the path to my file are C:/Programing/MyProjectName/Pages/Menu/Page.xaml
as you saw above the path I give to the Uri is file:///Pages/Menu/Page.xaml when I run this I get the error can't find the part of the path C:/Pages/Menu/Page.xaml which I expect because as I said above my files path is C:/Programing/MyProjectName/Pages/Menu/Page.xaml
so my question is, is there any way to use a relative path in my Uri, I want to use a relative path because my app will not always be in the same folder.
Incase your wondering why I'm using a Uri is because I'm setting the Source property on a WPF frame and it uses Uri instead of string.
In order to load a Page, use a Resource File Pack URI:
var uri = new Uri("pack://application:,,,/Pages/Menu/Page.xaml");
Which could also be written like this:
var uri = new Uri("/Pages/Menu/Page.xaml", UriKind.Relative);
I have an XmlTextReader to read series of XML files to load some information in to my program.
However, in some XML files I have the file name of an image and I want to load that image.
But the problem is the XML file does not have the full path of the image.
<Image id="ImageId" File="Image.bmp" />
<!-- full path is not available. Image is behind XML-->
This means the Image exist where the xml file exists.
for some reason, the only way to get the path of the XML file is to get the path of the XmlTextReader reading the current XML file.
I did some research and I found out you can retrieve the XML path from the XmlTextReader as below:
string path = reader.BaseURI; // this will get the path of reading XML
// reader is XmlTextReader
How can I combine path with the image's path?
I have tried the following way:
string FullImagePath = Path.Combine(reader.BaseURI, imagePath);
These are the values of the variables:
reader.BaseURI is "file:///D:/.../currentXml.xml"
imagePath is "Image.bmp".
Finally, FullImagePath, after assigning the result of Path.Combine is file:///D:/.../currentXml.xml\\Image.bmp, which is not what I expect.
Expected path of the image is: D:/.../Image.bmp, in the same directory as currentXml.xml.
So how can I get the path of the image file?
You have a two different problems that you need to solve separately.
Depending on the API used to consume the image file, a file:// URI path may or may not be supported. So you'd want to make that a local path as explained in Convert file path to a file URI?:
string xmlPath = "file://C:/Temp/Foo.xml";
var xmlUri = new Uri(xmlPath); // Throws if the path is not in a valid format.
string xmlLocalPath = xmlUri.LocalPath; // C:\Temp\Foo.xml
Then you want to build the path to the image file, which resides in the same directory as the XML file.
One way to do that is to get the directory that file is in, see Getting the folder name from a path:
string xmlDirectory = Path.GetDirectoryName(xmlLocalPath); // C:\Temp
Then you can add your image's filename:
string imagePath = Path.Combine(xmlDirectory, "image.png"); // C:\Temp\image.png
Or, in "one" line:
string imagePath = Path.Combine(Path.GetDirectoryName(new Uri(reader.BaseURI).LocalPath),
ImagePath);
Path.Combine(Path.DirectoryName(reader.BaseUri), imagePath)
As you are dealing with resolving URLs I would suggest to use XmlUrlResolver in System.Xml:
string localPath = new XmlUrlResolver().ResolveUri(new Uri(baseUri), imageName).LocalPath;
I am making a simplePlayer WindowsForm. In order for the video to play i need to provide the url
axWindowsMediaPlayer1.URL = #"C:\Users\Stephan\Desktop\trasa-1250.wmv";
Now i need to either use relative paths or add them as a resource and get the url for that resource but i don't know how to do that:
wplayer.URL = Resources.trasa_1250.
I've tried using
axWindowsMediaPlayer1.URL = #"~\trasa-1250.wmv";
and
axWindowsMediaPlayer1.URL = #".\trasa-1250.wmv";
but printing #"~\trasa-1250.wmv"; and #.\trasa-1250.wmv"; prints them as they are without replacing the ~ or the .
To get the absolute path for a file, by supplying a name relative to the current directory, you can use:
string filename = "trasa-1250.wmv";
string path = Path.GetFullPath(filename);
For completeness sake, to create an actual Url from this:
string url = new Uri(path).AbsoluteUri;
You can't create a Url to an embedded resource, unless you program the player to accept a custom Url scheme (to allow, for instance, "resource://assemblyName.namespace.resourceName") and process it correctly.
A common alternative is to let the caller provide the Stream from which to read the media - and let them decide on how to access the resource.
Here is a tutorial that will help:
https://www.youtube.com/watch?v=nF-HYoTurc8
You could also get the URL like this:
Uri MyUri = new Uri(#"/Resources/trasa-1250.wmv", UriKind.Relative);
I have url like http://xxx.xxx.xxx/mls/pmmls/12/-8/53/6/12-8536_2.jpg/t1349940727/100x100/
and need to get only filename from url like "12-8536_2.jpg"
url format is dynamic. Filename with extension must be in url. but it filename with extension may not be in last position of url
I have tried Path.GetFileName() but it give "".
is anyone know how extract filename for this type of url?
12-8536_2.jpg does not seem to be a file in that URL. In any case, if the "filename" in the URL will always be in .jpg, you can output the URL to a string (or AS a string) and Regex for it:
string filename = Regex.Match(URL,#"\/([A-Za-z0-9\-._~:?#\[\]#!$%&'()*+,;=]*).jpg").Groups[1].Value
EDIT: I'm thinking this is for a site with different preview sizes for a specific file. You can also specify the different possible extensions as follows (for example):
string filename = Regex.Match(URL,#"\/([A-Za-z0-9\-._~:?#\[\]#!$%&'()*+,;=]*)(.jpg|.JPG|.jpeg|.JPEG)").Groups[1].Value
There is no guarantee that any part of a URL maps to a file, so it does not make sense to try to get the FileName in a URL.
If you know the filename will always be followed by two further segments (in your example, t1349940727 and 100x100), you could do
var input =
"http://xxx.xxx.xxx/mls/pmmls/12/-8/53/6/12-8536_2.jpg/t1349940727/100x100/";
var uri = new Uri(input);
var fileName = uri.Segments[uri.Segments.Length - 3];
If you don't know that, then as others have said, there's no easy way to tell which part is the filename. You could try
var fileName = url.Segments.Last(seg => seg.Contains("."));
to get the last segment with a dot in.
You should define a list of extensions, like .jpg, .png .gif (all files types you are expecting).
Convert your url to string (if it isnt already) and try to find the index of the extension. You do now know the position of the file name and whether there is an file name. remove everything after the file name.
now find a "/" token, and remove the part before (and including) the "/" , repeat this untill you can no longer find "/" (for example with a while function).
more information on how to do this can be found here
static string getFileName(string url) {
string[] arr = url.Split('/');
return arr[arr.Length - 1];
}
once i click button, my image1 will display an image;
when i set my imagePath to
Server.MapPath("~/SME IMAGE/anonymous_avatar.gif");
my url not work
but when i set my path to
"~/SME IMAGE/anonymous_avatar.gif"
my url work
so basically what happen to my Server.MapPath?
try
{
string imagePath = Server.MapPath("~/SME IMAGE/anonymous_avatar.gif");
Image1.ImageUrl = imagePath;
Response.Write("success");
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
Server.MapPath it translates the "web" path into your local path.
So Server.MapPath("Server.MapPath") it will give the local location of your file.
You are assigning this to the ImageUrl property, but this will be translated to something like "www.mywebsite.com/C:\wwwroot\somefile.gif"
Try not to set the Server.MapPath - just use:
Image1.ImageUrl = "~/SME IMAGE/anonymous_avatar.gif";
Maybe make sure the url exists, and maybe add %20 instead of the space in the path. (Also try to avoid spaces in paths)
Server.MapPath returns the physical file path that corresponds to the specified virtual path on the Web server. So when you setting
string imagePath = Server.MapPath("~/SME IMAGE/anonymous_avatar.gif");
it could return something like C:\Inetpub\wwwroot\website\SME IMAGE\anonymous_avatar.gif which is actually a physical path to the file.
Image.ImageUrl property accepts the URL of an image to display in the Image control. You can use a relative or an absolute URL. MSDN says:
A relative URL relates the location of the image to the location of
the Web page without specifying a complete path on the server. The
path is relative to the location of the Web page. This makes it easier
to move the entire site to another directory on the server without
updating the code. An absolute URL provides the complete path, so
moving the site to another directory requires that you update the
code.