-1

I created the following dll file to load image and draw image

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

End Namespace

I use the dll file in the form application to load the picture. I put both pictures in the project debug folder. But when I try to run the project. It gives me the ""System.NullReferenceException" in the dll file on

f Images.ContainsKey(Name) Then

Here is my code behind the form application:

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
Ivancao
  • 11
  • 8
  • Daily duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Bjørn-Roger Kringsjå Dec 30 '14 at 08:32

1 Answers1

0

You have not initialized the Images dictionary in the code above. Edit the declaration as below:

Public Images As new Dictionary(Of String, Image)
danish
  • 5,550
  • 2
  • 25
  • 28