Link to home
Start Free TrialLog in
Avatar of Alpha_AI
Alpha_AI

asked on

Getting the images from all file extensions. such as pdf, .doc and so on

How do I get the icons that have assigned to all the file types
such as .pdf, .doc, .mpg
and assign those to TImage

I am after the small images and big images.

I know how to get the small images for executable.

       TheIcon := FOrm1.GetSmallIconFromFile(Trim(Data^.Type1),0);
        if TheIcon <> nil then
          begin
          TImage(((Form1.FindComponent('Edit'+Data^.Meaning2) as TEdit).Parent).Controls[i]).Picture.Assign(TheIcon);      // assign the icon to the TImage that exists on the panel
          end;

function TForm1.GetSmallIconFromFile(FName: String; idx: Word): TIcon;
var
  LargeIcon : Hicon;
  SmallIcon : Hicon;
  Icon : TIcon;
begin
Icon := TIcon.Create();
   ExtractIconEx(PChar(FName), idx, LargeIcon, SmallIcon, 1);
   if SmallIcon <= 1 then
     begin
     Icon.Handle := SmallIcon;
     Result := Icon;
     end
   else begin
     Icon.Handle := LargeIcon;
     Result := Icon;
  end;

ive tried to pull a image from many of the other file extension other than .exe and i cant rip it out.

Whats the solution?
Avatar of Tomas Helgi Johannsson
Tomas Helgi Johannsson
Flag of Iceland image

Avatar of Alpha_AI
Alpha_AI

ASKER

Thanks TomasHelg

This one http://www.swissdelphicenter.ch/torry/showcode.php?id=421 works and output the images into a listview.
This one http://www.swissdelphicenter.ch/torry/showcode.php?id=1108 can output the image to a TImage object

however the first one http://www.swissdelphicenter.ch/torry/showcode.php?id=421 for some reason has a better image quality (brighter and more crisp)

Here is the code for that one, I have some problem trying to output the image to a TImage object. I have modified it so it can only find one find to put into the Timage object
You will have to change the file to match your computer.

uses
  ShellApi;

procedure LV_InsertFiles(strPath: string; ListView: TListView; ImageList: TImageList);
var
  i: Integer;
  Icon: TIcon;
  SearchRec: TSearchRec;
  ListItem: TListItem;
  FileInfo: SHFILEINFO;
begin
  // Create a temporary TIcon
  Icon := TIcon.Create;
  ListView.Items.BeginUpdate;
  try
    // search for the first file
    i := FindFirst(strPath + 'twain.dll', faAnyFile, SearchRec);
    while i = 0 do
    begin
      with ListView do
      begin
        // On directories and volumes
        if ((SearchRec.Attr and FaDirectory <> FaDirectory) and
          (SearchRec.Attr and FaVolumeId <> FaVolumeID)) then
        begin
          ListItem := ListView.Items.Add;
          //Get The DisplayName
          SHGetFileInfo(PChar(strPath + SearchRec.Name), 0, FileInfo,
            SizeOf(FileInfo), SHGFI_DISPLAYNAME);
          Listitem.Caption := FileInfo.szDisplayName;
          // Get The TypeName
          SHGetFileInfo(PChar(strPath + SearchRec.Name), 0, FileInfo,
            SizeOf(FileInfo), SHGFI_TYPENAME);
          ListItem.SubItems.Add(FileInfo.szTypeName);
          //Get The Icon That Represents The File
          SHGetFileInfo(PChar(strPath + SearchRec.Name), 0, FileInfo,
            SizeOf(FileInfo), SHGFI_ICON or SHGFI_SMALLICON);
          icon.Handle := FileInfo.hIcon;
          ListItem.ImageIndex := ImageList.AddIcon(Icon);
 
// this is where i add in my little code
          Image1.Picture.Assign(Icon);

          // Destroy the Icon
          DestroyIcon(FileInfo.hIcon);
        end;
      end;
      i := FindNext(SearchRec);
    end;
  finally
    Icon.Free;
    ListView.Items.EndUpdate;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  // Assign a Imagelist to the ListView
  ListView1.SmallImages := ImageList1;
  // Show Listview in Report Style and add 2 Columns
  ListView1.ViewStyle := vsReport;
  ListView1.Columns.Add;
  ListView1.Columns.Add;
  LV_InsertFiles('C:\Windows\', ListView1, ImageList1);
end;

I dont know why it doesnt work. Any clues?

Ben
On this line

    i := FindFirst(strPath + 'twain.dll', faAnyFile, SearchRec);

I modified it so it only bring up the twain.dll file along with its icon.

For some reason i cannot output the image out to a TImage object

Ben
ASKER CERTIFIED SOLUTION
Avatar of Tomas Helgi Johannsson
Tomas Helgi Johannsson
Flag of Iceland 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
I found a really simple way somewhere online

                  begin
                   InfoSize := SizeOf(TSHFileInfo);
                   FillChar(Info, InfoSize, 0);
                   SHGetFileInfo(PChar(Trim(Data^.Type1)), 0, Info, InfoSize, SHGFI_ICON or SHGFI_SMALLICON);
                  TImage(((Form1.FindComponent('Edit'+Data^.Meaning2) as TEdit).Parent).Controls[i]).Picture.icon.Handle := Info.hIcon;
                  end;

with this code its so easy to do.

Ben
well the code i posted is not the correct code, its my modified code for my program :-)

Ben
Thanks again.

 Ben