Occurs when the image is disposed by a call to the
Dispose method.
Syntax
Example
Visual Basic | Copy Code |
---|
Private Sub DisposedEventExample()
RasterCodecs.Startup()
Dim codecs As New RasterCodecs()
Dim image1 As RasterImage = codecs.Load("C:\Users\Public\Documents\LEADTOOLS Images\Image1.cmp")
Console.WriteLine("Adding handler to Disposed for image with format " + image1.OriginalFormat.ToString())
AddHandler image1.Disposed, AddressOf rasterImage_Disposed
Console.WriteLine("Calling .Dispose to image with format " + image1.OriginalFormat.ToString())
image1.Dispose()
Using image2 As RasterImage = codecs.Load("C:\Users\Public\Documents\LEADTOOLS Images\Master.jpg")
Console.WriteLine("Adding handler to Disposed for image with format " + image2.OriginalFormat.ToString())
AddHandler image2.Disposed, AddressOf rasterImage_Disposed
Console.WriteLine("Will be disposed after this, format " + image2.OriginalFormat.ToString())
End Using
Dim image3 As RasterImage = codecs.Load("C:\Users\Public\Documents\LEADTOOLS Images\Ocr1.tif")
Console.WriteLine("Adding handler to Disposed for image with format " + image3.OriginalFormat.ToString())
AddHandler image3.Disposed, AddressOf rasterImage_Disposed
Console.WriteLine("Never disposing image with format " + image3.OriginalFormat.ToString())
codecs.Dispose()
RasterCodecs.Shutdown()
End Sub
Private Sub rasterImage_Disposed(ByVal sender As Object, ByVal e As EventArgs)
Dim image As RasterImage = DirectCast(sender, RasterImage)
Console.WriteLine("Being disposed, image with format " + image.OriginalFormat.ToString())
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
Requirements
Target Platforms: Microsoft .NET Framework 3.0, Windows XP, Windows Server 2003 family, Windows Server 2008 family
See Also