[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

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?
0
Alpha_AI
Asked:
Alpha_AI
  • 5
  • 2
1 Solution
 
Tomas Helgi JohannssonCommented:
0
 
Alpha_AIAuthor Commented:
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
0
 
Alpha_AIAuthor Commented:
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Tomas Helgi JohannssonCommented:
As .ICO files are not able to load directly into TImage then
use this technique : http://www.swissdelphicenter.ch/torry/showcode.php?id=22
by applying it to the Image1.Picture.Bitmap object.

Regards,
   Tomas Helgi
0
 
Alpha_AIAuthor Commented:
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
0
 
Alpha_AIAuthor Commented:
well the code i posted is not the correct code, its my modified code for my program :-)

Ben
0
 
Alpha_AIAuthor Commented:
Thanks again.

 Ben
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now