URI
In This Topic
URI Overview
A uniform resource identifier (URI) is a string of characters used to identify a name of a resource. URIs are used for referencing and identifying resources over a network, typically the World Wide Web, using specific protocols. Each URI is defined by a scheme specifying a concrete syntax and protocol, for example "http" scheme, "ftp" scheme, "mailto" scheme, "data" scheme and so on. The most commonly used URI types are the Uniform Resource Locator (URL), informally known as web address and the data URI, which is typically used to embed binary data like images in text based documents (HTML, XML, etc.). Nevron Open Vision provides a helper class (NUriHelpes) with various useful methods for working with URIs, but before looking at them in details, let's see what an URI is made of.
URI Structure
Generic URI Syntax |
Copy Code
|
<scheme name> : <hierarchical part> [ ? <query> ] [ # <fragment> ]
|
Here's an example to a specific web resource:
URI Example |
Copy Code
|
https://helpopenvision.nevron.com/URI.html#i-heading-uri-structure
|
The given example defines the following URI parts:
- Scheme: https
- Hierarchical path
- host: www.nevron.com
- path: uri.html
- Fragment: i-heading-uri-structure
This information instructs an application that the file"uri.html" is located in the given path at the specified host and can be accessed through the "HTTPS" protocol. The fragment part in the context of web pages is typically used to denote a specific section of the page, in this case the "uri-structure" section. This is an absolute URI, i.e. an URI which fully defines a resource. You may also meet relative URIs, which define only a part of the path to a resource and only have a meaning in the context of an absolute URI, called base URI. In order to access the resource the relative URI should be combined with the base URI to produce a new absolute URI, a process known as URI resolution.
Creating and working with URIs
In your applications you will generally receive URIs as strings. To create and work with URIs, use the standard System.Uri class, which is included in the .NET Framework. If you need to work with data URIs, you can use the NUriHelpers static class provided by Nevron Open Vision (NOV). It contains the following useful methods:
- IsData - checks whether an Uri is a data URI.
- GetData - returns the byte array of a data URI.
- CreateDataUriString - creates a base64 data URI string from an image (NImage) or a byte array. You can also pass an image format (for an image) or a MIME type (for a byte array). The following example shows how to create a data URI from an image:
Creating a data URI from an image |
Copy Code
|
string dataUriString = NUriHelpers.CreateDataUriString(image);
string htmlImage = "<img src=\"" + dataUriString + "\" />";
|
If you want to create an image from a data URI, use the NImage.FromDataUri method:
Creating an image from a data URI |
Copy Code
|
NImage image = NImage.FromDataUriString(dataUriString);
|