I was against adding picture processing code to the library but it came to my mind that I could use Free Image Library for this purpose. The TagsLib.dll size increased only by 50 KB.
Added 2 new functions to simplify managing cover arts:
function
TagsLibrary_GetCoverArtBitmapHandle(Tags: TTags; TagType: TTagType; Index: Integer; var CoverArt: TCoverArtData; var BitmapHandle: THandle; Width: Integer; Height: Integer; KeepAspectRatio: LongBool; Resampler: Integer; Handle: THandle): LongBool;
Get the cover art's HBITMAP handle specified by 'Index' using Free Image Library with optional resize. All picture formats supported by Free Image Library are supported.
The cover art details are returned in 'CoverArt'.
The HBITMAP handle is returned in 'BitmapHandle'.
'Width' and 'Height' are opional, use '0' to retrieve the picture in it's original size.
Set 'KeepAspectRatio' to True to resize the picture based on max. 'Width' or 'Height'.
'Resampler' is used to specify a Free Image Library resampler to be used for resizing the picture, Lanczos3 is recommended for sharpest result.
'Handle' specifies a Handle to be used for GetDC() when creating the DIB (eg. use a windows' HWND handle here).
Return value is 'True' on success.
This function is only available on Windows, 'FreeImage.dll' must be available on the .dll load path (eg. beside the .exe). On Win64 'vcomp120.dll' is also needed. 'FreeImage.dll' is loaded dynamicaly on first call to this function, if you don't call this function 'FreeImage.dll' will not be loaded at all, so it's not needed then.
function
TagsLibrary_AddCoverArtFromFileAutomatic(Tags: TTags; TagType: TTagType; FileName: PChar; Name: PChar; Description: PChar; CoverType: Integer; Handle: THandle): Integer;
Add a cover art specified by 'FileName' using Free Image Library. Picture formats supported: JPEG, PNG, GIF, BMP and TIFF.
'Name' is only used with APEv2, should be 'Cover Art (Front)'.
'Description' is self explanatory.
'CoverType': ID3v2 cover type, use '3' as front cover.
'Handle' specifies a Handle to be used for GetDC() when creating the DIB (eg. use a windows' HWND handle here).
Return value is the new cover art index on success, - 1 on fail.
This function is only available on Windows, 'FreeImage.dll' must be available on the .dll load path (eg. beside the .exe). On Win64 'vcomp120.dll' is also needed. 'FreeImage.dll' is loaded dynamicaly on first call to this function, if you don't call this function 'FreeImage.dll' will not be loaded at all, so it's not needed then.
Not sure if the VB6 and C++ header is right, please let me know if there are any problems!
Download (beta):
Tags Library.zipIn Delphi now it's this simple to load a cover art:
procedure TForm1.DisplayCoverArt(Index: Integer);
var
CoverArtData: TCoverArtData;
BitmapHandle: THandle;
begin
if TagsLibrary_GetCoverArtBitmapHandle(Tags, ttAutomatic, Index, CoverArtData, BitmapHandle, Image1.Width, Image1.Height, True, FILTER_LANCZOS3, Handle) then begin
Image1.Picture.Bitmap.Handle := BitmapHandle;
end;
end;
...and to add a new:
procedure TForm1.Button2Click(Sender: TObject);
begin
if OpenDialog1.Execute then begin
if TagsLibrary_AddCoverArtFromFileAutomatic(Tags, ttAutomatic, PChar(OpenDialog1.FileName), 'Cover Art (Front)' {This field is only used for APEv2 and should be always 'Cover Art (Front)'}, 'My description', 3, Handle) < 0 then begin
MessageDlg('Error while adding cover art.', mtError, [mbCancel], 0);
end;
ListCoverArts;
end;
end;