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

RasterThumbnailBrowser Class

Example 





Members 
Derives from the RasterImageList control to provide functionality to automatically generate thumbnails for images in a disk folder.
Object Model
RasterThumbnailBrowser ClassRasterImageListItemCollection ClassRasterImageListItem ClassRasterImageListItemCollection ClassRasterImageListItem Class
Syntax
'Usage
 
Dim instance As RasterThumbnailBrowser
function Leadtools.WinForms.RasterThumbnailBrowser()
Remarks

The RasterThumbnailBrowser lets you quickly and easily create thumbnails for images that reside on any file folder in the system. Since the RasterThumbnailBrowser derives from the RasterImageList control, you get all the visual styles available to the image list (different view styles, control the item size, color, etc.) as well as the same interface to manually add/remove/edit items.

You call the LoadThumbnails(String,String,RasterThumbnailBrowserLoadFlags) method passing it the path of the folder for which you want to create thumbnails, a search pattern and a boolean value indicating whether you want the control to block until all thumbnails are created or to return immediately and continue loading the thumbnails in a background thread.

The control will first read the folder and create the items you need. These items will initially have a default "loading" thumbnail set in their RasterImageListItem.Image that you can access and change through the LoadingThumbnail property. Once the item image file is loaded and its thumbnail extracted, the control will replace the item Image property with the correct thumbnail. This gives the user a visual feedback on which items are loaded and which are yet to be loaded. If the control cannot load a certain file (for example, if the file is not a valid image format file), an "error" thumbnail will be used in the item. You can access and change this thumbnail image through the ErrorThumbnail property.

During the loading operation, you can check the IsLoadingThumbnails property to determine if the control is still loading thumbnails in the background. The FinishedLoadingThumbnails event will fire when the loading operation is done. You can subscribe to the LoadThumbnail event to get feedback about the item currently being loaded. You use this event also to update a progress bar on your application and to cancel the load process if needed. The CancelLoadingThumbnails method lets you cancel the load operation at any time.

The ThumbnailSizeFlags property lets you control the quality of the thumbnail images created by the RasterThumbnailBrowser control.

Example
 
Private Class MyForm : Inherits Form
      Public theBrowser As RasterThumbnailBrowser
      Public cancelOperation As Boolean ' Informs us if the user canceled the operation
      Public buttonCancel As Button ' Cancel browse loading
      Public buttonBrowse As Button ' Diplays the browse control
      Public progressBar As ProgressBar ' shows progress of loading browse images
      Private Sub buttonBrowse_Click(ByVal sender As Object, ByVal e As EventArgs)
         ' If we are already loading thumbails, cancel
         ' this operation
         If theBrowser.IsLoadingThumbnails Then
            theBrowser.CancelLoadingThumbnails()
         End If

         ' Clean all the items
         theBrowser.Items.Clear()

         ' Update the application state
         buttonCancel.Enabled = True
         progressBar.Value = 0

         ' And load the new thumbnails
         cancelOperation = False

         Dim folderPath As String = LEAD_VARS.ImagesDir
         theBrowser.LoadThumbnails(folderPath, "*.*", RasterThumbnailBrowserLoadFlags.Block)

         theBrowser.Refresh()

         theBrowser.EnableBroswerWatcher = True
      End Sub

      Private Sub thumbnailBrowser_LoadThumbnail(ByVal sender As Object, ByVal e As RasterThumbnailBrowserLoadThumbnailEventArgs)
         ' If this is the first iteration, update the progress bar minimum and maximum values
         If e.Index = 0 Then
            progressBar.Minimum = 0
            progressBar.Maximum = e.Total - 1
         End If

         ' Update where we are in the loading operation
         progressBar.Value = e.Index

         ' Check if we need to cancel (due to the user clicking the Cancel button)
         If cancelOperation Then
            e.Cancel = True
         End If
      End Sub

      Private Sub thumbnailBrowser_AddFile(ByVal sender As Object, ByVal e As RasterThumbnailBrowserAddFileEventArgs)
         Console.WriteLine("AddFile: {0}    Add: {1}", e.FileName, e.Add)
      End Sub

      Private Sub thumbnailBrowser_FinishedLoadingThumbnails(ByVal sender As Object, ByVal e As EventArgs)
         buttonCancel.Enabled = False
      End Sub

      Private Sub buttonCancel_Click(ByVal sender As Object, ByVal e As EventArgs)
         ' The user has clicked the cancel button
         Me.cancelOperation = True
      End Sub

      ' Create  custom images to use as the "error" and "loading" thumbnails.
      Private Sub CreateErrorThumbnail()
         ' Get the image size
         Dim imageSize As Size = theBrowser.ItemImageSize

         ' No "loading" thumbnail
         theBrowser.LoadingThumbnail = Nothing

         ' For the "error" thumbnail, create a red X image
         Dim palette As RasterColor() = New RasterColor() {}
         Dim image As RasterImage = New RasterImage(RasterMemoryFlags.Conventional, imageSize.Width, imageSize.Height, 24, RasterByteOrder.Bgr, _
                                                    RasterViewPerspective.TopLeft, palette, IntPtr.Zero, 0)

         Dim hdc As IntPtr = RasterImagePainter.CreateLeadDC(image)
         Dim g As Graphics = Graphics.FromHdc(hdc)

         g.FillRectangle(Brushes.Magenta, 0, 0, imageSize.Width, imageSize.Height)

         g.DrawLine(Pens.Red, 0, 0, imageSize.Width, imageSize.Height)
         g.DrawLine(Pens.Red, imageSize.Width, 0, 0, imageSize.Height)

         g.Dispose()
         RasterImagePainter.DeleteLeadDC(hdc)

         ' Make this image transparent
         image.Transparent = True
         image.TransparentColor = RasterColor.FromKnownColor(RasterKnownColor.Magenta)

         theBrowser.ErrorThumbnail = image
      End Sub

      Public Sub Cleanup()
         RemoveHandler buttonBrowse.Click, AddressOf buttonBrowse_Click
         RemoveHandler buttonCancel.Click, AddressOf buttonCancel_Click
         RemoveHandler theBrowser.LoadThumbnail, AddressOf thumbnailBrowser_LoadThumbnail
         RemoveHandler theBrowser.FinishedLoadingThumbnails, AddressOf thumbnailBrowser_FinishedLoadingThumbnails
      End Sub

      Public Sub New()
         Size = New Size(300, 200)

         ' create the browser
         theBrowser = New RasterThumbnailBrowser()
         theBrowser.Codecs = New RasterCodecs()
         theBrowser.Dock = DockStyle.Fill
         theBrowser.ItemSpacingSize = New Size(10, 10)
         theBrowser.ThumbnailSizeFlags = RasterSizeFlags.Bicubic
         AddHandler theBrowser.LoadThumbnail, AddressOf thumbnailBrowser_LoadThumbnail
         AddHandler theBrowser.FinishedLoadingThumbnails, AddressOf thumbnailBrowser_FinishedLoadingThumbnails
         AddHandler theBrowser.AddFile, AddressOf thumbnailBrowser_AddFile

         ' Create a thumbnail image to be displayed on error
         CreateErrorThumbnail()

         ' add a panel
         Dim panel As Panel = New Panel()
         panel.Dock = DockStyle.Left
         panel.Width = 100
         Controls.Add(panel)
         panel.BringToFront()

         ' add a "browse" button
         buttonBrowse = New Button()
         buttonBrowse.Text = "Browse"
         buttonBrowse.Dock = DockStyle.Top
         panel.Controls.Add(buttonBrowse)
         AddHandler buttonBrowse.Click, AddressOf buttonBrowse_Click

         ' add a "cancel" button
         buttonCancel = New Button()
         buttonCancel.Text = "Cancel"
         buttonBrowse.Dock = DockStyle.Bottom
         panel.Controls.Add(buttonCancel)
         AddHandler buttonCancel.Click, AddressOf buttonCancel_Click

         ' add a progress bar
         progressBar = New ProgressBar()
         progressBar.Dock = DockStyle.Bottom
         Controls.Add(progressBar)

         Controls.Add(theBrowser)
         theBrowser.BringToFront()
      End Sub

      Private Sub InitializeComponent()
         Me.SuspendLayout()
         ' 
         ' MyForm
         ' 
         Me.ClientSize = New System.Drawing.Size(315, 273)
         Me.Name = "MyForm"
         Me.ResumeLayout(False)

      End Sub
   End Class
   Public Sub RasterThumbnailBrowser_LoadThumbnails2()
      Dim form As MyForm = New MyForm()
      form.ShowDialog()
      form.Cleanup()
   End Sub

Public NotInheritable Class LEAD_VARS
   Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"
End Class
class MyForm : Form
   {
      public RasterThumbnailBrowser theBrowser;
      public bool cancelOperation;        // Informs us if the user canceled the operation
      public Button buttonCancel;         // Cancel browse loading
      public Button buttonBrowse;         // Diplays the browse control
      public ProgressBar progressBar;     // shows progress of loading browse images
      void buttonBrowse_Click(object sender, EventArgs e)
      {
         // If we are already loading thumbails, cancel
         // this operation
         if(theBrowser.IsLoadingThumbnails)
            theBrowser.CancelLoadingThumbnails();

         // Clean all the items
         theBrowser.Items.Clear();

         // Update the application state
         buttonCancel.Enabled = true;
         progressBar.Value = 0;

         // And load the new thumbnails
         cancelOperation = false;

         string folderPath = LEAD_VARS.ImagesDir;
         theBrowser.LoadThumbnails(folderPath, "*.*", RasterThumbnailBrowserLoadFlags.Block);

         theBrowser.Refresh();

         theBrowser.EnableBroswerWatcher = true;
      }

      private void thumbnailBrowser_LoadThumbnail(object sender, RasterThumbnailBrowserLoadThumbnailEventArgs e)
      {
         // If this is the first iteration, update the progress bar minimum and maximum values
         if(e.Index == 0)
         {
            progressBar.Minimum = 0;
            progressBar.Maximum = e.Total - 1;
         }

         // Update where we are in the loading operation
         progressBar.Value = e.Index;

         // Check if we need to cancel (due to the user clicking the Cancel button)
         if(cancelOperation)
            e.Cancel = true;
      }

      private void thumbnailBrowser_AddFile(object sender, RasterThumbnailBrowserAddFileEventArgs e)
      {
         Console.WriteLine("AddFile: {0}    Add: {1}", e.FileName, e.Add);
      }

      private void thumbnailBrowser_FinishedLoadingThumbnails(object sender, EventArgs e)
      {
         buttonCancel.Enabled = false;
      }

      private void buttonCancel_Click(object sender, EventArgs e)
      {
         // The user has clicked the cancel button
         this.cancelOperation = true;
      }

      // Create  custom images to use as the "error" and "loading" thumbnails.
      void CreateErrorThumbnail()
      {
         // Get the image size
         Size imageSize = theBrowser.ItemImageSize;

         // No "loading" thumbnail
         theBrowser.LoadingThumbnail = null;

         // For the "error" thumbnail, create a red X image
         RasterColor[] palette = new RasterColor[0];
         RasterImage image = new RasterImage(
            RasterMemoryFlags.Conventional,
            imageSize.Width,
            imageSize.Height,
            24,
            RasterByteOrder.Bgr,
            RasterViewPerspective.TopLeft,
         palette,
         IntPtr.Zero,
         0
         );

         IntPtr hdc = RasterImagePainter.CreateLeadDC(image);
         Graphics g = Graphics.FromHdc(hdc);

         g.FillRectangle(Brushes.Magenta, 0, 0, imageSize.Width, imageSize.Height);

         g.DrawLine(Pens.Red, 0, 0, imageSize.Width, imageSize.Height);
         g.DrawLine(Pens.Red, imageSize.Width, 0, 0, imageSize.Height);

         g.Dispose();
         RasterImagePainter.DeleteLeadDC(hdc);

         // Make this image transparent
         image.Transparent = true;
         image.TransparentColor = RasterColor.FromKnownColor(RasterKnownColor.Magenta);

         theBrowser.ErrorThumbnail = image;
      }

      public void Cleanup()
      {
         buttonBrowse.Click -= new EventHandler(buttonBrowse_Click);
         buttonCancel.Click -= new EventHandler(buttonCancel_Click);
         theBrowser.LoadThumbnail -= new EventHandler<RasterThumbnailBrowserLoadThumbnailEventArgs>(thumbnailBrowser_LoadThumbnail);
         theBrowser.FinishedLoadingThumbnails -= new EventHandler(thumbnailBrowser_FinishedLoadingThumbnails);
      }

      public MyForm()
      {
         Size = new Size(300, 200);

         // create the browser
         theBrowser = new RasterThumbnailBrowser();
         theBrowser.Codecs = new RasterCodecs();
         theBrowser.Dock = DockStyle.Fill;
         theBrowser.ItemSpacingSize = new Size(10, 10);
         theBrowser.ThumbnailSizeFlags = RasterSizeFlags.Bicubic;
         theBrowser.LoadThumbnail += new EventHandler<RasterThumbnailBrowserLoadThumbnailEventArgs>(thumbnailBrowser_LoadThumbnail);
         theBrowser.FinishedLoadingThumbnails += new EventHandler(thumbnailBrowser_FinishedLoadingThumbnails);
         theBrowser.AddFile += new EventHandler<RasterThumbnailBrowserAddFileEventArgs>(thumbnailBrowser_AddFile);

         // Create a thumbnail image to be displayed on error
         CreateErrorThumbnail();

         // add a panel
         Panel panel = new Panel();
         panel.Dock = DockStyle.Left;
         panel.Width = 100;
         Controls.Add(panel);
         panel.BringToFront();

         // add a "browse" button
         buttonBrowse = new Button();
         buttonBrowse.Text = "Browse";
         buttonBrowse.Dock = DockStyle.Top;
         panel.Controls.Add(buttonBrowse);
         buttonBrowse.Click += new EventHandler(buttonBrowse_Click);

         // add a "cancel" button
         buttonCancel = new Button();
         buttonCancel.Text = "Cancel";
         buttonBrowse.Dock = DockStyle.Bottom;
         panel.Controls.Add(buttonCancel);
         buttonCancel.Click += new EventHandler(buttonCancel_Click);

         // add a progress bar
         progressBar = new ProgressBar();
         progressBar.Dock = DockStyle.Bottom;
         Controls.Add(progressBar);

         Controls.Add(theBrowser);
         theBrowser.BringToFront();
      }

      private void InitializeComponent()
      {
         this.SuspendLayout();
         // 
         // MyForm
         // 
         this.ClientSize = new System.Drawing.Size(315, 273);
         this.Name = "MyForm";
         this.ResumeLayout(false);

      }
   } 
   public void RasterThumbnailBrowser_RasterThumbnailBrowser()
   {
      MyForm form = new MyForm();
      form.ShowDialog();
      form.Cleanup();
   }

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

RasterThumbnailBrowser Members
Leadtools.WinForms Namespace
Thumbnail Browser

 

 


Products | Support | Contact Us | Copyright Notices

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