Leadtools.WinForms Send comments on this topic. | Back to Introduction - All Topics | Help Version 16.5.9.25
AutoDisposeImages Property
See Also  Example
Leadtools.WinForms Namespace > RasterImageViewer Class : AutoDisposeImages Property



Gets or sets a value indicating whether to auto dispose the old image when a new image is set into this RasterImageViewer.

Syntax

Visual Basic (Declaration) 
<DescriptionAttribute("Indicating whether to auto dispose the old image when a new image is set into this control")>
<CategoryAttribute("Behavior")>
Public Overridable Property AutoDisposeImages As Boolean
Visual Basic (Usage)Copy Code
Dim instance As RasterImageViewer
Dim value As Boolean
 
instance.AutoDisposeImages = value
 
value = instance.AutoDisposeImages
C# 
[DescriptionAttribute("Indicating whether to auto dispose the old image when a new image is set into this control")]
[CategoryAttribute("Behavior")]
public virtual bool AutoDisposeImages {get; set;}
C++/CLI 
[DescriptionAttribute("Indicating whether to auto dispose the old image when a new image is set into this control")]
[CategoryAttribute("Behavior")]
public:
virtual property bool AutoDisposeImages {
   bool get();
   void set (bool value);
}

Return Value

true if the image in Image is automatically disposed when a new image is set; otherwise, false. Default value is true.

Example

This sample loads two images to demonstrate how the AutoDisposeImages property effects whether the item image remains usable.

Visual BasicCopy Code
Private Sub viewer_ImageChanged(ByVal sender As Object, ByVal e As EventArgs)
    Dim viewer As RasterImageViewer = CType(IIf(TypeOf sender Is RasterImageViewer, sender, Nothing), RasterImageViewer)
    Dim s As String
    If Not viewer.Image Is Nothing Then
        s = String.Format("Image Changed: New Width {0}, NewHeight {1}", viewer.Image.Width, viewer.Image.Height)
    Else
        s = "No Image"
    End If
    Console.WriteLine(s)
End Sub

Public Sub RasterImageViewer_AutoDisposeImages(ByVal viewer As RasterImageViewer)
    ' Load two image2
    RasterCodecs.Startup()
    Dim codecs As RasterCodecs = New RasterCodecs()
    Dim file1 As String = LeadtoolsExamples.Common.ImagesPath.Path + "Image1.cmp"
    Dim file2 As String = LeadtoolsExamples.Common.ImagesPath.Path + "Image2.cmp"

    AddHandler viewer.ImageChanged, AddressOf viewer_ImageChanged

    Dim image1 As RasterImage = codecs.Load(file1)
    Dim image2 As RasterImage = codecs.Load(file2)

    ' Make sure the AutoDisposeImages property of the viewer is set to true
    viewer.AutoDisposeImages = True

    ' Set the first image into the viewer
    viewer.Image = image1

    ' Now set the second image into the viewer (this disposes the first image since the AutoDisposeImages property is true)
    viewer.Image = image2

    ' Try to access the first image, it generates a NullReferenceException exception since the image has been disposed
    MessageBox.Show("Try to access the first image, it generates a NullReferenceException exception since the image has been disposed.")
    Try
        Dim width As Integer = image1.Width
        MessageBox.Show(String.Format("Width is {0} pixels", width))
    Catch ex As NullReferenceException
        MessageBox.Show(ex.Message)
    End Try

    ' Set the image to null (will dispose the second image as well)
    viewer.Image = Nothing

    ' Now re-load the two images
    image1 = codecs.Load(file1)
    image2 = codecs.Load(file2)

    ' Make sure the AutoDisposeImages property of the viewer is set to false
    viewer.AutoDisposeImages = False

    ' Set the first image into the viewer
    viewer.Image = image1

    ' Now set the second image into the viewer (this will not disposes the first image since the AutoDisposeImages property is false)
    viewer.Image = image2

    ' Try to access the first image, it should work fine this time
    Try
        Dim width As Integer = image1.Width
        MessageBox.Show(String.Format("Width is {0} pixels", width))
    Catch ex As NullReferenceException
        MessageBox.Show(ex.Message)
    End Try

    ' Set the image to null (will not dispose the second image)
    viewer.Image = Nothing

    ' We should now dispose the two images manually
    image1.Dispose()
    image2.Dispose()

    RemoveHandler viewer.ImageChanged, AddressOf viewer_ImageChanged
End Sub
C#Copy Code
private void viewer_ImageChanged(object sender, EventArgs e) 

   RasterImageViewer viewer = sender as RasterImageViewer; 
   string s; 
   if (viewer.Image != null) 
      s = string.Format("Image Changed: New Width {0}, NewHeight {1}", viewer.Image.Width, viewer.Image.Height); 
   else s = "No Image"; 
   Console.WriteLine(s); 

 
public void RasterImageViewer_AutoDisposeImages(RasterImageViewer viewer) 

   // Load two image2 
   RasterCodecs.Startup(); 
   RasterCodecs codecs = new RasterCodecs(); 
   string file1 = LeadtoolsExamples.Common.ImagesPath.Path + "Image1.cmp"; 
   string file2 = LeadtoolsExamples.Common.ImagesPath.Path + "Image2.cmp"; 
 
   viewer.ImageChanged += new EventHandler(viewer_ImageChanged); 
 
   RasterImage image1 = codecs.Load(file1); 
   RasterImage image2 = codecs.Load(file2); 
 
   // Make sure the AutoDisposeImages property of the viewer is set to true 
   viewer.AutoDisposeImages = true; 
 
   // Set the first image into the viewer 
   viewer.Image = image1; 
 
   // Now set the second image into the viewer (this disposes the first image since the AutoDisposeImages property is true) 
   viewer.Image = image2; 
 
   // Try to access the first image, it generates a NullReferenceException exception since the image has been disposed 
   MessageBox.Show("Try to access the first image, it generates a NullReferenceException exception since the image has been disposed."); 
   try 
   { 
      int width = image1.Width; 
      MessageBox.Show(string.Format("Width is {0} pixels", width)); 
   } 
   catch (NullReferenceException ex) 
   { 
      MessageBox.Show(ex.Message); 
   } 
 
   // Set the image to null (will dispose the second image as well) 
   viewer.Image = null; 
 
   // Now re-load the two images 
   image1 = codecs.Load(file1); 
   image2 = codecs.Load(file2); 
 
   // Make sure the AutoDisposeImages property of the viewer is set to false 
   viewer.AutoDisposeImages = false; 
 
   // Set the first image into the viewer 
   viewer.Image = image1; 
 
   // Now set the second image into the viewer (this will not disposes the first image since the AutoDisposeImages property is false) 
   viewer.Image = image2; 
 
   // Try to access the first image, it should work fine this time 
   try 
   { 
      int width = image1.Width; 
      MessageBox.Show(string.Format("Width is {0} pixels", width)); 
   } 
   catch (NullReferenceException ex) 
   { 
      MessageBox.Show(ex.Message); 
   } 
 
   // Set the image to null (will not dispose the second image) 
   viewer.Image = null; 
 
   // We should now dispose the two images manually 
   image1.Dispose(); 
   image2.Dispose(); 
 
   viewer.ImageChanged -= new EventHandler(viewer_ImageChanged); 
}

Remarks

By default, whenever you set a new image into the Image property, the old image (if any) is disposed. Set the value of this property to false to prevent disposing of the old image.

Requirements

Target Platforms: Microsoft .NET Framework 2.0, Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also