private void serialPort2_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
How have you defined that this method should be called? If this method is triggered as soon as ANY data arrives on the serial port, then the code in it is suspect.
List<byte> recibido = new List<byte>(); //byte receive
Make an empty List.
int bytes = serialPort2.BytesToRead;
The name is atrocious. It is not an array as implied. It is a count of the number of bytes that are available to read, which will certainly not equal the number of bytes in the picture.
byte buffer = new byte[bytes];
Now, we create an array to hold the serial data.
serialPort2.Read(buffer, 0, bytes);
foreach (byte elem in buffer)
recibido.Add(elem); //here "it receives the file"
Then, we read all the data that we knew about earlier (more may have arrived) from the serial port, storing the data in the array. Then, we loop, moving each character from the array to the list.
Then, the function ends, and the list goes out of scope without ever having been persisted anywhere. So, all the serial data just read, valuable or not, just hit the bit bucket.
private void button1_Click(object sender, EventArgs e)
List<byte> recibido = new List<byte>();
Create a List, close the serial port, clear the empty list, and let the empty, cleared list go out of scope. What was the purpose of the List?
private void button2_Click(object sender, EventArgs e) //with this button I pretend to show the image after the computer receive it.
List<byte> recibido = new List<byte>(); //list receive
//Guarda todo en archivo, here It suppose to save everything inside the archive
FileStream archivoP = new FileStream("Prueba.jpg",
BinaryWriter escribirP = new BinaryWriter(archivoP);
foreach (byte elem in recibido)
Create a new, empty List. If the file exists, delete it. Then, open the file. Write the data in the empty list to the file, and close it. Then, try to display the empty picture, and wonder why the picture is empty.
Do you have a clue, now, how to fix your problem?