Reserved characters in the display of WPF images.

Source: Internet
Author: User

Reserved characters in the display of WPF images.

Displaying an image in WPF is a simple task. One image and one line of XAML code.

However, a strange thing happened some time ago:

Normal programs run on the development machine, but images cannot be displayed on some client machines. In addition to this problem, other operating conditions are normal. Check the code first, and then check the compilation and packaging process. No problems are found. Then, go to the customer's machine to test whether the image in the corresponding path exists, whether there is a problem with reading and writing the image path, or no problem is found. Finally, we found that when the program is moved to another path, everything is normal. The Error Path is D :\\ C #\\. This is what we want to talk about today. Some special characters (or reserved characters) affect the image display.

First, let's take a brief look at the two common image resource storage methods used to display images in WPF: Resources and content. Resources are compiled into exe or dll. The advantage is speed and simplicity. The content is an independent file explicitly associated with the exe or dll. Its advantage is flexibility. I believe everyone knows this. Because users often need to replace image resource files in my projects, I chose the "content" method.

In WPF, both resources and content are identified and loaded by URI (uniform resource identifier. Here you can learn more about the URI construction and parsing principles: Pack URI in msdn wpf. The Pack URI scheme is used by the Open Packaging Conventions specification, which defines the Pack URI scheme using the extendibility of RFC 2396. That is to say, the defined URI must comply with RFC 2396.

The authorization component of Pack URI is an embedded URI that points to the package and must comply with RFC 2396. In addition, the character "," must be replaced with the character "/", and the reserved characters (such as "%" and "?") must be reserved. Escape. For more information, see OPC.

Let's take a look at the requirements and restrictions on the URI in RFC 2396:

Let's take a look at the reserved characters: reserved = ";" | "/" | "? "|": "|" @ "|" & "|" = "|" + "|" $ "| ",". As the name suggests, these characters are reserved for the system and cannot be contained in our own resource URI.

Check which characters are excluded: excluded = "<" | ">" | "#" | "%" | <">. These characters are not allowed to appear. Let's take a look at the explanations for these characters in RFC 2396:

The angle-bracket "<" and ">" and double-quote (") characters are excluded because they are often used as the delimiters
around URI in text documents and protocol fields. The character "#" is excluded because it is used to delimit a URI from a fragment identifier in URI references (Section 4).The percent character "%" is excluded because it is used for the encoding of escaped characters.

Obviously, our image contains the "#" symbol because of the Project path, and the image uses the content method, which leads to URI Parsing failure, the image is not displayed.

Although we write only one line of XAML code to display images, the Pack URI of WPF performs resource classification, parsing, and file loading.

So far, we have clearly understood the cause of the error, but we haven't thought of a solution yet. We can only tell the user to standardize the directory name. If any one has a solution, thank you!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.