The object is already in use, when saving image in C #

1

I have a button that saves the image of my camera, in the following way:

 MemoryStream photo = new MemoryStream();
 pictureBox1.Image.Save(photo , ImageFormat.Jpeg);
 byte[] byte_photo = photo.GetBuffer();

After I save my byte array in the database, when the user presses the button continuously, he throws the following error:

El objeto ya está en uso.
en System.Drawing.Image.get_Width()
en System.Drawing.Image.get_Size()
en System.Windows.Forms.PictureBox.ImageRectangleFromSizeMode(PictureBoxSizeMode mode)
en System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
en System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
en System.Windows.Forms.Control.WmPaint(Message& m)
en System.Windows.Forms.Control.WndProc(Message& m)

To try to solve, use Await and Async, as follows:

     async Task<string> task_photo()
    {  
        MemoryStream photo = new MemoryStream();
        pictureBox1.Image.Save(photo , ImageFormat.Jpeg);
        byte[] byte_photo = photo.GetBuffer();
        var idphoto = ObjectId.Empty;
        idphoto = fs.UploadFromBytes("nombre_archivo", byte_photo);
        return idphoto;
    }

   async void crearEmpleado(string idEmployee, string nombre)
    {
       // Mando a llamar mi Task
       string don = await task_photo();
       // Donde don es lo que me Retorna
    }

Unfortunately the problem is the same, since it sends me the same error (Object in use ...), but as a warning in my VS I have:

    This async method lacks 'await' operators and will run synchronously.
    Consider using the 'await' operator to await non-blocking API calls, 
    or   'await Task.Run(...)' to do CPU-bound work on a background thread.

I was reading a bit, apparently the object is no longer in memory, but I do not know how to solve it ....

    
asked by Rastalovely 02.03.2017 в 04:44
source

2 answers

2

I think the problem if you look closely is that the thread of execution of the UI is accessing the image and you are also accessing the same image from another thread. GDI + does not allow two threads to access a bitmap at the same time. If you need to convert your image to an array of bytes you can do the following using the class ImageConverter

public static byte[] ImageToByte(Image img)
{
    ImageConverter converter = new ImageConverter();
    return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
    
answered by 02.03.2017 / 08:37
source
2

This is the way I solved my problem, thanks to the contributions of @Sergio

    public static byte[] ImageToByte2(Image img)
    {
        byte[] byteArray = new byte[0];
        using (MemoryStream stream = new MemoryStream())
        {
            img.Save(stream, ImageFormat.Jpeg);
            stream.Close();
            byteArray = stream.ToArray();
        }
        return byteArray;
    }

And I only send it to call in the following way:

   byte[] foto = ImageToByte2(pictureBox1.Image);
    
answered by 02.03.2017 в 16:10