LEADTOOLS Windows Forms (Leadtools.WinForms assembly)
LEAD Technologies, Inc

AutoDisposeImages Property (RasterImageList)

Example 





Gets or sets a value indicating whether to automatically dispose item images.
Syntax
[CategoryAttribute("Behavior")]
[DescriptionAttribute("Automatically disposes the item images when them item is removed from the control or when the control is disposed.")]
public bool AutoDisposeImages {get; set;}
'Declaration
 
<CategoryAttribute("Behavior")>
<DescriptionAttribute("Automatically disposes the item images when them item is removed from the control or when the control is disposed.")>
Public Property AutoDisposeImages As Boolean
'Usage
 
Dim instance As RasterImageList
Dim value As Boolean
 
instance.AutoDisposeImages = value
 
value = instance.AutoDisposeImages
[CategoryAttribute("Behavior")]
[DescriptionAttribute("Automatically disposes the item images when them item is removed from the control or when the control is disposed.")]
public bool AutoDisposeImages {get; set;}
CategoryAttribute("Behavior")
DescriptionAttribute("Automatically disposes the item images when them item is removed from the control or when the control is disposed.")
 get_AutoDisposeImages();
set_AutoDisposeImages(value);
[CategoryAttribute("Behavior")]
[DescriptionAttribute("Automatically disposes the item images when them item is removed from the control or when the control is disposed.")]
public:
property bool AutoDisposeImages {
   bool get();
   void set (    bool value);
}

Property Value

true to automatically dispose item images; false, otherwise. Default value is true.
Remarks

When the value of the AutoDisposeImages is set to true, the RasterImageList control will automatically call the RasterImage.Dispose method on an item's RasterImageListItem.Image when the item is removed from the Items collection or when the control itself is disposed.

The control will check if an image is used with multiple items (for example, if multiple items are using the same image but to show different pages). In this case, the control will correctly dispose the image only once.

If you setup the items with images that you do not want to dispose of and are going to re-use somewhere else, you need to set the value of the AutoDisposeImages property of the RasterImageList control to false.

You can also temporarily change the value of the AutoDisposeImages property if you want to save the item image before you dispose it. For example, set the AutoDisposeImages property to false, call Remove on the Items collection to remove the item, then re-set the AutoDisposeImages property back to true.

Note that if you set the AutoDisposeImages value to false and you do not dispose the item images yourself, the .NET garbage collector will eventually detect that these images are not used anymore and will dispose them for you when the control and any other reference to these images are no longer used in your application.

Example
 
Public Sub RasterImageList_AutoDisposeImages(ByVal imageList As RasterImageList)
      ' Make sure the AutoDisposeImages property is set to true
      imageList.AutoDisposeImages = True
      MessageBox.Show("Removing an item then accessing its image with AutoDisposeImages set to true")

      ' Remove the first item
      Dim item As RasterImageListItem = imageList.Items(0)
      imageList.Items.RemoveAt(0)

      ' Try to save this item's image into a disk file
      ' This will throw an exception since the item image has been disposed of
      ' as a result of having the AutoDisposeImages false set to true
      Dim codecs As RasterCodecs = New RasterCodecs()
      Try
         codecs.Save(item.Image, Path.Combine(LEAD_VARS.ImagesDir, "Test.jpg"), RasterImageFormat.Jpeg, 24)
         MessageBox.Show("Image saved correctly")
      Catch ex As Exception
         MessageBox.Show(ex.Message)
      End Try

      MessageBox.Show("Removing an item then accessing its image with AutoDisposeImages set to false")

      ' Now remove the next item, this time make sure to set the
      ' AutoDisposeImages property to false before you remove the item
      ' from the control collection
      item = imageList.Items(0)

      imageList.AutoDisposeImages = False
      imageList.Items.RemoveAt(0)
      imageList.AutoDisposeImages = True

      ' Try to save this item's image into a disk file
      ' This time, this should work correcly since the control did not dispose
      ' the item image.
      Try
         codecs.Save(item.Image, Path.Combine(LEAD_VARS.ImagesDir, "Test.jpg"), RasterImageFormat.Jpeg, 24)
         MessageBox.Show("Image saved correctly")
      Catch ex As Exception
         MessageBox.Show(ex.Message)
      End Try
   End Sub

Public NotInheritable Class LEAD_VARS
   Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"
End Class
public void RasterImageList_AutoDisposeImages(RasterImageList imageList)
   {
      // Make sure the AutoDisposeImages property is set to true
      imageList.AutoDisposeImages = true;
      MessageBox.Show("Removing an item then accessing its image with AutoDisposeImages set to true");

      // Remove the first item
      RasterImageListItem item = imageList.Items[0];
      imageList.Items.RemoveAt(0);

      // Try to save this item's image into a disk file
      // This will throw an exception since the item image has been dispose
      // as a result of having the AutoDisposeImages false set to true
      RasterCodecs codecs = new RasterCodecs();
      try
      {
         codecs.Save(item.Image,Path.Combine(LEAD_VARS.ImagesDir,  "Test.jpg"), RasterImageFormat.Jpeg, 24);
         MessageBox.Show("Image saved correctly");
      }
      catch(Exception ex)
      {
         MessageBox.Show(ex.Message);
      }

      MessageBox.Show("Removing an item then accessing its image with AutoDisposeImages set to false");

      // Now remove the next item, this time make sure to set the
      // AutoDisposeImages property to false before you remove the item
      // from the control collection
      item = imageList.Items[0];

      imageList.AutoDisposeImages = false;
      imageList.Items.RemoveAt(0);
      imageList.AutoDisposeImages = true;

      // Try to save this item's image into a disk file
      // This time, this should work correcly since the control did not dispose
      // the item image.
      try
      {
         codecs.Save(item.Image, Path.Combine(LEAD_VARS.ImagesDir, "Test.jpg"), RasterImageFormat.Jpeg, 24);
         MessageBox.Show("Image saved correctly");
      }
      catch(Exception ex)
      {
         MessageBox.Show(ex.Message);
      }
   }

static class LEAD_VARS
{
   public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";
}
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

RasterImageList Class
RasterImageList Members

 

 


Products | Support | Contact Us | Copyright Notices

© 2006-2012 All Rights Reserved. LEAD Technologies, Inc.