0

I made an image viewer app for learning purposes. I wanted to add a functional code for using the app with open with command in folders ,thus I added a argument code for it but the code seems to not work.Whenever I try to open it with the open with dialog the program does not even start I tried a new form method too, but now changed to pictureBox method. It is not working plus Iam using AdvancedInstaller I want to add a method of Registering the program on target computer. Here is the code


#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
using namespace System::Drawing;
using namespace System::IO;
[STAThread]
int main(array<String^>^ args)
{
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false);
    ImageViewer::MyForm^ form = gcnew ImageViewer::MyForm();
    if (args->Length >0)
    {
        String^ filePath = args[0];
        if (System::IO::File::Exists(filePath))
        {
            Stream^ st = File::OpenRead(filePath);

            Image^ ig = Image::FromStream(st);
            st->Close();

                PictureBox ^ pB = dynamic_cast<PictureBox^>(Application::OpenForms[0]->Controls["pictureBox1"]);
            pB->Image = ig;
        }
    }
    else {
        Application::Run(form);
    } 
    return 0;
    
}

I added the code in form.cpp file.

I tried to change the code piece from new form to picture box. I did not try the registry method as it had some risk potentials

Ahmed
  • 1
  • You're calling `Application::Run()` only when `args->Length == 0` -- BTW, create the Image like this: `Image^ image = Image::FromStream(gcnew MemoryStream(File::ReadAllBytes(args[0])), true, false);` (you should also check whether that's actually a supported Image, or catch the exception) -- I'd prefer to add a public method to the Form, say `public: System::Void SetImage(Image^ image); { }`, that sets the Image property of a PictureBox with the object it receives. So you can then just have `form->SetImage(image);` – Jimi May 08 '23 at 20:36
  • OH! thanks for the response I corrected my mistake. In my view I will create a registry for specific images so there is no need to add a catch exception instead add a !nullptr in main form – Ahmed May 12 '23 at 18:27

0 Answers0