为什么错误“文件名不存在”出现?

时间:2015-08-01 22:26:50

标签: c# variables

我正在尝试制作一个将视频分成几帧的按钮,但我无法弄清楚为什么文件名仍有错误。 我没有在OpenFileDialog中定义它吗? 这是代码。

    private void button1_Click(object sender, EventArgs e)
    {

        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
        string fileName = openFileDialog1.FileName;

        }

        FilgraphManager filgraphManager = new FilgraphManager();
        filgraphManager.RenderFile(fileName);
        IBasicVideo bv = (IBasicVideo)filgraphManager;
        int vx, vy;
        bv.GetVideoSize(out vx, out vy);
        var scale = 100F / (float)vx; //100Fのところに出力画像の長辺の長さを入れる
        var w = (int)(scale * vx);
        var h = (int)(scale * vy);

        var md = (IMediaDet)new MediaDet();
        md.Filename = fileName;
        md.CurrentStream = 0;
        string name = @"hoge.bmp";
        md.WriteBitmapBits(5.0d, w, h, name);
    }

在定义filename1时我做错了什么?它是如何解决的?

3 个答案:

答案 0 :(得分:1)

在评论中,我无法编写代码。如果是块,你必须在文件外部定义文件名的类型:

string fileName="";
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
        fileName = openFileDialog1.FileName;

}

if (!string.IsNullOrEmpty(fileName))
// ...

答案 1 :(得分:0)

问题是fileName只在if语句中分配。 这解决了它。

private void button1_Click(object sender, EventArgs e)
    {
        string fileName;
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        if (openFileDialog1.ShowDialog() ==      System.Windows.Forms.DialogResult.OK)
        {
            fileName = openFileDialog1.FileName;


            FilgraphManager filgraphManager = new FilgraphManager();
            filgraphManager.RenderFile(fileName);
            IBasicVideo bv = (IBasicVideo)filgraphManager;
            int vx, vy;
            bv.GetVideoSize(out vx, out vy);
            var scale = 100F / (float)vx; //100Fのところに出力画像の長辺の長さを入れる
            var w = (int)(scale * vx);
            var h = (int)(scale * vy);

            var md = (IMediaDet)new MediaDet();
            md.Filename = fileName;
            md.CurrentStream = 0;
            string name = @"hoge.bmp";
            md.WriteBitmapBits(5.0d, w, h, name);
        }
    }

答案 2 :(得分:0)

你必须在其他名为filename的范围内有一个变量,然后用这里对话框的输出覆盖......但是...

    if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
    string fileName = openFileDialog1.FileName;

    }

    FilgraphManager filgraphManager = new FilgraphManager();
    filgraphManager.RenderFile(fileName);

因此,在RenderFile调用中,文件名可能仍为空。