Hello, there is an image, let's say jpg which is open with a notepad and this text is copied into another text document and saved as jpg. The question is why the image does not open as the original? How is it possible to read this image from the text? And if using byte read and write from a saved text file also fails.
- compare two documents by bytes (not in a text editor, of course, but with something like "Hex View" in Far'e or code) - and you will see - Igor
- Understood thanks. I found the differences now I will try again to read the file byte-bye. - Taras M. Nov.
- A text editor could add a BOM depending on the encoding. - Alexander Petrov
|
2 answers
Found the answer on the Internet to your question from the service: https://halluphigh.wordpress.com/2010/08/26/extracting-a-jpeg-image-from-any-file-using-c/
This code uses magic numbers to indicate the beginning and end of the file for jpeg; this is 0xFF and 0xD8 is not difficult to find and for other formats it was taken from here: https://en.wikipedia.org/wiki/List_of_file_signatures
response code itself:
/Remove the excess data in given file and returns the new location of the modified file and //returns an empty string if no file was created. public static string StripExcessDataFromJpeg(string fileLocation) { //if no image is found in the file, it will return this error message string newFileName = ""; BinaryReader br = new BinaryReader(File.Open(fileLocation, FileMode.Open)); try { bool done = false; long count = 0; //The file must be read until the end of the file (in which case there was no image) //or until the jpeg file is found. while ((count < br.BaseStream.Length) && !done) { //has to be read one at a time so not to consume more than needed count++; if (br.ReadByte() == 0xFF) { count++; if (br.ReadByte() == 0xD8) { done = true; newFileName = fileLocation + ".jpg"; WriteJpegBinaryToFile(newFileName, br); } } } } catch (Exception) { Console.WriteLine("File format not found."); //File name set back to empty to indicate the file wasn't created. newFileName = ""; } br.Close(); return newFileName; } private static void WriteJpegBinaryToFile(string fileName, BinaryReader br) { FileStream fs = File.Create(fileName); //write back the jpeg header already consumed in the stream byte[] jpegHeader = { 0xFF, 0xD8 }; fs.Write(jpegHeader, 0, jpegHeader.Length); long count = 0; while (count < br.BaseStream.Length) { long bytesToRead = 1024; //Read 1kb at a time, increase if dealing with larger files. if (bytesToRead + count > br.BaseStream.Length) { bytesToRead = br.BaseStream.Length - count; } byte[] bytes = new byte[bytesToRead]; br.Read(bytes, 0, bytes.Length); count += bytesToRead; fs.Write(bytes, 0, bytes.Length); } fs.Close(); } |
A text editor can ignore non-printable characters, that is, ASCII codes that are less than 32, with the exception of line feeds and tabs. Most likely it does not copy them to the buffer.
Use to copy a part of the file some binary editor.
|