Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2008-04

Read image from resources with TLazIntfImage

8 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Valdas Jankūnas

Hello,

how i can read PNG image from Lazarus resources using TLazIntfImage?

I tried TPortableNetworkGraphic to read transparent PNG from resource or file, but in transparent areas of saved PNG image i get random garbage. Code:

     img:=TPortableNetworkGraphic.Create;
     img.LoadFromFile(FAILAS_OUT);
     img.SaveToFile(FAILAS_IN);
     img.Free;

This code is correct? If so i report bug to bugtracker.

If i doing same test (with file) using TLazIntfImage with TFPCustomImageReader and TFPCustomImageWriter, then i get correct results, but i not know how using TLazIntfImage load image from resource.

Valdas Jank?nas

Reply by email

Héctor Fiandor Rosario
to Valdas Jankūnas

Dear sirs:

I have used for years with Delphi5 the following function to get de series number of the harddisk (c)

function GetVolumeID(DriveChar: Char): String;
//Para obtener el # de serie de un disco
var
   MaxFileNameLength, VolFlags, SerNum: DWord;
begin
   if GetVolumeInformation(PChar(DriveChar + ':\'), nil, 0,
      @SerNum, MaxFileNameLength, VolFlags, nil, 0)
   then
   begin
       Result := IntToStr(SerNum);
   end
   else
       Result := '';
end;

and now, with FreePascal, I have to use something as this for the same proposal.

Somebody can help me with this task?

Thanks in advance,

yours truly,

Ing. H?ctor F. Fiandor

Reply by email

Felipe Monteiro de Carvalho
to Valdas Jankūnas: how i can read PNG image from Lazarus resources using TLazIntfImage? I tried…

I don't know how to answer your original doubt, but I load bitmaps with color transparency from .rc resources which are linked inside the executable on win32 or from external files in any other operating system with the following code. Maybe it will help you:

{*******************************************************************
* TGlass.LoadBitmap ()
*
* DESCRIPTION: Loads bitmap from an internal resource to a bitmap object *
* PARAMETERS:
*
* RETURNS:
*
*******************************************************************} class function TGlass.LoadBitmap(bmp: TBitmap; NomeDoRecurso: Integer): Boolean; var

  buffer: THandle;
  memstream: TMemoryStream;
  FileName: string;

begin

  Result := False;
{$IFDEF Win32}
  buffer := Windows.LoadBitmap(hInstance, MAKEINTRESOURCE(NomeDoRecurso));
  if (buffer = 0) then exit;
  bmp.Handle := buffer;
  memstream := TMemoryStream.create;
  try
    try
      bmp.savetostream(memstream);
      memstream.position := 0;
      bmp.loadfromstream(memstream);
    finally
      memstream.free;
    end;
  except
    Exit;
  end;
{$ENDIF}
{$IFDEF Unix}
  case NomeDoRecurso of
   IDB_TOPLEFT: FileName := 'topleft.bmp';
   IDB_TOPRIGHT: FileName := 'topright.bmp';
   IDB_BOTTOMLEFT: FileName := 'bottomleft.bmp';
   IDB_BOTTOMRIGHT: FileName := 'bottomright.bmp';
   IDB_TOP: FileName := 'top.bmp';
   IDB_LEFT: FileName := 'left.bmp';
   IDB_BOTTOM: FileName := 'bottom.bmp';
   IDB_RIGHT: FileName := 'right.bmp';
   IDB_CECAE: FileName := 'cecae.bmp';
   IDB_FEUSP: FileName := 'feusp.bmp';
   IDB_VMG: FileName := 'vmg.bmp';
   IDB_LUPA: FileName := 'lupa.bmp';
   IDB_USPLEGAL: FileName := 'usplegal.bmp';
  else
    WriteLn('Wrong image name');
    Exit;
  end;
  Write('[TGlass.LoadBitmap] Loading ' + vConfigurations.MyDirectory +
FileName);
  try
    bmp.LoadFromFile(vConfigurations.MyDirectory + FileName);
  except
    WriteLn(ErrorLoading);
    Exit;
  end;
  WriteLn('');
{$ENDIF}
  Result := True;
end;

{*******************************************************************
* TGlass.LoadImages ()
*
* DESCRIPTION: Load bitmaps required by the Magnifying Glass
* This function loads and generates the rectangular glass *
* PARAMETERS:
*
* RETURNS:
*
*******************************************************************} function TGlass.LoadImages: Boolean;
begin

  Result := True;
  if (not LoadBitmap(bmpTopLeft, IDB_TOPLEFT)) then Result := False;
  bmpTopLeft.Transparent := True;
  bmpTopLeft.TransparentColor := clFuchsia;
  if (not LoadBitmap(bmpTopRight, IDB_TOPRIGHT)) then Result := False;
  bmpTopRight.Transparent := True;
  bmpTopRight.TransparentColor := clFuchsia;
  if (not LoadBitmap(bmpBottomLeft, IDB_BOTTOMLEFT)) then Result := False;
  bmpBottomLeft.Transparent := True;
  bmpBottomLeft.TransparentColor := clFuchsia;
  if (not LoadBitmap(bmpBottomRight, IDB_BOTTOMRIGHT)) then Result := False;
  bmpBottomRight.Transparent := True;
  bmpBottomRight.TransparentColor := clFuchsia;
  if (not LoadBitmap(bmpTop, IDB_TOP)) then Result := False;
  bmpTop.Transparent := True;
  bmpTop.TransparentColor := clFuchsia;
  if (not LoadBitmap(bmpLeft, IDB_LEFT)) then Result := False;
  bmpLeft.Transparent := True;
  bmpLeft.TransparentColor := clFuchsia;
  if (not LoadBitmap(bmpBottom, IDB_BOTTOM)) then Result := False;
  bmpBottom.Transparent := True;
  bmpBottom.TransparentColor := clFuchsia;
  if (not LoadBitmap(bmpRight, IDB_RIGHT)) then Result := False;
  bmpRight.Transparent := True;
  bmpRight.TransparentColor := clFuchsia;
  // Check the sizes
  if ((bmpTopLeft.Width  bmpTopRight.Width) or
        (bmpTopLeft.Height  bmpTopRight.Height) or
        (bmpBottomLeft.Width  bmpBottomRight.Width) or
        (bmpBottomLeft.Height  bmpBottomRight.Height)) // Sizes must match
        then Result := False;
  // Check the sizes
  if ((bmpLeft.Width  bmpRight.Width) or
      (bmpLeft.Height  bmpRight.Height) or
      (bmpTop.Width  bmpBottom.Width) or
      (bmpTop.Height  bmpBottom.Height)) // Sizes must match
      then Result := False;
end;

Reply by email

Valdas Jankūnas
to Felipe Monteiro de Carvalho

Felipe Monteiro de Carvalho ra??:

Show quoted text 5 lines
I don't know how to answer your original doubt, but I load bitmaps with color transparency from .rc resources which are linked inside the executable on win32 or from external files in any other operating system with the following code. Maybe it will help you:

...

Thanks, i take a look...

Valdas Jank?nas

Reply by email

Marc Weustink
to Valdas Jankūnas: how i can read PNG image from Lazarus resources using TLazIntfImage? I tried…
Valdas Jank?nas wrote 10 lines
Hello, how i can read PNG image from Lazarus resources using TLazIntfImage? I tried TPortableNetworkGraphic to read transparent PNG from resource or file, but in transparent areas of saved PNG image i get random garbage.Code: img:=TPortableNetworkGraphic.Create; img.LoadFromFile(FAILAS_OUT); img.SaveToFile(FAILAS_IN); img.Free; This code is correct? If so i report bug to bugtracker.

When you only loading, the stream loaded from is internally stored, so when you only save this stream is saved. It should result in an exact copy of your source.
However if you modify something, then it depends on the widgetset (since it gets converted to a bitmaphandle first). On Gtk1 or Gtk2 this will replace your 8bit alpha channel with a 1 bit channel.

If i doing same test (with file) using TLazIntfImage with TFPCustomImageReader and TFPCustomImageWriter, then i get correct results, but i not know how using TLazIntfImage load image from resource.

See TCustomBitmap loadfromlazarusresource code.

I think somehow after the image is loaded, TPortableNetworkGraphic thinks something is changed and the original stream isn't saved.

Marc

Reply by email

Valdas Jankūnas
to Marc Weustink

Marc Weustink ra??:

Show quoted text 29 lines
Valdas Jank?nas wrote: > Hello, > how i can read PNG image from Lazarus resources using TLazIntfImage? > I tried TPortableNetworkGraphic to read transparent PNG from resource > or file, but in transparent areas of saved PNG image i get random > garbage.Code: > img:=TPortableNetworkGraphic.Create; > img.LoadFromFile(FAILAS_OUT); > img.SaveToFile(FAILAS_IN); > img.Free; > This code is correct? If so i report bug to bugtracker. When you only loading, the stream loaded from is internally stored, so when you only save this stream is saved. It should result in an exact copy of your source. However if you modify something, then it depends on the widgetset (since it gets converted to a bitmaphandle first). On Gtk1 or Gtk2 this will replace your 8bit alpha channel with a 1 bit channel. > If i doing same test (with file) using TLazIntfImage with > TFPCustomImageReader and TFPCustomImageWriter, then i get correct > results, but i not know how using TLazIntfImage load image from > resource. See TCustomBitmap loadfromlazarusresource code. I think somehow after the image is loaded, TPortableNetworkGraphic thinks something is changed and the original stream isn't saved.

Thanks for info.

Valdas Jank?nas

Reply by email

cyber python

Remember to include Windows in your uses clause :

function GetVolumeID(DriveChar: Char): String;
//Para obtener el # de serie de un disco
var
  MaxFileNameLength, VolFlags, SerNum: DWord;
  DrivePath : String;
begin
  DrivePath :=  DriveChar + ':\';
  if GetVolumeInformation(PChar(DrivePath), nil, 0,
     @SerNum, MaxFileNameLength, VolFlags, nil, 0)
  then
  begin
      Result := IntToStr(SerNum);
  end
  else
      Result := '';
end;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.lazarus.freepascal.org/pipermail/lazarus/attachments/20080429/19f60dfc/attachment.html

Reply by email