Convert from string to image in C #

I am trying to convert a Unicode string to an image in C #. Every time I run it, I get an error on this line

Image image = Image.FromStream(ms, true, true); 

which states: ArgumentException was not handled by user code. Invalid parameter. Any idea why this is happening? Below is the rest of the function.

 public Image stringToImage(string inputString) { byte[] imageBytes = Encoding.Unicode.GetBytes(inputString); MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length); ms.Write(imageBytes, 0, imageBytes.Length); Image image = Image.FromStream(ms, true, true); return image; } 
+7
source share
5 answers

Unicode does not encode all possible byte sequences that you need to represent for an image.

byte[] β†’ String β†’ byte[] is a conversion that simply will not work for many given byte sequences. You will need to use byte [] throughout.

For example, if you read bytes, convert them to UTF-16, then it is possible that byte sequences will be discarded as invalid. Here is an example of an invalid byte sequence from UTF-16 .

Code points U + D800 to U + DFFF [edit] The Unicode standard constantly reserves these code point values ​​for encoding UTF-16 lead and surrogates, and they will never be assigned by a character, so there should be no reason to code them. The official Unicode standard says that all forms of UTF, including UTF-16, cannot encode these code points.

+8
source

Take out your call, which is recorded in a MemoryStream. A constructor call that takes a byte array automatically puts the contents of the byte array into the stream. Otherwise, your stream contains 2 copies of the raw data. In addition, the Write call leaves the position of the stream at the end of the stream, so there is no data that FromStream can read.

So this will be:

 public Image stringToImage(string inputString) { byte[] imageBytes = Encoding.Unicode.GetBytes(inputString); // Don't need to use the constructor that takes the starting offset and length // as we're using the whole byte array. MemoryStream ms = new MemoryStream(imageBytes); Image image = Image.FromStream(ms, true, true); return image; } 
+1
source

This may help you:

 public Bitmap stringToImage(string inputString) { byte[] imageBytes = Encoding.Unicode.GetBytes(inputString); MemoryStream ms = new MemoryStream(imageBytes); return new Bitmap(ms); } 
+1
source

Are you getting the image as a string, from ldap? I'm sure this is true, the string will actually be base64 encoded, in which case it contains bytes that represent valid characters, not image data.

Could you post a snippet of the line you get?

If this is true, you need to take the string and rotate it byte [] with un-base64, and then use an array of bytes to create the image. Combined with @JonBenedicto code:

 public Image stringToImage(string inputString) { byte[] imageBytes = Convert.FromBase64String(inputString); MemoryStream ms = new MemoryStream(imageBytes); Image image = Image.FromStream(ms, true, true); return image; } 
+1
source

Using a string, you can get data loss, I just send an example of converting an image to a byte array and an array to an image again, and after an image to a byte array, to a string and vice versa, without data loss.

  MemoryStream ms = new MemoryStream(); Image.FromFile(@"C:\..\..\..\img.jpg").Save(ms,ImageFormat.Jpeg); byte[] bytes = ms.ToArray(); MemoryStream ms1 = new MemoryStream(bytes); Image NewImage = Image.FromStream(ms1); NewImage.Save(@"C:\..\..\..\img1.jpg"); 

try this and it can help you create what you need.

trying to convert to string and vice versa, better to use base64.

  MemoryStream ms = new MemoryStream(); Image.FromFile(@"C:\..\..\..\img.jpg").Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg); byte[] bytes = ms.ToArray(); string byteString = Convert.ToBase64String(bytes); byte[] NewBytes = Convert.FromBase64String(byteString); MemoryStream ms1 = new MemoryStream(NewBytes); Image NewImage = Image.FromStream(ms1); 

This should give you the result you need.

  MemoryStream ms = new MemoryStream(); Image.FromFile(@"C:\..\..\..\img.jpg").Save(ms,ImageFormat.Jpeg); byte[] bytes = ms.ToArray(); string byteString = Convert.ToBase64String(bytes); 

then when you pass this line to your method ...

  public Image stringToImage(string inputString) { byte[] NewBytes = Convert.FromBase64String(inputString); MemoryStream ms1 = new MemoryStream(NewBytes); Image NewImage = Image.FromStream(ms1); return NewImage; } 
0
source

All Articles