VB.NET:System.Drawing.dll中的内存不足

VB.NET:System.Drawing.dll中的内存不足,vb.net,Vb.net,我试图自学编程,我正在制作一个自动绘图程序,将数据提交到excel并返回图形。代码如下: 保存文件并将其导出: '在程序激活时,为数据库建立I/O流并加载图形 Private Sub Form1_Initialized(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated ' Open the database xlWorkBook = xlApp.Workbooks.Ope

我试图自学编程,我正在制作一个自动绘图程序,将数据提交到excel并返回图形。代码如下: 保存文件并将其导出:

'在程序激活时,为数据库建立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文件中的相关单元格并更改绘制的数据。使用userform中的textbox更改事件更新单元格:

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的图形?
非常感谢您提供的任何帮助。

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

Graph
图片盒的名称吗?你不认为输入文本框的每个字符都需要做很多工作吗?导出后的Graph.gif文件有多大?你能用油漆或PhotoViewer或类似的东西打开它吗?是的,对不起,Graph是picturebox的名称。我意识到输入文本框中的每个字符都需要做大量的工作。我将实现一个“更新图形”按钮,以帮助减轻一些处理需求。