直接打开图片到程序 [英] Opening Image directly into program

查看:122
本文介绍了直接打开图片到程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中的窗口实现了从基本的图片浏览器程序,下面的教程。
程序工作正常,但我想打开它像默认的Windows照片查看器。我试图直接与程序中打开图像,但打开该程序,把图像箱子是空的。



在图像浏览里面的打开图像框工作正常程序,但如何使其外部工作?



附加:而且是有办法让它全屏



对不起,英文不好

PS:帮助时,请考虑我很小白。谢谢:)

 命名空间Basic_Picture_Viewer 
{
公共部分Form1类:表格
{
公共Form1中()
{
的InitializeComponent();
}

私人无效showButton_Click(对象发件人,EventArgs五)
{
如果(openFileDialog1.ShowDialog()== DialogResult.OK)
{
pictureBox1.Load(openFileDialog1.FileName);
}
}

私人无效clearButton_Click(对象发件人,EventArgs五)
{
pictureBox1.Image = NULL;
}

私人无效backgroundButton_Click(对象发件人,EventArgs五)
{
如果(colorDialog1.ShowDialog()== DialogResult.OK)
{
pictureBox1.BackColor = colorDialog1.Color;
}
}

私人无效closeButton_Click(对象发件人,EventArgs五)
{
ActiveForm.Close();
}

私人无效checkBox1_CheckedChanged(对象发件人,EventArgs五)
{
如果(checkBox1.Checked)
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
,否则
pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
}

私人无效openFileDialog1_FileOk(对象发件人,发送CancelEventArgs E)
{

}

私人无效Form1_Load的(对象发件人,EventArgs五)
{

}

私人无效rotateButton_Click(对象发件人,EventArgs五)
{
如果(pictureBox1。 !图片= NULL)
{
图片IMG = pictureBox1.Image;
img.RotateFlip(RotateFlipType.Rotate90FlipNone);
pictureBox1.Image = IMG;
}

}
}


解决方案

好了,在你的Program.cs文件,根据上述评论的链接实现了条命令行参数,并将其传递到窗体。

  [STAThread] 
静态无效的主要(字串[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(假);

如果(args.Length大于0)
Application.Run(新Form1的(参数[0]));
,否则
Application.Run(新Form1的());
}



然后在你的形式,构造改为

 公共Form1中(字符串文件名= NULL)
{
的InitializeComponent();

如果(!文件名= NULL)
{
//添加验证,以确保文件存在,这里
this.WindowState = FormWindowState.Maximized;
pictureBox1.Load(文件名);
}
}

您会要么需要一个try / catch块或东西试图打开它之前检查文件是否存在。根据你的描述,我会考虑在丢失文件的特殊情况,所以这似乎是一个计划,我的用例。


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.

这篇关于直接打开图片到程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆