Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type
- TIconDirEntry = packed record
- bWidth: Byte; // Width, in pixels, of the image
- bHeight: Byte; // Height, in pixels, of the image
- bColorCount: Byte; // Number of colors in image (0 if >=8bpp)
- bReserved: Byte; // Reserved ( must be 0)
- wPlanes: Word; // Color Planes
- wBitCount: Word; // Bits per pixel
- dwBytesInRes: DWORD; // How many bytes in this resource?
- dwImageOffset: DWORD; // Where in the file is this image?
- end;
- TIconDir = packed record
- idReserved: Word; // Reserved (must be 0)
- idType: Word; // Resource Type (1 for icons)
- idCount: Word; // How many images?
- idEntries: array [0..255] of TIconDirEntry;
- end;
- PIconDir = ^TIconDir;
- procedure TForm14.Button1Click(Sender: TObject);
- var
- Icon: TIcon;
- FileHeader: PIconDir;
- IconHeader: PIconDir;
- FileStream: TMemoryStream;
- IconStream: TMemoryStream;
- begin
- FileStream := TMemoryStream.Create;
- try
- FileStream.LoadFromFile('d:\Icon.ico');
- // point to the icon file header
- FileHeader := FileStream.Memory;
- IconStream := TMemoryStream.Create;
- try
- // set the size of the extracted icon stream
- IconStream.Size := SizeOf(Word) * 3 + SizeOf(TIconDirEntry);
- // set the position of the extracted icon stream behind header
- IconStream.Position := SizeOf(Word) * 3 + SizeOf(TIconDirEntry);
- // fill the header for the extracted icon
- IconHeader := IconStream.Memory;
- IconHeader.idCount := 1;
- IconHeader.idType := FileHeader.idType;
- IconHeader.idReserved := FileHeader.idReserved;
- IconHeader.idEntries[0] := FileHeader.idEntries[0];
- IconHeader.idEntries[0].dwImageOffset := IconStream.Size;
- // set the position of the icon file stream to the extracted icon offset
- FileStream.Position := FileHeader.idEntries[0].dwImageOffset;
- // copy the extracted icon data
- IconStream.CopyFrom(FileStream, FileHeader.idEntries[0].dwBytesInRes);
- // reset the extracted icon stream position
- IconStream.Position := 0;
- Icon := TIcon.Create;
- try
- Icon.LoadFromStream(IconStream);
- ImageList1.AddIcon(Icon);
- finally
- Icon.Free;
- end;
- finally
- IconStream.Free;
- end;
- finally
- FileStream.Free;
- end;
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement