Page 1 of 1

[i18n] Improved TCustomResFlagImageList

PostPosted: August 28th, 2012, 2:49 pm
by arhangelsoft
Greetings, Kambiz!

i18n Library automatically include flag's images into output binary like bitmaps, but this format is very hudge.
i'm converted images into JPEG(economy 24 kb) and modify the i18nCtrls.pas see TCustomResFlagImageList class.
Below i've posted a modificated code:
Code: Select all
{ TCustomResFlagImageList }

function EnumBitmapsCallback(hModule: hModule; lpszType, lpszName: PChar;
  lParam: Integer): Integer; stdcall;
var
  IL: TCustomResFlagImageList absolute lParam;
  ISO_3166_2: string;
begin
  if (lpszType = 'JPEG') and (ULONG_PTR(lpszName) shr 16 <> 0) and
    (AnsiStrPos(lpszName, PChar(IL.GetResourcePrefix)) = lpszName) then
  begin
    SetString(ISO_3166_2, lpszName + Length(IL.GetResourcePrefix),
      StrLen(lpszName) - Cardinal(Length(IL.GetResourcePrefix)));
    IL.AddFlagFromResource(ISO_3166_2, lpszName, hModule);
  end;
  Result := 1;
end;

function TCustomResFlagImageList.AddFlagFromResource(const ISO_3166_2,
  ResName: string; hModule: hModule): Integer;
var
  JPEG: TJPEGImage;
  RS: TResourceStream;
  BMP: TBitmap;
begin
  if hModule = 0 then
    hModule := HInstance;

  JPEG := TJPEGImage.Create;
  BMP := TBitmap.Create;
  RS := TResourceStream.Create(HInstance, PChar(ResName), PChar('JPEG'));
  try
    JPEG.LoadFromStream(RS);
    BMP.Assign(JPEG);
    Result := AddFlag(UpperCase(ISO_3166_2), BMP.Handle);
  finally
    FreeAndNil(JPEG);
    FreeAndNil(BMP);
    FreeAndNil(RS);
  end;
end;

procedure TCustomResFlagImageList.PrepareFlags;
begin
  with GetFlagSize do
    SetSize(cx, cy);
  EnumResourceNames(HInstance, PChar('JPEG'), @EnumBitmapsCallback,
    Integer(Self));
end;

Mod/ images see in attachments.

Also I'm think that the flags images must be stored in language files as a streams, 1 language = 1 flag, not in EXE. But I'm don't understood how it's made.. What you think?

Re: [i18n] Improved TCustomResFlagImageList

PostPosted: August 29th, 2012, 1:24 pm
by Kambiz
I used bitmap because JPEG doesn't support transparency.
The Swiss flag is square and the Nepal's flag is non-quadrilateral. Transparency is required for these two flags.
Using a few kilo bytes or even mega bytes more is nothing compare to the nowadays computers' Giga memories. But the quality is a matter indeed.

Re: [i18n] Improved TCustomResFlagImageList

PostPosted: August 29th, 2012, 4:33 pm
by arhangelsoft
What about PNG's using? Delphi 2009+ also supports it! This format is more useful than BMP and JPEG.

Re: [i18n] Improved TCustomResFlagImageList

PostPosted: August 29th, 2012, 6:14 pm
by Kambiz
Regardless of the image format you use as the resource, Windows uses bitmaps for imagelists.