【问题标题】:removing image reference from picturebox (C#)从图片框中删除图像引用(C#)
【发布时间】:2013-02-04 14:47:54
【问题描述】:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace MyProject
{
    public partial class MyForm : Form
    {
        Process MyProcess;

        public MyForm()
        {
            InitializeComponent();
        }

        private void MyForm_KeyPress(object sender, KeyPressEventArgs e)
        {
            switch (e.KeyChar)
            {
                case 'a':
                    this.MyPictureBox.Image = null;
                    this.MyProcess = new Process();
                    this.MyProcess.StartInfo =
                        new ProcessStartInfo("\"C:\\Program Files (x86)\\LilyPond\\usr\\bin\\lilypond.exe\"", "--png tmp.ly");
                    this.MyProcess.Start();
                    this.MyProcess.WaitForExit();
                    this.MyPictureBox.Image = new Bitmap("tmp.png");
                    break;
                default:
                    break;
            }
        }
    }
}

"C:\Program Files (x86)\LilyPond\usr\bin\lilypond.exe" --png tmp.ly 命令创建tmp.png。当我第一次按a 键时,MyProcess 返回 0,但下一个 - 总是返回 1。我认为问题在于覆盖了 MyPictureBox 使用的文件tmp.png,但我不知道如何修复它。你能帮帮我吗?

【问题讨论】:

标签: c# lilypond


【解决方案1】:

据我所知,lilypond 没有提供使用命令行参数进行覆盖的选项。如果我是对的,那么您可以在 MyProcess 启动之前包含删除 png 文件(如果存在)的代码。

if (System.IO.File.Exists(path))
{
    System.IO.File.Delete(path);
}

【讨论】:

  • 我应该在哪里插入该代码?在this.MyPictureBox.Image = null; 在private void MyForm_KeyPress(object sender, KeyPressEventArgs e) 之后?我得到了 IOException (The process cannot access the file << path >> because it is being used by another process.)
  • 在“MyProcess.Start();”之前确定假设在“case 'a':”之后添加它
  • 很抱歉,您的建议并不能解决我的问题,因为问题在于 MyPictureBox 使用 png 文件。我发现好的方法是使用MyPictureBox.Image.Dispose() 方法。
  • 使用图片加载.png怎么样? "Image image = Image.FromFile(path); MyPictureBox.Image = image;"
猜你喜欢
  • 2018-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-05
  • 2017-07-02
  • 2020-01-05
相关资源
最近更新 更多