Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TBitmap.Create not working in delphi console application

I need to process a set of bmp files using a console application, i'm using the TBitmap class, but the code doesn't compile because this error

E2003 Undeclared identifier: 'Create'

This sample app reproduces the issue

{$APPTYPE CONSOLE}

{$R *.res}

uses
 System.SysUtils,
 Vcl.Graphics,
 WinApi.Windows;

procedure CreateBitMap;
Var
  Bmp  : TBitmap;
  Flag : DWORD;
begin
  Bmp:=TBitmap.Create; //this line produce the error of compilation
  try
    //do something
  finally
   Bmp.Free;
  end;
end;

begin
  try
    CreateBitMap;

  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

why this code doesn't compile?

like image 596
Salvador Avatar asked May 06 '12 22:05

Salvador


1 Answers

The issue is in the order of your uses clause, the WinApi.Windows and Vcl.Graphics units have a type called TBitmap, when the compiler find an ambiguous type resolves the type using the last unit of the uses list where is present. in this case use the TBitmap of the Windows unit which points to the BITMAP WinAPi structure , to resolve this change the order of your units to

uses
 System.SysUtils,
 WinApi.Windows,
 Vcl.Graphics;

or you can declare the type using the full qualified name like so

procedure CreateBitMap;
Var
  Bmp  : Vcl.Graphics.TBitmap;
  Flag : DWORD;
begin
  Bmp:=Vcl.Graphics.TBitmap.Create;
  try
    //do something
  finally
   Bmp.Free;
  end;
end;
like image 169
RRUZ Avatar answered Sep 23 '22 05:09

RRUZ