Insert image into xml file using c# - c#

I've looked everywhere for the answer to this question but cant find anything so hoping you guys can help me on here.
Basically I want to insert an image into an element in xml document that i have using c#
I understand i have to turn it into bytes but im unsure of how to do this and then insert it into the correct element...
please help as i am a newbie

Read all the bytes into memory using
File.ReadAllBytes().
Convert the bytes to a Base64 string
using Convert.ToBase64String().
Write the Base64 Encoded string to
your element content.
Doneski!

Here's an example in C# for writing and reading images to/from XML.

You can use a CDATA part or simply put all the bytes in their hexadecimal form as a string.
Another option is to use a base64 encoding
The element you use is up to you.

http://www.dreamincode.net/code/snippet1335.htm seems to do exactly what you want to do. It might be something you might want to try out. Note that it is in VB.NET which you can easily convert to C#.

XML can only contain characters, it can't contain an image. There are various ways you can represent an image using characters, for example by encoding the image in PNG and then encoding the PNG in base64; or you could generate an element that contains a link to a URI from where the image can be retrieved. All such conventions have to be agreed between sender and recipient. So before you rush into base64 encoding, check that this is what the recipient expects.

Related

C# Parse HTML Post Data

I have MemoryStream data (HTML POST Data) which i need to parse it.
Converting it to string give result like below
key1=value+1&key2=val++2
Now the problem is that all this + are space in html. Am not sure why space is converting to +
This is how i am converting MemoryStream to string
Encoding.UTF8.GetString(request.PostData.ToArray())
If you are using Content-Type of application/x-www-form-urlencoded, your data needs to be url encoded.
Use System.Web.HttpUtility.UrlEncode():
using System.Web;
var data = HttpUtility.UrlEncode(request.PostData);
See more in MSDN.
You can also use JSON format for POST.
I suppose that the data you are retrieving are encoded with URL rules.
You can discover why data are encoded to this format reading this simple article from W3c school.
To encode/decode your post string you may use this couple of methods:
System.Web.HttpUtility.UrlEncode(yourString); // Encode
System.Web.HttpUtility.UrlDecode(yourString); // Decode
You can find more informations about URL manipulation functions here.
Note: If you need to encode/decode an array of string you need to enumerate your collection with a for or foreach statement. Remember that with this kind of cycles you cannot directly change the cycle variable value during the enumeration (so probably you need a temporary storage variable).
At least, to efficiently parse strings, I suggest you to use the System.Text.RegularExpression.Regex class and learn the regex "language".
You can find some example on how to use Regex here; Regex101 site has also a C# code generator that shows you how to translate your regex into code.

Get only strings from binary file

I try to print to screen a string from a binary file using xaml labels, but when i display the file content I got a beautiful "corrupted" character instead of the entire file content.
I think the problem is reading the file, I already can change the label content using the most basic technique it work pretty well till today....
label.Text = mystring ;
The fact is : I have data in my binaries files that inst text (some random data that I don't care) located to the start of the file, my theory is my program start reading, read a non ascii character and stop reading...
I read using the File class, maybe the wrong thing.....
label.Text = File.ReadAllText(my_file);
So, im lock now. I don't exactly know what im supposed to do....
Hope you can help me :D
I can't tell much without looking at the text, but it seems you need to add the Encoding
Something like this:
string myText = File.ReadAllText(path, Encoding.Default);
You need to know how your binary file is structured. You need to know the encoding of the strings. A normal text file normally has markers at the beginning two or so bytes that identify its encoding if it is Unicode. This way the system can know whether its UTF-8, UTF-16, ...
If you try to read a binary file this Information is not there. Instead the reading process will most probably find unexpected binary data. So you cannot read a binary file as text. If your file is structured the way that at the beginning is binary data and later only text, just skip the first part and start reading at the start of the second part. But I don't think, that it is that easy:
if it really is binary data, chances are that the file structure is much more complicated and you need to do more work to read it.
if only the first two bytes are binary data, then maybe its a text file and you can read it without problems, you maybe only need to pass the right encoding to the reading function

Asp.Net : Convert PDF to Image

I have a pdf file in a base64 string format. I need to convert it to an image (any type) to use further in my code.
I have searched SO as well as the web and I have not been successful with anything.
Does anyone have a tried and true method for this?
I can use a third party. I just need a working one!!
Thank you
I used the Apitron PDf Rasterizer
to convert my file. It is very simple to use and works great. No missing dlls and clear documentation. However, you do need to purchase it.
Thanks to all for assistance
base64 is the form of string web friendly representation of byte array. you may convert it to a byte array like this:
byte [] decodedBytes = Convert.FromBase64String (encodedText);
then you may simple save it to a file:
System.IO.File.WriteAllBytes("file.pdf", decodedBytes);
and then finally convert pdf to image

Unable decrypt URL encoded Base64 String

Hello thank you for reading;
I've tried to decrypt thise Base64 string in anyway possible. Also tried searching Stackoverflow and tried other methods. Every response it's just jibberish.
v6kEwElTQI%2fNlQc87zM7Od2%2fsaAghvSbCVyYaJRTf4U%3d
Hope you've got any ideas!
It's not exactly readable, but it does successfully Base64 decode:
Firstly, I manually de-URL-encoded it. There are three URL escape sequences in there:
%2f : '/' (two of these)
%3d : '='
Giving me v6kEwElTQI/NlQc87zM7Od2/saAghvSbCVyYaJRTf4U=
With those changed, it's Base64.
Then, you can use the old methods of decoding in C# .NET:
byte[] arr = Convert.FromBase64String('v6kEwElTQI/NlQc87zM7Od2/saAghvSbCVyYaJRTf4U=');
Console.WriteLine(System.Text.Encoding.Default.GetString(arr));
// As a note, I'm using Default encoding here. Your system may use
// a different encoding by default. Alternatively, you can replace
// 'Default' here with ASCII or Unicode.
Which gave me ¿©ÀIS#Í•<ï3;9Ý¿±  †ô› \˜h”S…
Using unicode I get 쀄卉轀闍㰇㏯㤻뿝ꂱ蘠鯴尉梘厔蕿 instead.
I have ruled out that it's not an image (at least in a System.Drawing.Image format.)
While decoding it was fun, I have to agree with the comments. It's important to know what the data is going to be after it's decoded since it'll only be a nameless stream of bytes at that point.

How to convert serialized byte array back to its text form

I have a text that is a property of an object.The object gets XmlSerialized and after that there is an element in the XML call Text that represents the text from the object.I am wondering how to turn it back to string.
THE TYPE OF SERIALIZATION: XmlSerializer serizlizer = new XmlSerializer(typeof(Act));
THE PROPERTY IN THE CLASS :
[System.Runtime.Serialization.OptionalFieldAttribute()]
private byte[] ActTextField;
In the xml file it looks something like that:
0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAAAAABAAAALQAAAAAAAAAAEAAALwAAAAEAAAD+////AAAAACwAAAD////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////spcEAJ2AJBAAA8BK/AAAAAAAAEAAAAAAABgAAYB4AAA4AYmpiavbg9uAAAAAAAAAAAAAAAAAAAAAAAAACBBYALiIAAJSKAQCUig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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB4EIAAgAB8EIAAgACAEIAAgABUEIAAgABQEIAAgABUEIAAgABsEIAAgABUEIAAgAB0EIAAgABgEIAAgABUEIAANABUELgAgAEAEMAQ5BD4EPQQ1BD0EIABBBEoENAQgADIEIAA3BDAEOgRABDgEQgQ+BCAAQQRKBDQENQQxBD0EPgQgADcEMARBBDUENAQwBD0EOAQ1BCAAPQQwBCAANAQyBDAENAQ1BEEENQRCB
I can not even suppose what is its encoding and how to decode it.I tried to read it into byte array but it didn't actualy work after applying few decodings Encode.UTF8 , Encode.ASCII,
That looks like Base64 to me - just use
byte[] data = Convert.FromBase64String(base64Text);
It's odd that it's using base64 at all if this is really a text property though. I'd expect just the text.
To convert that binary data back to text you would need to know which encoding was used to convert it to the binary data to start with - and UTF-8 is the most likely - but all the repeated AAAAA... parts in there make this look pretty unlike text, to be honest.
EDIT: Now that we've seen the field declaration, we can see that it was a byte[] to start with, so that makes sense for it to be encoded in this way. Judging by comments, it sounds like it's actually a Word file - at which point extracting the text is a very separate problem.

Categories