【发布时间】:2014-12-30 06:38:01
【问题描述】:
我创建了以下 dll 文件来加载图像和绘制图像
Imports System.Drawing
Namespace GDI
Public Class ImageManager
Public Images As Dictionary(Of String, Image)
Public Sub LoadImage(Name As String, Path As String)
If Images.ContainsKey(Name) Then
Exit Sub
Else
Try
Dim i As Image = Image.FromFile(Path)
Images.Add(Name, i)
Catch ex As Exception
End Try
End If
End Sub
Public Sub RemoveImage(Name As String)
If Images.ContainsKey(Name) Then Images.Remove(Name)
End Sub
Public Sub DrawImage(Surface As Object, Name As String, Position As Point, Optional Size As Point = Nothing)
Try
If Images.ContainsKey(Name) Then
Dim G As Graphics = Surface.CreateGraphics
If Size.IsEmpty Then
G.DrawImage(Images(Name), Position)
Else
G.DrawImage(Images(Name), New Rectangle(Position.X, Position.Y, Size.X, Size.Y))
End If
End If
Catch ex As Exception
End Try
End Sub
End Class
结束命名空间
我使用表单应用程序中的 dll 文件来加载图片。我将两张图片都放在项目调试文件夹中。但是当我尝试运行该项目时。它在
上的 dll 文件中给了我“System.NullReferenceException”f Images.ContainsKey(Name) Then
这是表单应用程序背后的代码:
Imports GFX.GDI
Public Class Form1
Private ImgMan As New ImageManager
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ImgMan.LoadImage("background", "Pic\background.jpg")
ImgMan.LoadImage("download", "Pic\downlaod.jpg")
End Sub
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
ImgMan.DrawImage(Me, "runningman", New Point(0, 0), New Point(Me.Width, Me.Height))
End Sub
End Class
【问题讨论】: