Skip to content

How To: Save Each Image With a Unique Filename#

Question#

How do I save each image as "image_1.png", "image_2.png", and so on in my API?

Solution#

There are many ways to approach this task, but here are two of the easiest:

  • Save the image using itoa() to convert the image number to a string for a unique file name. This might not work on Linux, as itoa() is a non-standard C++ function.

    char buf[32];
    Pylon::String_t imageNumber;
    imageNumber = itoa(ptrGrabResult->GetImageNumber(), buf, 10);
    Pylon::String_t fileName = "image_" + imageNumber + ".png";
    CImagePersistence::Save(ImageFileFormat_Png, fileName, ptrGrabResult);
    

  • Save the image using sprintf() to convert the image number to a string for a unique file name. This is more portable and will work on Linux, but adds one more line of code.

    char buf[32];
    Pylon::String_t imageNumber;
    sprintf(buf, "%d", ptrGrabResult->GetImageNumber());
    imageNumber = buf;
    Pylon::String_t fileName = "image2_" + imageNumber + ".png";
    CImagePersistence::Save(ImageFileFormat_Png, fileName, ptrGrabResult);
    

Back to Knowledge Articles