Opening an image directly in the program

I made a basic program for viewing images in C # windows, starting with a tutorial. The program works fine, but I want to open it as the default windows viewer. I tried to open the image using the program directly, but it opened the program, and the image window was empty.

The image box works fine when images are viewed to open inside the program, but how to make it work from the outside?

Optional: and is there a way to do this full screen?

Sorry for the bad english.

PS: Think, I will help a lot when I help. Thanks:)

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; } } } 
+4
source share
2 answers

Okay, so in the Program.cs file, implement the arguments of the commmand line according to the link in the comment above and submit 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 will either want a try / catch block, or something to check for the existence of the file before trying to open it. In the example used, you describe that I consider the missing file to be an exceptional case, so this looks like a plan.

+5
source

The declaration 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 opportunity to either call a function with or without an argument, if you specify an argument, the new argument is used instead of the default argument, and if you do not specify an argument when calling the function, the default argument is used. The default value for the optinal argument should always be a value that is constant at compile time. To better clarify, let me give you an example. Consider that we have a function that adds two numbers .

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

We specified two optional arguments for the function, the above functions are not marked with any error, but, as I said, the optional arguments must have a default value that is known at design time, so the function below indicates the 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; } 

Note that the variables z and x are computed using some logic at runtime, but are not unknown at compile time. That would mean a mistake.

Next, let me tell you about the differences in normal function and functionality with optional parameters . The first function that compiles without 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 that we defined 10.15 as default arguments. But the calls are valid and will be compiled without errors.

So, now I hope you find the answer to your question, if you want to read more, look here.

+2
source

Source: https://habr.com/ru/post/1499980/


All Articles