将图像直接打开到程序中

Tah*_*ain 4 c# command-line-arguments winforms

我按照教程在C#窗口中制作了一个基本的图片查看器程序.该程序工作正常,但我想打开它像默认的Windows照片查看器.我试图用程序直接打开图像,但打开程序,图像框为空.

当图像被浏览以在程序内打开但图像框如何在外部工作时,图像框工作正常?

额外:有没有办法让它全屏?

抱歉英文不好.

PS:在帮助时考虑我非常的菜鸟.谢谢 :)

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

    }
}
Run Code Online (Sandbox Code Playgroud)

Lat*_*y81 5

好的,在Program.cs文件中,根据上面注释中的链接实现commmand行参数,并将其传递给表单.

    [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());
    }
Run Code Online (Sandbox Code Playgroud)

然后在您的表单中,将构造函数更改为

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

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

在尝试打开文件之前,你要么想要一个try/catch块,要么检查文件是否存在.根据您描述的用例,我会将丢失的文件视为特殊情况,因此这对我来说似乎是一个计划.