Link to home
Start Free TrialLog in
Avatar of saimatkong
saimatkong

asked on

How to create thumbnail

How to create thumbnail ?

and i need to create the thumbnail for a particular filename for every folder under a particular folder.

thanks
Avatar of AGBrown
AGBrown
Flag of United Kingdom of Great Britain and Northern Ireland image

You can do this with the System.IO.DirectoryInfo, System.IO.FileInfo and System.Drawing objects and namespaces. The Bitmap class in System.Drawing has a method called GetThumbnailImage which does what it says on the tin.

Roughly, it would go something like:
<psuedo>
Main()
{
-Get parentDirectory as DirectoryInfo
RecurseDirectories(parentDirectory)
}

RecurseDirectories(DirectoryInfo parentDirectory)
{
-for each directory in parentDirectory.GetDirectories()
    -RecurseDirectories(directory)

-for each file in parentDirectory.GetFiles("targetFileName")
}

CreateThumbnail(string FileInfoFullName)
{
using (System.Drawing.Bitmap bmp = new Bitmap(FileInfoFullName))
{
-manipulate bmp using bmp.GetThumbnailImage() and save it to a file
}
}
</psuedo>

Good references for doing the thumbnail creation:
http://www.codeproject.com/aspnet/thumbtools.asp

Andy
Avatar of saimatkong
saimatkong

ASKER

hmm that's asp.net what i mean is C# win app.

and can it be loading all the date from different xml in diff sub folder under one folder
and load all the title into a list and when a list is being clicked, the image and the details is being loaded.


thanks again.
saimatkong,

Don't worry, the code is not asp.net specific, you can transfer it to windows and get it to work in c#. The code I have shown above just mapped out the psuedo-code/logic, you will need to do the specifics for your application. It shows three things:
1) A main function that starts the process of recursively going through the subdirectories
2) A recursive function that will go into each directory within a directory. If there are no directories, or once it returns from the subdirectories, then you can process the files.
3) A CreateThumbnail function that uses the filename along with the System.Drawing.Bitmap objects to generate the thumbnail.

You can use the System.IO objects to fill your list. Then, when you click a file in that list you can use the System.Drawing.Bitmap object to get or create a thumbnail. To load all the files in all sub folders into a list you will need a recursive function, much like I've shown.

Andy
thanks for ur help.
anyway it's not really the stuff that i'm looking for,
but really appriciate ur effort.
Avatar of Bob Learned
What kind of thumbnails are you looking for?  Are you looking for the same type of thumbnails that Windows Explorer displays?

Bob
ya something like tat.

thanks
saimatkong,

No problem. I'm not sure I fully understand your question if this isn't what you are looking for. As far as I know, there is no other way to generate thumbnails other than using the GDI+ objects in System.Drawing, either using the GetThumbnailImage or scaling an image yourself. I also don't know of any other way of loading a folder/file list into a list without using a recursive function with the System.IO objects. I'll watch with interest to see if anyone has a different solution.

Andy
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is an example form (2003):

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

  /// <summary>
  /// Summary description for formShellThumbnail.
  /// </summary>
public class formShellThumbnail : System.Windows.Forms.Form
{
  /// <summary>
  /// Required designer variable.
  /// </summary>
  private System.ComponentModel.Container components = null;

  public formShellThumbnail()
  {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent();

    //
    // TODO: Add any constructor code after InitializeComponent call
    //
  }

  /// <summary>
  /// Clean up any resources being used.
  /// </summary>
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if(components != null)
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }

  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  [STAThread]
  static void Main()
  {
    Application.Run(new formShellThumbnail());
  }

  #region Windows Form Designer generated code
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {
    this.pictureBox1 = new System.Windows.Forms.PictureBox();
    this.SuspendLayout();
    //
    // pictureBox1
    //
    this.pictureBox1.Location = new System.Drawing.Point(44, 28);
    this.pictureBox1.Name = "pictureBox1";
    this.pictureBox1.Size = new System.Drawing.Size(112, 88);
    this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
    this.pictureBox1.TabIndex = 0;
    this.pictureBox1.TabStop = false;
    //
    // formShellThumbnail
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(208, 162);
    this.Controls.Add(this.pictureBox1);
    this.Name = "formShellThumbnail";
    this.Text = "Shell Thumbnails";
    this.Load += new System.EventHandler(this.formShellThumbnail_Load);
    this.ResumeLayout(false);

  }
  #endregion

  private System.Windows.Forms.PictureBox pictureBox1;

  private ShellThumbnail _shell = new ShellThumbnail();

  private void formShellThumbnail_Load(object sender, System.EventArgs e)
  {
    this.pictureBox1.Image = _shell.GetThumbnail(@"C:\WINDOWS\Web\Wallpaper\Autumn.jpg");
 
  }
}

Bob
thansk anyway =)
wow so long the code.
how to execute or call it btw ?
thanks
This code doesnt seem to work for PDF files.  Any ideas why?
I am using ShellThumbnail in windows7 and it is returning folder thumbnail with black background, any help ???