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;
}
}
}
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.
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.