Leadtools Send comments on this topic. | Back to Introduction - All Topics | Help Version 16.5.9.25
Disposed Event
See Also  Example
Leadtools Namespace > RasterImage Class : Disposed Event



Occurs when the image is disposed by a call to the Dispose method.

Syntax

Visual Basic (Declaration) 
Public Event Disposed() As EventHandler(Of EventArgs)
Visual Basic (Usage)Copy Code
Dim instance As RasterImage
Dim handler As EventHandler(Of EventArgs)
 
AddHandler instance.Disposed, handler
C# 
public event EventHandler<EventArgs> Disposed()
C++/CLI 
public:
event EventHandler<EventArgs>^ Disposed();

Example

Visual BasicCopy Code
Private Sub DisposedEventExample()
   ' Startup
   RasterCodecs.Startup()
   Dim codecs As New RasterCodecs()
   ' Load an image
   Dim image1 As RasterImage = codecs.Load("C:\Users\Public\Documents\LEADTOOLS Images\Image1.cmp")

   ' Add a handler to the Disposed event
   Console.WriteLine("Adding handler to Disposed for image with format " + image1.OriginalFormat.ToString())
   AddHandler image1.Disposed, AddressOf rasterImage_Disposed

   ' Now dispose the image
   Console.WriteLine("Calling .Dispose to image with format " + image1.OriginalFormat.ToString())
   image1.Dispose()

   ' Now use the "using" syntax
   Using image2 As RasterImage = codecs.Load("C:\Users\Public\Documents\LEADTOOLS Images\Master.jpg")
      ' Add a handler to the Disposed event
      Console.WriteLine("Adding handler to Disposed for image with format " + image2.OriginalFormat.ToString())
      AddHandler image2.Disposed, AddressOf rasterImage_Disposed

      ' The event should occur at the next line
      Console.WriteLine("Will be disposed after this, format " + image2.OriginalFormat.ToString())
   End Using

   ' Load another image
   Dim image3 As RasterImage = codecs.Load("C:\Users\Public\Documents\LEADTOOLS Images\Ocr1.tif")

   ' Add a handler to the Disposed event
   Console.WriteLine("Adding handler to Disposed for image with format " + image3.OriginalFormat.ToString())
   AddHandler image3.Disposed, AddressOf rasterImage_Disposed

   ' Do not dispose the image
   Console.WriteLine("Never disposing image with format " + image3.OriginalFormat.ToString())

   ' Notice that the event will never occur for image3. The image is not going to be disposed
   ' only its finalizer called. The garbage collector cannot guarantee that our event handler
   ' is still a valid object. This scenario creates resource leaks and it is recommended that
   ' you always dispose and object that implements the IDisposable interface, like we did
   ' with image1 and image2

   codecs.Dispose()
   RasterCodecs.Shutdown()
End Sub

Private Sub rasterImage_Disposed(ByVal sender As Object, ByVal e As EventArgs)
   ' Get the RasterImage object being disposed
   Dim image As RasterImage = DirectCast(sender, RasterImage)
   Console.WriteLine("Being disposed, image with format " + image.OriginalFormat.ToString())

   ' Remove the handler to decouple the object from our application
   ' and let the garbage collector take over
   RemoveHandler image.Disposed, AddressOf rasterImage_Disposed
End Sub
C#Copy Code
private void DisposedEventExample() 

   // Startup 
   RasterCodecs.Startup(); 
   RasterCodecs codecs = new RasterCodecs(); 
   // Load an image 
   RasterImage image1 = codecs.Load(@"C:\Users\Public\Documents\LEADTOOLS Images\Image1.cmp"); 
 
   // Add a handler to the Disposed event 
   Console.WriteLine("Adding handler to Disposed for image with format " + image1.OriginalFormat); 
   image1.Disposed += new EventHandler<EventArgs>(rasterImage_Disposed); 
 
   // Now dispose the image 
   Console.WriteLine("Calling .Dispose to image with format " + image1.OriginalFormat); 
   image1.Dispose(); 
 
   // Now use the "using" syntax 
   using(RasterImage image2 = codecs.Load(@"C:\Users\Public\Documents\LEADTOOLS Images\Master.jpg")) 
   { 
      // Add a handler to the Disposed event 
      Console.WriteLine("Adding handler to Disposed for image with format " + image2.OriginalFormat); 
      image2.Disposed += new EventHandler<EventArgs>(rasterImage_Disposed); 
 
      // The event should occur at the next line 
      Console.WriteLine("Will be disposed after this, format " + image2.OriginalFormat); 
   } 
 
   // Load another image 
   RasterImage image3 = codecs.Load(@"C:\Users\Public\Documents\LEADTOOLS Images\Ocr1.tif"); 
 
   // Add a handler to the Disposed event 
   Console.WriteLine("Adding handler to Disposed for image with format " + image3.OriginalFormat); 
   image3.Disposed += new EventHandler<EventArgs>(rasterImage_Disposed); 
 
   // Do not dispose the image 
   Console.WriteLine("Never disposing image with format " + image3.OriginalFormat); 
 
   // Notice that the event will never occur for image3. The image is not going to be disposed 
   // only its finalizer called. The garbage collector cannot guarantee that our event handler 
   // is still a valid object. This scenario creates resource leaks and it is recommended that 
   // you always dispose and object that implements the IDisposable interface, like we did 
   // with image1 and image2 
 
   codecs.Dispose(); 
   RasterCodecs.Shutdown(); 

 
private void rasterImage_Disposed(object sender, EventArgs e) 

   // Get the RasterImage object being disposed 
   RasterImage image = sender as RasterImage; 
   Console.WriteLine("Being disposed, image with format " + image.OriginalFormat); 
 
   // Remove the handler to decouple the object from our application 
   // and let the garbage collector take over 
   image.Disposed -= new EventHandler<EventArgs>(rasterImage_Disposed); 
}

Remarks

When you create a Disposed delegate, you identify the method that handles the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate.

The Disposed event will fire whenver the Dispose method of this object is called. After this event occurs, the RasterImage object is disposed and should not be used anymore. It is recommedned that you remove the handler to the Dispose event so the .NET Garbage Collector runtime can remove the object from its internal use and mark it as "ununsed".

If the RasterImage is being freed as a result of the .NET Garbage Collector, in other worlds, the object finalizer is being called, then this event will not occur.

Requirements

Target Platforms: Microsoft .NET Framework 3.0, Windows XP, Windows Server 2003 family, Windows Server 2008 family

See Also