0

I am taking images with a machine vision camera and storing them into a folder. I want to show the saved image in a pictureBox before the next image is stored. My code is successfully showing the image in the pictureBox, however, there is a delay so i.e by the time image 10 is saved to the folder the GUI is only showing image 3. I am not sure how to increase the performance to allow the GUI to update in real time as I am taking images.

I think my problem may be due to using the png image to create the MemoryStream instead of the byte[] buffer (raw sensor data). I have attempted using the byte[] but I was not able to successfully populate the pictureBox.

This is my code:

// Grab a number of images.
for (int i = 0; i < num_images; ++i)
{
// Wait for an image and then retrieve it. A timeout of 5000 ms is used.
     IGrabResult grabResult = camera.StreamGrabber.RetrieveResult(5000, 
     TimeoutHandling.ThrowException);
     using (grabResult)
     {
          // Image grabbed successfully?
          if (grabResult.GrabSucceeded)
          {

               buffer = grabResult.PixelData as byte[];
               //ImageWindow.DisplayImage(0, grabResult);
               file_extension = "Position_" + i + ".png";
               image_filename = String.Concat(image_filepath, file_extension);
               ImagePersistence.Save(ImageFileFormat.Png, image_filename, grabResult);
               //image_algorithms();

               Image image = Image.FromFile(image_filename);
               var ms = new MemoryStream();
               image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
               var bytes = ms.ToArray();
               var imageMemoryStream = new MemoryStream(bytes);
               Image imgFromStream = Image.FromStream(imageMemoryStream);
               pictureBox1.Image = imgFromStream;
               pictureBox1.Refresh();

               }
               else
               {
                    Console.WriteLine("Error: {0} {1}", grabResult.ErrorCode, 
                    grabResult.ErrorDescription);
               }
         }
}
palacetrading
  • 71
  • 1
  • 11
  • 5000ms are 5 seconds, much much longer than it could take to show an imge. Where is the time really going?? – TaW Feb 07 '20 at 16:27
  • @TaW The 5000 is a timeout counter for if the image drops a frame (almost never). That's part of the camera manufacturer SDK. – palacetrading Feb 07 '20 at 16:37
  • OK, so what are your numbers? speed needed and image size`? – TaW Feb 07 '20 at 16:50
  • ~1 image saved and shown on GUI per second, I added System.Threading.Thread.Sleep(1000) at the end of my for loop to slow things down but it's still delayed. Image size: 3840 x 2748 – palacetrading Feb 07 '20 at 16:57
  • Um, so what is the issue: too slow or too fast??? Also: Do you need to display the full size? – TaW Feb 07 '20 at 17:00
  • @TaW Too slow! My images have finished writing to the output folder while the GUI is still updating and showing an early image and not the final image. No I am using a a picture box that is 10% of the total size of the image – palacetrading Feb 07 '20 at 17:06

0 Answers0