2

I have the PNG image data like below but I can't able to decode it by using any of the decoding method.

People who have knowledge on this help me to get the image by using encoded/decoding technique of this.

"�PNG\r\n\u001a\n\0\0\0\rIHDR\0\0\u0013`\0\0\u001bf\u0001\0\0\0\0Nw�v\0\0 \0IDATx��O��H���(/�\u0017\u0006�b-tP\u001ej�U؃����4\u0005l)Y�}�Q\u001fa���9*\a���ڢgGo{\u001f\u0006\u001d_@@C��\u0004:,�\u001e�\t\u001d\u0004�\u001c�0��o\n*a �\u0019��6��I���H�����o�#\u007f\u000f\"#��iF��9iƗ\u00165\0\u0010\u0011=t\u0001��\u0003d\u0003d@6@\u0006d\u0003d@6@\u0006d\u0003d@6@\u0006d\u0003d@6@\u0006d\u0003d....

It has all the png critical chunks like IHDR, IDAT, IEND.

//For Encoding

byte[] buf = File.ReadAllBytes(@"C:\Users\GPL\Desktop\Newfolder\balloon_PNG4957.png");
var s = Encoding.ASCII.GetString(buf);
File.WriteAllText(@"C:\Users\GPL\Desktop\balloon_PNG4957.txt", s);

//For Decoding

var rawdata = File.ReadAllText(@"C:\Users\GPL\Desktop\balloon_PNG4957.txt");
byte[] buf = Encoding.ASCII.GetBytes(rawdata);
var ms = new MemoryStream(buf);
var bitmap = Image.FromStream(ms);  //Error
pictureBox1.Image = bitmap;

Here while decoding I am getting error - ""Parameter is not valid". "

John
  • 41
  • 1
  • 4

2 Answers2

0

It is called escaped string literal

try this (replace real text here after st=) like this:

    string st= "�PNG\r\n\u001a\n\0\0\0\rIHDR\0\0\u0013`\0\0\u001bf...";
    File.WriteAllBytes("png.png",  st.Select(s => (byte) s).ToArray());

or simply convert it char by char :

var  ba = new List<byte>();
foreach (var s in st)
{ 
    ba.Add((byte) s);
}  
File.WriteAllBytes("png.png",  ba.ToArray());

note: for two bytes Unicode chars use another ba.Add((byte) (s>>8)); inside foreach.


this is what you need: C# escape characters in user input

see: Can I convert a C# string value to an escaped string literal


If it is a file you may read it like this and show inside pictureBox1:

var bitmap = Image.FromFile(@"filename.png");
pictureBox1.Image = bitmap;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;

If it is Stream or MemoryStream or byte[] use this:

byte[] buf = File.ReadAllBytes(@"filename.png");
var ms=new MemoryStream(buf);

var bitmap = Image.FromStream(ms);
pictureBox1.Image = bitmap;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;

It is ASCII Encoding:

byte[] buf = File.ReadAllBytes(@"filename.png"); 
var sb=new StringBuilder();
var s=Encoding.ASCII.GetString(buf );
textBox1.Text = buf.Length + @", " + s.Length;
File.WriteAllText("png.txt", s);
Community
  • 1
  • 1
  • Thanks for the reply Amd, I tried in all the possible ways it's not getting convert. its throwing the error like "No imaging component suitable to complete this operation was found". Even I tried with base64, PngBitmapEncoder nothing helped. – John Aug 11 '16 at 09:33
  • @John: Is this what you need? –  Aug 11 '16 at 09:34
  • No Amd. I want to know what type of encoded string is that...??? how to get the image back by decode that string..??? Its not a image file, its the encoded png image data in the form of string. – John Aug 11 '16 at 09:36
  • thanks for your reply. I got the think what you are telling. In your case I encoded the string with ASCII, and I saved it in the text file. Now let me how to decode that and get the image back. From your given example it is getting the input as image file only. Just explain me by giving the input as encoded text file return value is png image. – John Aug 11 '16 at 12:20
  • To decode ASCII ---> I am converting the encoded png string file to decoded bytes, again bytes to memorystream, finally when converting memorystream to image it is giving this error "Parameter is not valid". I am not getting the things why its not converting as image.!!! suggest your solutions. – John Aug 11 '16 at 13:41
  • @John Edit your post (click on edit below your Question) and copy paste your code then select your code then press Ctl+K to format that code then click on save , so I can see your code and error . –  Aug 11 '16 at 13:57
  • Nice one but the png image is not getting open in any of the image viewer. also when converting the byte array to memory stream getting the below 2 errors in memory stream object. 1)ReadTimeout = 'ms.ReadTimeout' threw an exception of type 'System.InvalidOperationException', 2)WriteTimeout = 'ms.WriteTimeout' threw an exception of type 'System.InvalidOperationException'. These two errors lead to "Parameter is not valid." error. – John Aug 11 '16 at 17:26
0
Stream mr = response.GetResponseStream();
var bitmap = Image.FromStream(mr);  
Bitmap bmp = new Bitmap(bitmap);                                                                             
MemoryStream ms = new MemoryStream();                                                               
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);