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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With