2013-02-04 62 views
0
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,但next - 总是返回1。我认为问题在于覆盖文件tmp.png,这是由MyPictureBox使用,但我不知道如何修复它。你可以帮帮我吗?从图片框中删除图像引用(C#)

+0

删除它,如果存在。检查这个职位:http://stackoverflow.com/questions/8905714/overwrite-existing-image –

回答

1

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

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

我应该在哪里插入代码?在'this.MyPictureBox.Image = null;'private void MyForm_KeyPress(object sender,KeyPressEventArgs e)''后面?我得到了IOException('进程无法访问文件<< path >>,因为它正在被另一个进程使用.') – pt12lol

+0

肯定之前之前“MyProcess.Start();”比方说,在“case'a'之后加上:” – 2013-02-04 15:46:57

+0

对不起,但你的建议并不能解决我的问题,因为问题在于MyPictureBox使用png文件。我发现很好的方法是使用'MyPictureBox.Image.Dispose()'方法。 – pt12lol