0

Hello I have to save an image contained in a picturebox in C #, to do so I use the code below, but this exception is generated:

a generic error occurred in gdi+ at system.drawing.image.save

how can I solve them?

C# Code:

 using (MemoryStream m = new MemoryStream())
 {
   if (pictureBoxFirma.Image != null)
   {
     const int milliseconds = 2000;
     Thread.Sleep(milliseconds);

     pictureBoxFirma.Image.Save(m, ImageFormat.Jpeg);
     byte[] photo_aray = new byte[m.Length];
     m.Position = 0;
     m.Read(photo_aray, 0, photo_aray.Length);
     utemp.AggionraFirmaUtente(photo_aray, IdUtente);
   }
   else
   {
     Console.WriteLine("Errore: inserimento immagine non riuscito");
   }
 }
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
riki
  • 1,502
  • 5
  • 17
  • 45
  • Try to get more information from that exception. Is there an inner exception? Post the stack trace. – bommelding Jan 30 '19 at 08:21
  • Jpegs are a bit sensetive in how the streams are created / closed. Try the code in this answer: https://stackoverflow.com/a/10174671/1257728 to create a byte array from the image – mortb Jan 30 '19 at 08:22
  • You don't need the delay. You don't need the byte array. Just use `m.ToArray()`. The file format of the original Image must be `jpeg`. If it's `png` (or something else), you'll get a blob. – Jimi Jan 30 '19 at 08:29
  • 1
    @mortb thanks i have fix – riki Jan 30 '19 at 08:30

0 Answers0