I need to format text and provide it via a WebService to client application which are also written in C#. I need to have text formatting like already StackOverflow provides with bold, numbering, etc.
Additionally i need to embed an image within this "text". Cause the provided text including the images need to be stored on the client side for caching for the case that the webservices are not available.
I think solving the text formatting could be done with a markup language but how to store/embed an image within an formatted text?
Or maybe is this possible with HTML? The important thing is, that i provide for example on the web an small interface for text input and select for the images which then get embedded in the text. So that i need to have a parser and display component on client side (winforms app) and web side (asp.net mvc application).
Well, you would have to use some sort of markup language. HTML is the most logical choice if the result is intended to be displayed on the web. However, no markup language will generally let you embed an image. Markup is text-based and images are binaries. The two formats don't really mix.
That said, HTML is somewhat of special case. You can use Data URIs to include arbitrary image data or SVG, since SVG itself is a specialized vector format that uses markup rather than binary code to encode an image. If your image isn't a vector, though, you'll have to rely on Data URIs, and even then, those should only be used for relatively small images. The Data URI is basically a byte array encoded as base64, which means it doesn't scale well. For example, I just tested encoding a 44 KB image as a data URI and the result was 62 KB. Nevertheless, if you choose to do it this way, the syntax is:
<img src="data:[mimetype];base64,[data]"/>
Where [mimetype]
is the image's mime type (image/jpeg
, image/png
, etc.) and [data]
is your base64 encoded byte array.
Data URIs are generally well supported in browsers now, but some older versions of IE, in particular, cannot read them. That's likely not a concern though. What is a concern is that the response you're sending pretty much has to be used to display on the web or it will have to be processed by something that can parse HTML (and Data URIs) like WebKit. As a result, you're boxing yourself in, in terms of how the response can be utilized.