A
A
Andrey Fomin2021-02-10 18:27:51
SQLite
Andrey Fomin, 2021-02-10 18:27:51

How to handle an exception?

Hello!
There is an application and a server, both written in C#. The form has a picturebox and a button, when clicked, an sql-query with SELECT is generated, this query is sent to the server, then the server pulls the picture from the database as a byte array, sends this byte array back to the application, and after the application receives this byte array , you need to display this array of bytes into a picture in the picturebox.
In this piece of code, a request is sent to the server to take a picture:

stream = Client.GetStream();
string Message3 = $"SELECT Image FROM users WHERE Login = '{LoginField.Text}'";
byte[] command3 = Encoding.UTF8.GetBytes(Message3);
stream.Write(command3, 0, command3.Length);

In this piece of code, the server takes the image after receiving the request and sends it back:
byte[] cmd = new byte[64];
                        StringBuilder builder = new StringBuilder();
                        int bytes = 0;
                        do
                        {
                            bytes = stream.Read(cmd, 0, cmd.Length);
                            builder.Append(Encoding.UTF8.GetString(cmd, 0, bytes));
                        }
                        while (stream.DataAvailable);

                        string message = builder.ToString();
                        Console.WriteLine(message);
                        stream.Flush();
                        if (message.Contains("SELECT"))
                        {
                         if (message.Contains("Image"))
                        {
                         SqliteConnection connect = new SqliteConnection("Data source = accounts.db");
                                connect.Open();
                                SqliteCommand command = new SqliteCommand(message, connect);
                                command.ExecuteNonQuery();
                                SqliteDataReader Reader = command.ExecuteReader();
                                while (Reader.Read())
                                {
                                    if ((string)Reader["Image"] == "")
                                    {
                                        string foto = null;
                                        byte[] image = Encoding.UTF8.GetBytes(foto);
                                        Console.WriteLine(image);
                                        stream.Write(image, 0, image.Length);
                                    }
                                    else
                                    {
                                        string foto = (string)Reader["Image"];
                                        byte[] image = Convert.FromBase64String(foto);
                                        Console.WriteLine(image);
                                        stream.Write(image, 0, image.Length);
                                    }
                                }
                         }

In this piece of code, the sent picture is received and displayed in the picturebox:
byte[] GetData3 = new byte[128];

            MemoryStream memoryStream = new MemoryStream(GetData3);
            pictureBox1.Image = Image.FromStream(memoryStream);

And in this line
pictureBox1.Image = Image.FromStream(memoryStream);

throws an exception:
System.Argument.Expection: Invalid parameter.

For what reason could such an exception appear and how to fix it?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question