Advertisement
Guest User

Untitled

a guest
Mar 1st, 2013
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.37 KB | None | 0 0
  1. type
  2.   TIconDirEntry = packed record
  3.     bWidth: Byte;           // Width, in pixels, of the image
  4.     bHeight: Byte;          // Height, in pixels, of the image
  5.     bColorCount: Byte;      // Number of colors in image (0 if >=8bpp)
  6.     bReserved: Byte;        // Reserved ( must be 0)
  7.     wPlanes: Word;          // Color Planes
  8.     wBitCount: Word;        // Bits per pixel
  9.     dwBytesInRes: DWORD;    // How many bytes in this resource?
  10.     dwImageOffset: DWORD;   // Where in the file is this image?
  11.   end;
  12.  
  13.   TIconDir = packed record
  14.     idReserved: Word;       // Reserved (must be 0)
  15.     idType: Word;           // Resource Type (1 for icons)
  16.     idCount: Word;          // How many images?
  17.     idEntries: array [0..255] of TIconDirEntry;
  18.   end;
  19.   PIconDir = ^TIconDir;
  20.  
  21. procedure TForm14.Button1Click(Sender: TObject);
  22. var
  23.   Icon: TIcon;
  24.   FileHeader: PIconDir;
  25.   IconHeader: PIconDir;
  26.   FileStream: TMemoryStream;
  27.   IconStream: TMemoryStream;
  28. begin
  29.   FileStream := TMemoryStream.Create;
  30.   try
  31.     FileStream.LoadFromFile('d:\Icon.ico');
  32.     // point to the icon file header
  33.     FileHeader := FileStream.Memory;
  34.  
  35.     IconStream := TMemoryStream.Create;
  36.     try
  37.       // set the size of the extracted icon stream
  38.       IconStream.Size := SizeOf(Word) * 3 + SizeOf(TIconDirEntry);
  39.       // set the position of the extracted icon stream behind header
  40.       IconStream.Position := SizeOf(Word) * 3 + SizeOf(TIconDirEntry);
  41.       // fill the header for the extracted icon
  42.       IconHeader := IconStream.Memory;
  43.       IconHeader.idCount := 1;
  44.       IconHeader.idType := FileHeader.idType;
  45.       IconHeader.idReserved := FileHeader.idReserved;
  46.       IconHeader.idEntries[0] := FileHeader.idEntries[0];
  47.       IconHeader.idEntries[0].dwImageOffset := IconStream.Size;
  48.       // set the position of the icon file stream to the extracted icon offset
  49.       FileStream.Position := FileHeader.idEntries[0].dwImageOffset;
  50.       // copy the extracted icon data
  51.       IconStream.CopyFrom(FileStream, FileHeader.idEntries[0].dwBytesInRes);
  52.       // reset the extracted icon stream position
  53.       IconStream.Position := 0;
  54.  
  55.       Icon := TIcon.Create;
  56.       try
  57.         Icon.LoadFromStream(IconStream);
  58.         ImageList1.AddIcon(Icon);
  59.       finally
  60.         Icon.Free;
  61.       end;
  62.  
  63.     finally
  64.       IconStream.Free;
  65.     end;
  66.  
  67.   finally
  68.     FileStream.Free;
  69.   end;
  70. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement