include image in XML file - c#

I want to be able to send an image from HTML page to a XML file using C#.
The image should be sent along with some text, the problem is how do I store the image in the XML file efficiently, so it can be sent over the wire and how do I store the position of the image on the HTML page, so it can be restored later in the original position?
I was originally going to keep a hyperlink in my XML file to an image and load it that way on the HTML page, using ASP.NET, but I wondered if there's better ways?
EDIT:
So how do I keep the coordinates of the picture in the page in relation to all other objects. What ways can I save it to the XML file and how do I get the coordinates? Using ASP.NET, HTML and or JavaScript?

You can do it but its a really bad idea. If this is in an ASP.Net context then the hyperlink method sounds much more reasonable.
However, if you insist on encoding images in XML, then have a look at base64 encoding or ASCII 85.

There are binary xml specifications out there for sending of such data via xml, but these don't cater to your specific "position of the image" issue.
I would simply keep the image URL and use that.

I wouldn't recommend doing this (better add a link on the serverside that you can reference a < img />, like http://yourhost/someGuid maybe, and leave the serialization to your webserver and the browser), but base64 would be an easy option for your usecase.

Follow the KISS principle.
Do what you were originally going to do, and keep the link in the file.

You could encode the image as a data URI, which most browsers seem to support now. It's still base 64, and so less compact than a separate binary file, but it is a standard way of inlining small images.

Related

Is there a high fidelity way to convert HTML into PDF and DOCX?

I need to convert HTML files into PDF and DOCX respectively (just the HTML -> PDF part would good enough for now though).
Obviously I know there are some projects that help with what I want to achieve, I am currently using HTML-Renderer for the PDF part, and OpenXML for the DOCX.
I've tried HTML-Renderer but the fidelity of the conversion is not great, since I read somewhere I can't make headers and footers with HTML for multipage formats. furthermore the conversion scratches off the end of the text when it passes from one page to another.
As for the DOCX, I don't know what the best options are.
I want, if possible, to know what are good high fidelity ways to convert HTML to those formats, any helps is greatly appreciated.
I'm open to ideas/advice on how to make it myself, but right now I don't have the time to do so, so I would much rather use an existent NuGet/DLL/library.
You could consider shelling out to pandoc:
https://pandoc.org
For visual appeal, you might like the Eisvogel template:
https://github.com/Wandmalfarbe/pandoc-latex-template
...which although designed for Markdown, ought to work for well structured, semantic HTML as input to Pandoc too.

How to show preview of image being uploaded before actually sending it to server

I have a FileUpload control in my aspx page and I want to display the image selected before postback so that user confirms the image to upload.
You can use this AJAX Control Toolkit Control for Image Confirmation.
http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/AsyncFileUpload/AsyncFileUpload.aspx
What you're looking for would require a pure client-side solution, which I do not think is possible since they do not have access to the file system(under normal circumstances).
Note that using AJAX requires actually sending the file to the server first.
This Can be done VIA HTML5 now,
http://www.html5rocks.com/en/tutorials/file/dndfiles/
When the upload is selected you want to read the file with
readAsBinaryString
Then you would need to turn that binary to Base64 so you could display it on the page,
http://www.webtoolkit.info/javascript-base64.html
Then you will need to put it into an img tag on the in the src E.G
<img src="data:{image/mime_type};base64,{base64_binary_data}" width="100" height="100" />
Where {image/mime_type} is the mime type of the image they have uploaded E.G image/png, image/jpg
And {base64_binary_data} is the readAsBinaryString after it's gone though the base64 conversion
It's not possible through HTML. But it may be possible using flash/silverlight.
I say this because I remember coming across an issue when I wanted to know up front how big a file was before the user uploaded it. Html doesn't give you the ability to know this, the user must upload the whole file before you can say how big it is.
However, the workaround was to use flash because flash seems to have permissions to grab data about a file from the local disk before sending it to the server (after the user selects the file of course). Since it can grab the filename, size, etc then I imagine you'll be able to grab the image data too and then display it.
You're going to need to use flash or silverlight to do that.
Here's an article where it's explained how to do that with flash:
http://blog.flexexamples.com/2008/08/25/previewing-an-image-before-uploading-it-using-the-filereference-class-in-flash-player-10/

Set the source of Adobe PDF browser plugin to be a byte stream?

Is it possible to have the source for an Adobe Plugin to be a PDF byte stream?
If so, how would this be declared in markup?
EDIT 1:
OK, so let me elaborate on this. I have built a site that runs on Azure. The site's back-end is a WCF web service, also on Azure.
One particular functionality of the site has to do with reporting. Once the user selects the type of report they want, a request is made to the service and a byte stream of a pdf file is sent back as a response.
Now, the question is how would the user view the file? Is it possible to set the Adobe Acrobat PDF plugin's document source to be something along the lines of:
<OBJECT>
<PARAM src="asdfkjaskflasjdlfkjasd"/> //byte stream
<PARAM mimeType="application/pdf;"/> //stream's mime type
</OBJECT>
EDIT 2:
OK, so no-one wants to take a crack at this. However, the only reason I am asking is because it is entirely possible to set the source of an image with the following code:
<img class="myImage" img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAkAAAAJCAYAAA"/>
where everything past the image/png;base64 represents the byte content of the image encoded as a base64 string.
So is it still possible?
Thanks,
I doubt the Adobe plugin can be made to fetch PDF data from a data: URL.
And even if it is possible in some browsers, you will with 100% certainty not be able to achieve a cross-browser solution this way: data: URLs don't work in IE < 8 at all, and are limited to 32 kilobytes' size 8 and 9.
What speaks against simply serving the byte stream in a separate resource with a PDF content-type?

Response.BinaryWrite DIV

Is there a way to write PDF to a div from DataBase i.e. Retrieve a Byte[] from Database and Reponse.BinaryWrite to a div.
We do similar thing for Images using src = "anotherpage.aspx" where image is written on anotherpage.
Is it possible with PDF without using IFrame?
If what you're trying to do is show a PDF file inside a DIV, you're going down the wrong path. You either need to:
Convert the PDF to Flash (ala Flash Paper)
or
Convert the PDF to HTML (like Scribd does using HTML 5).
Then you can embed the PDF inside a DIV. But no browser I know of supports directly embedding PDFs.
Otherwise you have to put the PDF in an IFRAME, but how this is shown is PDF plug-in dependent.
No. The reason it works with a src=otherpage.aspx request is that the src attribute results in the user's web browser making a completely separate request for the other resource. You're serving up an additional page to make that happen. Writing a PDF file directly is trying to inject the PDF into the same request as your page - not really "similar" to your img src at all. In fact, what is most similar to the "src=otherpage.aspx" method is the iframe approach that you mentioned.
As a side note, you our "AnotherPage.aspx" example should really be changed to "AnotherPage.ashx". Note the letter 'h' in there. That means you're using a handler rather than a page, which will perform better.

Save an HTML page with all styles and images in C#?

How do I save an HTML page with all styles and images in C#? I need to make a programmatic implementation of a browser's 'Save' feature which doesn't rely on Internet Explorer (WebBrowser component).
I do not think this is very easy.
Download all the HTML for the page using webclient and write the text to an HTML-file. Then use an html-parser to find all linked images and save them in their sub-directory. Do the same for the CSS.
If you do not want to save all the images you can just add the URL of the page to the beginning of all links to images. Also, note that some URL:s are not relative and you will have to compensate for that. And don't forget to scan the css-file for all linked images
I have a similar thing to solve. Biggest problems for you will be the images that come from CSS, they are very difficult to parse.
So, I chose to use FiddlerCore to achieve that.
Might help you too.
The difficult part of your task is to create your own structure, and change image paths accordingly.

Categories