I have binary files which contain each one PNG file at a time (the binary file is not a DLL, not a EXE, nothing usual, just a file which contains different textual information, a PNG file and some other stuff. The format of the file is unknown to me. The PNG file is displayable with a program which does this kind of files). I have not the source of this program which does these files. My task is now to extract this PNG file out of the binary file for displaying it or saving it as PNG. I wrote a code which works on some of these files (let's say about 50% of the files), but on anothers not. On the not working files the program which created this files can still display the containing image, so the image is inside of every file surely valid - but anyway my code doesn't work on some of the files.
Some images seem to have maybe another format, maybe encoding type (I tried already all different encoding types, nothing succeeded). Here is my code (I hope someone can tell me what to change that the image becomes readable always).
What does my code: It finds the know starting string of the PNG image "‰PNG" and the known ending string "IEND®B`‚". This strings are in any of my binary files containing the PNG's the same. Then my code takes the string between start and end + the start and the end sequence and saves it to a file with Encoding.Default. Most by this way extracted PNG files can be displayed with an Image Viewer, but around 50% are invalid. The image looks okay if I open it with an editor and compare the characters to a working image. Sofar I have no clue which symbol is the reason for the wrong image format.
If needs I'll provide more information, here my code:
private void button2_Click(object sender, EventArgs e)
{
string ReadFile1 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "file.dat");
string WriteFile1 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "test.png");
string TMP = File.ReadAllText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), ReadFile1), Encoding.Default); //System.Text.Encoding.GetEncoding(1251)
int start1 = TMP.IndexOf("PNG", 0 ,StringComparison.Ordinal);
if (start1 == 0) { return; }
int end1 = TMP.IndexOf("IEND", StringComparison.Ordinal);
string PNG = TMP.Substring(start1 - 1, (end1 + 9) - start1);
File.WriteAllText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "test.png"), PNG, Encoding.Default);
}
I also thought first of getting the PNG with a binary method and used this code, but I had exactly the same results then with just reading the string. Here my earlier code. I seeked the position in the byte array using the string to compare. I had no luck with the binary code...
byte[] by;
// 1.
// Open file with a BinaryReader.
using (BinaryReader b = new BinaryReader(File.Open(ReadFile1, FileMode.Open), Encoding.Default))
{
// 2.
// Variables for our position.
int pos = start1 - 1; //I determine the right positions before doing this
int required = (end1 + 9) - start1;
// 3.
// Seek to our required position.
b.BaseStream.Seek(pos, SeekOrigin.Begin);
// 4.
// Read the next 2000 bytes.
by = b.ReadBytes(required);
b.Close();
}
FileStream writeStream;
writeStream = new FileStream(WriteFile1, FileMode.Create);
BinaryWriter writeBinay = new BinaryWriter(writeStream, Encoding.Default);
writeBinay.Write(by);
writeBinay.Close(); */