Save JPG from a camera in specific timestamp (Unable to Connect to Toolkit)

I’m trying the following: my app will receive a command to get a snapshot from a camera at a past moment from another app. I successfully logged in, listed the cameras, as in:

var configs = Configuration.Instance.GetItems();
            foreach (var config in configs) {
                foreach (var cameraGroup in config.GetChildren()) {
                    foreach (var defaultCameraGroup in cameraGroup.GetChildren()) {
                        foreach (var camera in defaultCameraGroup.GetChildren()) {
                            if (camera.FQID.Kind == Kind.Camera) {
                                Cameras[camera.FQID.ObjectId] = camera;
                                var status = 0;
                                var jpegSource = new JPEGVideoSource(camera);
                                try {
                                    jpegSource.Init();
                                    var jpegData = jpegSource.GetAtOrBefore(new DateTime()) as JPEGData;
                                    File.WriteAllBytes($"C:/Snapshots/{camera.Name}.jpg", jpegData.Bytes);
                                } catch (Exception e) {
                                    Console.WriteLine(e.Message);
                                    status = 1;
                                }
                                LstCameras.Items.Add(camera.Name, status);
                            }
                        }
                    }
                }
            }

But at the line “jpegSource.Init()” it crashes with “Unable to connect to Toolkit” error.

I’ve read a couple of threads about it:

https://supportcommunity.milestonesys.com/s/question/0D50O000049u3cpSAA/unable-to-connect-to-toolkit

https://developer.milestonesys.com/s/question/0D50O00003NHiu8SAD/unable-to-connect-to-toolkit

But neither helped. Tried restarting the server, my PC. Both firewalls are disabled and I can connect to the server fine.

I also tried the MediaLiveViewer from the samples, but it also cannot connect.

Can someone help me with this?

Are you sure you have initialized the right Environments? In my code I have initialized Media and Export environments in addition to SDK.Environment.

VideoOS.Platform.SDK.Environment.Initialize();
VideoOS.Platform.SDK.Media.Environment.Initialize();
VideoOS.Platform.SDK.Export.Environment.Initialize();

And you will need to be sure to get all the necessary dependent DLL’s. Use the copy scripts in the MIPSDK/Bin folder to make sure you get the right binaries.

Here’s a snippet of a Get-Snapshot command I wrote.

Finally, one thing that I struggled with myself is that the Environment.CurrentDirectory needs to be the directory where all the MIP SDK binaries are sitting. I discovered while building MilestonePSTools that PowerShell would set the Environment.CurrentDirectory to whatever directory location the terminal was set to. This meant the Media components were unable to find their C++ dependencies unless I wrapped my media-related commands with code to temporarily change the current directory. I doubt this is your issue but it’s something to be aware of.

Thanks, Joshua!

I was missing the media environment init, but that was still not the problem.

I was debugging and found an inner exception with the following message:

“Connection to image server could not be established. Check whether the correct credentials where supplied!”

Here’s my Program.cs:

static class Program {
 
        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            VideoOS.Platform.SDK.Environment.Initialize();
            VideoOS.Platform.SDK.Media.Environment.Initialize();
            VideoOS.Platform.SDK.Export.Environment.Initialize();
            Console.WriteLine(Environment.CurrentDirectory);
            var success = false;
            Application.Run(new DialogLoginForm(result => success = result));
            if (success) {
                Application.Run(new MediaServer());
            }
        }
 
    }

Is there anything wrong with the authentication process? (I successfully logged in and listed the cameras).

Here’s a link to the repository with this project:

https://github.com/tzm-lpedrozo/xprotect-mediaserver

It’s very simple, I hope it helps solve the mistery.

After all of this, I discovered the app works if I open it on the server, though it doesn’t on client machines.