2014-10-11 77 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+它可以做你需要的一切。