4

I made a basic picture viewer program in C# windows from , following a tutorial. The program works fine but I want to open it like default windows photo viewer. I tried to open an image with the program directly but that opened the program and the image box was empty.

The image box works fine when images are browsed to open inside the program but how to make it work externally?

Extra : And is there a way to make it full screen?

Sorry for bad english.

P.S: Consider me very noob when helping. Thank you :)

namespace Basic_Picture_Viewer
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void showButton_Click(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            pictureBox1.Load(openFileDialog1.FileName);
        }
    }

    private void clearButton_Click(object sender, EventArgs e)
    {
        pictureBox1.Image = null;
    }

    private void backgroundButton_Click(object sender, EventArgs e)
    {
        if (colorDialog1.ShowDialog() == DialogResult.OK)
        {
            pictureBox1.BackColor = colorDialog1.Color;
        }
    }

    private void closeButton_Click(object sender, EventArgs e)
    {
        ActiveForm.Close();
    }

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (checkBox1.Checked)
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
        else
            pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
    }

    private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
    {

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void rotateButton_Click(object sender, EventArgs e)
    {
        if (pictureBox1.Image != null)
        {
            Image img = pictureBox1.Image;
            img.RotateFlip(RotateFlipType.Rotate90FlipNone);
            pictureBox1.Image = img;
        }

    }
}
Taha Rushain
  • 635
  • 1
  • 9
  • 26
  • 1
    http://stackoverflow.com/questions/1179532/how-do-i-pass-command-line-arguments-to-a-winforms-application shows how to get the command line arguments, from there you can pass them along to the methods which populate the imaage box – Lathejockey81 Sep 01 '13 at 01:31
  • Can you explain it a little bit and help me implement in my problem – Taha Rushain Sep 01 '13 at 01:41
  • 1
    Not without some code that shows where it needs to happen. The example in the link shows exactly what you need to know to get the command line arguments, and I don't know where to send them without some code. – Lathejockey81 Sep 01 '13 at 01:46
  • You need to get the filename from command-line args. I've retagged your question to get you the right crowd :) – Mathieu Guindon Sep 01 '13 at 01:47
  • @Lathejockey81 I addded the code , kindly have a look – Taha Rushain Sep 01 '13 at 01:51

2 Answers2

5

Okay, so in your Program.cs file, implement the commmand line arguments according to the link in the comment above, and pass it to the form.

    [STAThread]
    static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        if(args.Length > 0)
            Application.Run(new Form1(args[0]));
        else
            Application.Run(new Form1());
    }

Then in your form, change the constructor to

public Form1(String fileName = null)
{
    InitializeComponent();

    if (fileName != null)
    {
        // Add validation to ensure file exists here
        this.WindowState = FormWindowState.Maximized;
        pictureBox1.Load(fileName);
    }
}

You'll either want a try/catch block or something to check for the existence of the file before attempting to open it. Under the use case you describe I would consider a missing file an exceptional case, so that seems like a plan to me.

Lathejockey81
  • 1,198
  • 7
  • 8
  • I added them exactly as you said but iam getting some error: Error 1 'System.Array' does not contain a definition for 'length' and no extension method 'length' accepting a first argument of type 'System.Array' could be found. Error 2 'Basic_Picture_Viewer.Form1' does not contain a constructor that takes 1 argument Error 3 and 4 : fileName does not exist in context – Taha Rushain Sep 01 '13 at 02:04
  • Ugh. too much javascript this week... Count is the property - I'll update. For the second error, did you add the optional property to the Form1 constructor? – Lathejockey81 Sep 01 '13 at 02:07
  • No... How am I supposed to do that? – Taha Rushain Sep 01 '13 at 02:11
  • The first line of the second code snippet is the constructor. You'll note that inside the parenthesis is `String fileName = null` – Lathejockey81 Sep 01 '13 at 02:12
  • One last error , in if(args.Count > 0) Error 1 Operator '>' cannot be applied to operands of type 'method group' and 'int' – Taha Rushain Sep 01 '13 at 02:14
  • By using this : if (args.Count() > '0') Application.Run(new Form1(args[0])); Gives Index out of bound error – Taha Rushain Sep 01 '13 at 02:19
  • I'm more tired than I thought. Length does work, but maybe you didn't have it capitalized correctly. Count is a method, so it must be written as .Count() to work. One more update. – Lathejockey81 Sep 01 '13 at 02:20
  • Awesome it works! Can you explain this command? Why we give parameter filename = null , wont this always make filename null? public Form1(String fileName = null) – Taha Rushain Sep 01 '13 at 02:31
2

The statement public Form1(String fileName = null) specifies one optional argument.Optional arguments are arguments that have a default value in the function,you always have the option to either call the function with or without argument,if you specify any argument the new argument is used in place of default argument,and in case you don't specify an argument when calling the function the default argument is used.The default value to optinal argument must always be a value that is a constant at compile time.To better clarify that let me take an example.Consider we have a function that adds two numbers.

private int AddNumbers(int a=10,int b=15)
{
  int c=a+b;
  return c;
}

We have specifies two optional arguments for the function,the above functions doesn't flag any error but as I said optional arguments must have a default value which is know at design time so the function below flags an error that follows Default parameter values must be compile time constant. because it uses default values that will be known at runtime.

int z,x;
private int AddNumbers(int a=z,int b=x)
{
 int c=a+b;
 return c;
}

Consider that the variables z and x are calculated using some logic at runtime,but are not unknown at compile time.This would flag the error.

Next,let me tell you differences in a normal function and a functional with optional parameters.The first function that compiles with no errors can be called in two ways;

**By passing some arguments when calling**

AddNumbers(5,15);

This will return 20.

**By calling the function without specifying any arguments**

AddNumbers();

This will return 25,remember we defined 10,15 as default arguments.Both the calls are valid and will compile without any errors.

So now I hope you've found answer to your question,if you would like to read more have a look here.

devavx
  • 1,035
  • 9
  • 22