【问题标题】:Opening Image directly into program直接在程序中打开图像
【发布时间】:2013-09-04 10:47:34
【问题描述】:

我按照教程在 C# windows 中制作了一个基本的图片查看器程序。 该程序运行良好,但我想像默认的 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;
        }

    }
}

【问题讨论】:

  • stackoverflow.com/questions/1179532/… 展示了如何获取命令行参数,从那里您可以将它们传递给填充图像框的方法
  • 你能解释一下并帮助我解决我的问题
  • 不是没有一些代码来显示它需要发生的地方。链接中的示例准确显示了获取命令行参数所需了解的内容,如果没有一些代码,我不知道将它们发送到哪里。
  • 您需要从命令行参数获取文件名。我已经重新标记了您的问题,以便为您找到合适的人群:)
  • @retailcoder 谢谢

标签: c# winforms command-line-arguments


【解决方案1】:

语句public Form1(String fileName = null) 指定了一个可选参数。可选参数是在函数中具有默认值的参数,您始终可以选择带或不带参数调用函数,如果您指定任何参数,则新参数是用于代替默认参数,如果在调用函数时未指定参数,则使用默认参数。可选参数的默认值必须始终是编译时常量的值。为了更好地阐明这一点让我举个例子。假设我们有一个函数adds two numbers

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

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

int z,x;
private int AddNumbers(int a=z,int b=x)
{
 int c=a+b;
 return c;
}

考虑变量zx 在运行时使用一些逻辑计算,但在编译时不是未知的。这将标记错误。

接下来,让我告诉你normal functionoptional parameters的函数之间的区别。第一个编译没有错误的函数可以通过两种方式调用;

**By passing some arguments when calling**

AddNumbers(5,15);

这将返回 20。

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

AddNumbers();

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

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

【讨论】:

    【解决方案2】:

    好的,所以在您的 Program.cs 文件中,根据上面评论中的链接实现命令行参数,并将其传递给表单。

        [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 ​​'System.Array' 不包含 'length' 的定义,并且没有扩展方法 'length' 接受类型的第一个参数可以找到“System.Array”。错误 2“Basic_Picture_Viewer.Form1”不包含采用 1 个参数的构造函数错误 3 和 4:上下文中不存在文件名
    • 呃。本周 JavaScript 太多...计数是属性 - 我会更新。对于第二个错误,您是否将可选属性添加到 Form1 构造函数?
    • 不...我该怎么做?
    • 第二个代码sn -p的第一行是构造函数。你会注意到括号里面是String fileName = null
    • 最后一个错误,在 if(args.Count > 0) 错误 1 ​​运算符“>”不能应用于“方法组”和“整数”类型的操作数
    猜你喜欢
    • 2018-10-21
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    • 2014-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-20
    相关资源
    最近更新 更多