LEADTOOLS(Leadtoolsアセンブリ)

フレームを表示

Disposedイベント








imageがDisposeメソッドに呼び出しによって破棄されるとき、発生します。
構文
public event EventHandler<EventArgs> Disposed
'Declaration
 
Public Event Disposed As EventHandler(Of EventArgs)
'Usage
 
Dim instance As RasterImage
Dim handler As EventHandler(Of EventArgs)
 
AddHandler instance.Disposed, handler
public event EventHandler<~Remove~> Disposed
add_Disposed(function(sender, e))
remove_Disposed(function(sender, e))

public:
event EventHandler<EventArgs^>^ Disposed
解説

配置されたdelegateを作成するとき、イベントを処理するメソッドを識別します。イベントハンドラでイベントを関連付けるために、イベントにdelegateのインスタンスを追加します。delegateを削除しない限り、イベントが発生するときはいつでも、イベントハンドラは呼び出されます。

このオブジェクトのDisposeメソッドが呼び出されるときはいつでも、破棄されたイベントは発生します。このイベントが発生したあと、RasterImageオブジェクトが破棄されて、もう使われるべきでありません。Disposeイベントにハンドラを削除するよう勧めるので、.NETガーベジコレクタ実行時はその内部使用からオブジェクトを削除することができて、「使っていない」それをマークすることができます。

RasterImageが.NETガーベジコレクタの結果として開放されているならば、他の世界で、オブジェクトファイナライザーは呼び出されています、そして、このイベントは発生しません。

サンプル
Copy Code  
Imports Leadtools
Imports Leadtools.Codecs
Imports Leadtools.ImageProcessing
Imports Leadtools.ImageProcessing.Core
Imports Leadtools.ImageProcessing.Color
Imports Leadtools.Controls
Imports Leadtools.Dicom
Imports Leadtools.Drawing
Imports Leadtools.Svg

Private Sub DisposedEventExample()
   ' Startup
   Dim codecs As New RasterCodecs()
   ' Load an image
   Dim image1 As RasterImage = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "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(Path.Combine(LEAD_VARS.ImagesDir, "cannon.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(Path.Combine(LEAD_VARS.ImagesDir, "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()
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

Public NotInheritable Class LEAD_VARS
Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"
End Class
using Leadtools;
using Leadtools.Codecs;
using Leadtools.ImageProcessing;
using Leadtools.ImageProcessing.Core;
using Leadtools.ImageProcessing.Color;
using Leadtools.Dicom;
using Leadtools.Drawing;
using Leadtools.Controls;
using Leadtools.Svg;

private void DisposedEventExample()
{
   // Startup
   RasterCodecs codecs = new RasterCodecs();
   // Load an image
   RasterImage image1 = codecs.Load(Path.Combine(ImagesPath.Path,"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(Path.Combine(ImagesPath.Path,"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(Path.Combine(ImagesPath.Path,"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();
}

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);
}
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Dicom;
using Leadtools.ImageProcessing;
using Leadtools.ImageProcessing.Core;
using Leadtools.ImageProcessing.Color;
using Leadtools.Examples;
using Leadtools.Windows.Media;

public void DisposedEventExample()
{
   // Create an image
   RasterImage image1 = new RasterImage(RasterMemoryFlags.Conventional, 100, 100, 24, RasterByteOrder.Bgr, RasterViewPerspective.TopLeft, null, null, 0);
   // Add a handler to the Disposed event
   Debug.WriteLine("Adding handler to Disposed for image with format " + image1.OriginalFormat);
   image1.Disposed += new EventHandler<EventArgs>(rasterImage_Disposed);

   // Now dispose the image
   Debug.WriteLine("Calling .Dispose to image with format " + image1.OriginalFormat);
   image1.Dispose();

   // Now use the "using" syntax
   using (RasterImage image2 = new RasterImage(RasterMemoryFlags.Conventional, 100, 100, 24, RasterByteOrder.Bgr, RasterViewPerspective.TopLeft, null, null, 0))
   {
      // Add a handler to the Disposed event
      Debug.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
      Debug.WriteLine("Will be disposed after this, format " + image2.OriginalFormat);
   }

   // Load another image
   RasterImage image3 = new RasterImage(RasterMemoryFlags.Conventional, 100, 100, 24, RasterByteOrder.Bgr, RasterViewPerspective.TopLeft, null, null, 0);

   // Add a handler to the Disposed event
   Debug.WriteLine("Adding handler to Disposed for image with format " + image3.OriginalFormat);
   image3.Disposed += new EventHandler<EventArgs>(rasterImage_Disposed);

   // Do not dispose the image
   Debug.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
}

private void rasterImage_Disposed(object sender, EventArgs e)
{
   // Get the RasterImage object being disposed
   RasterImage image = sender as RasterImage;
   Debug.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);
}
Imports Leadtools
Imports Leadtools.Codecs
Imports Leadtools.Dicom
Imports Leadtools.ImageProcessing
Imports Leadtools.ImageProcessing.Core
Imports Leadtools.ImageProcessing.Color
Imports Leadtools.Windows.Media

Public Sub DisposedEventExample()
   ' Create an image
   Dim image1 As RasterImage = New RasterImage(RasterMemoryFlags.Conventional, 100, 100, 24, RasterByteOrder.Bgr, RasterViewPerspective.TopLeft, Nothing, Nothing, 0)
   ' Add a handler to the Disposed event
   Debug.WriteLine("Adding handler to Disposed for image with format " & image1.OriginalFormat)
   AddHandler image1.Disposed, AddressOf rasterImage_Disposed

   ' Now dispose the image
   Debug.WriteLine("Calling .Dispose to image with format " & image1.OriginalFormat)
   image1.Dispose()

   ' Now use the "using" syntax
   Using image2 As RasterImage = New RasterImage(RasterMemoryFlags.Conventional, 100, 100, 24, RasterByteOrder.Bgr, RasterViewPerspective.TopLeft, Nothing, Nothing, 0)
      ' Add a handler to the Disposed event
      Debug.WriteLine("Adding handler to Disposed for image with format " & image2.OriginalFormat)
      AddHandler image2.Disposed, AddressOf rasterImage_Disposed

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

   ' Load another image
   Dim image3 As RasterImage = New RasterImage(RasterMemoryFlags.Conventional, 100, 100, 24, RasterByteOrder.Bgr, RasterViewPerspective.TopLeft, Nothing, Nothing, 0)

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

   ' Do not dispose the image
   Debug.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
End Sub

Private Sub rasterImage_Disposed(ByVal sender As Object, ByVal e As EventArgs)
   ' Get the RasterImage object being disposed
   Dim image As RasterImage = TryCast(sender, RasterImage)
   Debug.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
   RemoveHandler image.Disposed, AddressOf rasterImage_Disposed
End Sub
必要条件

ターゲットプラットホーム

参照

参照

RasterImageクラス
RasterImageメンバ