Need help with imagebox

WMCooper

New member
Joined
Nov 30, 2017
Messages
3
Location
U.S.A.
Programming Experience
5-10
Hi everyone, my background is RPG programming on an IBM so I'm new to C#. This is probably a simple task in C# but my google searching has not been helpful so far so I'm hoping someone here can help me and hopefully this is the right forum for this.
I've gone through online tutorials to make a simple image viewer but they all have a button for the user to click and browse to an image. Is there a way to pass an image file to an image viewer program as a parameter so the viewer displays the image as soon as it opens up?

To go into a little more detail,  what I have is a Microsoft Access database application where a user enters a search term and that brings up an image on a form. When double clicking on the image I want an image viewer to open up with the image displayed and a print button. I know you can print from inside Access but don't like the way it looks and was hoping for a simple c# image viewer that would be a little more user friendly.
The Access code works with mspaint.exe but I want something that only has a print feature, no ability to change or update the image.

What I do have is a simple c# image viewer that has a print button and a browse button but when it opens from within Access it does not automatically show the image. It just comes up with a blank imagebox not getting an image from the Access code the way mspaint.exe does.

Hopefully all that made some sense. :) Is there a sample C# code that does this?

Here is the Access code that opens mspaint

sSourcePath = Me.PicFile
strProg = "C:\Windows\System32\mspaint.exe "
Call Shell(strProg & sSourcePath, vbMaximizedFocus)
 
Last edited:
In the example you provided, the path of the file to open is being passed as a commandline argument. In C#, there are a couple of ways to access arguments passed at the commandline.

Firstly, you can access then the old C/C++ way, i.e. via the Main method. Here's the default code in the Program.cs file in a Windows Forms project in VS 2017:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

You can modify that slightly to give your code access to the commandline arguments:
static void Main(string[] args)
{
    foreach (var arg in args)
    {
        Console.WriteLine(arg);
    }

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}

That gives you access to the commandline arguments before the startup form opens, which may be desirable or it may not. If you want access to the commandline arguments somewhere else, e.g. in the Load event handler of the startup form, then you can do it like this:
var args = Environment.GetCommandLineArgs();

foreach (var arg in args)
{
    Console.WriteLine(arg);
}

Unlike before, this time you will get the path of the EXE as the first value in the array, so your processing needs to take that into account.

The commandline arguments are received as a string array so it's up to you to validate those strings to make sure that they make sense. Such arguments can be used for anything you like, e.g. you might use "-min" to tell your application to start minimised, so the validation will be specific to each app. In your case, you might want to ignore all but the first argument or you might want to generate an error if more than one argument is provided. If an argument is provided but is not a valid image file path, you may just ignore it or you may generate an error. That error might be fatal, i.e. the app closes, or you might notify the user and proceed to open the app as though no argument was passed.

If you want to test processing of commandline arguments in the debugger, open the Debug page of the project properties and there's a field in which you can enter arguments to be passed to your app when you run it in the debugger.
 
Note that the PictureBox control in Windows Forms allows you to assign a file path to the ImageLocation property of the control to display that image. I believe that doing that with an invalid path will display an error image containing a red cross. That means that you may not even have to do any validation on your argument and just let the PictureBox be the feedback to the user.
 
Back
Top Bottom