My friends send me a file. He save multiple gif file in one file and he give me list like
file1 - StartFrom - 1 and length 18493,
file2 - StartFrom - 132089 and length 824,
file3 - StartFrom - 18494 and length 2476 etc..
I don’t know how he put all gif file into one file. I need extract all gif image from this file.
Is anyone help me one how i write a code in vb.net or C# . I write a code below:
private void button1_Click(object sender, EventArgs e)
{
byte[] buffer = new byte[18493];
string destPath = Application.StartupPath + "\\mm.gif";
string sourcePath = Application.StartupPath + "\\Data.qdd";
var destStream = new FileStream(destPath, FileMode.Create);
int read;
var sourceStream = new FileStream(sourcePath, FileMode.Open);
while ((read = sourceStream.Read(buffer, 1, 18493)) != 0)
destStream.Write(buffer, 0, read);
}
But there is and error show is :
Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.
EDIT edited solution
The error lies here
You create a buffer byte array variable with length 18493, so it has 18493 elements.
The position in the array are from 0 to 18492.
The Read function try to write 18943 byte from position 1, so the last byte will be written to buffer[18943] going out of the limit of your array.
Try to change the read to
EDIT 2 Solution to the whole problem
To solve the problem and not just the error you should allocate three different buffer and write free different file. (You could use just one buffer and clean it every time, but is easier to understand if you use three of them)
You cannot use a while to read because in that way you’ll go to the end of the file!
Just read the file for the desired lenght and then move for the next file.