将图像直接打开到程序中

时间:2013-09-01 01:24:51

标签: c# winforms command-line-arguments

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

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

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

抱歉英文不好。

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

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

    }
}

2 个答案:

答案 0 :(得分: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());
    }

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

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

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

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

答案 1 :(得分:2)

语句public Form1(String fileName = null)指定一个可选参数。可选参数是在函数中具有默认值的参数,如果指定任何参数,则始终可以选择使用或不使用参数调用函数使用参数代替默认参数,如果在调用函数时没有指定参数,则使用默认参数.optinal参数的默认值必须始终是编译时为常量的值。为了更好澄清一下,让我举一个例子。考虑到我们有一个adds two numbers的函数。

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

我们为函数指定了两个可选参数,上面的函数没有标记任何错误,但正如我所说的可选参数必须有一个在设计时知道的默认值,所以下面的函数标记了{{{因为它使用了在运行时已知的默认值。

Default parameter values must be compile time constant.

考虑变量int z,x; private int AddNumbers(int a=z,int b=x) { int c=a+b; return c; } z是在运行时使用某些逻辑计算的,但在编译时并不知道。这会标记错误。

接下来,让我告诉你x和函数normal function的区别。第一个没有错误编译的函数可以用两种方式调用;

optional parameters

**By passing some arguments when calling**

这将返回20。

AddNumbers(5,15);

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

这将返回25,记住我们将10,15定义为默认参数。这些调用都是有效的,并且编译时没有任何错误。

所以现在我希望你已经找到了问题的答案,如果你想阅读更多看看here.

相关问题