2014-10-11 73 views
-1

我试图教自己编程,并且我正在制作一个自动绘图程序,将数据提交给excel并返回图形。代码如下: 保存文件,然后导出:VB.NET:内存耗尽System.Drawing.dll

“开程序激活,建立一个I/O流数据库和加载图表

Private Sub Form1_Initialized(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated 

    ' Open the database 
    xlWorkBook = xlApp.Workbooks.Open(DatabasePath + "Database.xlsx") 

    ' Set the relevant worksheet 
    xlWorkSheet = xlWorkBook.Sheets("Sheet1") 

    ' Set the display status of the database 
    xlApp.Visible = False 

    ' Clear the picture box before exporting to prevent the compiler from accessing a file already in use 
    If Not (Graph.Image Is Nothing) Then 
     Graph.Image.Dispose() 
    End If 

    ' Export the graph 
    xlWorkSheet.ChartObjects(1).chart.Export(FileName:=DatabasePath + ("Graph.gif"), FilterName:="gif") 

    ' Load the saved graph image into the userform 
    Graph.Image = Image.FromFile(DatabasePath + ("Graph.gif")) 

End Sub 

它不会加载该文件在形式激活虽然。 当我使用另一段代码更新Excel文件中的相关单元并更改绘制的数据时,会出现系统错误。

Private Sub Proposed_Dollars_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Proposed_Dollars.TextChanged 

    ' Export the data values 
    xlWorkSheet.Range("Q10").Value = Proposed_Dollars.Text() 

    ' Clear the picture box before exporting to prevent the compiler from accessing a file already in use 
    If Not (Graph.Image Is Nothing) Then 
     Graph.Image.Dispose() 
    End If 

    ' Export the graph 
    xlWorkSheet.ChartObjects(1).chart.Export(FileName:=DatabasePath + ("Graph.gif"), FilterName:="GIF") 

    ' Load the saved graph image into the userform 
    Graph.Image = Image.FromFile(DatabasePath + ("Graph.gif")) 

End Sub 

是否有我丢失会导致程序的图形不加载到用户窗体PictureBox的任何明显的内存泄漏:细胞在用户窗体使用文本框更改事件更新? 我感谢任何帮助。

+0

是'图表'你的图片框的名字? – Plutonix 2014-10-11 00:34:10

+1

对于输入到文本框中的每个字符,这是一个很大的工作,你不觉得吗? – OneFineDay 2014-10-11 00:35:47

+1

导出后Graph.gif文件有多大?你可以在Paint或PhotoViewer或类似的东西中打开它吗? – 2014-10-11 00:36:08

回答

0

将数据保存回excel文件是好的,但强制它绘制图形的每个字符太多。你应该做的是从excel文件加载一些变量,允许用户改变数据,这将改变变量,然后使用Paint事件绘制整个图形并使用变量显示线条,矩形,椭圆等...看看GDI+它可以做你需要的一切。