Bitmap image is not valid

When I run the following code in Delphi 3, I get the following error:

Bitmap image is not valid

Why?


procedure TForm1.Button1Click(Sender: TObject);
begin
With Image1 do begin
      Bitmap.LoadFromFile('hello.jpg');
      Picture.Bitmap := Bitmap;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
      Bitmap := TBitmap.Create;      
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
      Bitmap.Free;
end;
Monroe406Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Monroe406Author Commented:
Oh...yes, I do declare Bitmap in the Interface section ...I simply did not post it in the above code.

var
      Bitmap : TBitmap;
0
kretzschmarCommented:
hi monroe,

just add jpeg in your uses-clause, declare a var typ TJpgImage

and change your code as follows

var
  JPGImage : TJPGImage
procedure TForm1.Button1Click(Sender: TObject);
begin
With Image1 do begin
JPGImage.LoadFromFile('hello.jpg');
Picture.Bitmap.Assign(JPGImage); //other variants are possible
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
JPGImage := TJPGImage.Create;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
JPGImage.Free;
end;

meikl


0
DrDelphiCommented:
if you are using Delphi 3, all you need to do to add a Jpeg into an Timage is include the jpeg unit in your uses section. Then even in designtime you can add a Jpeg. Otherwise:

Image1.Picture.LoadFromfile('Hello.jpg');


Good luck!!

0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Monroe406Author Commented:
when I add Jpeg to my Uses section I get the following error:

Error (0): WARNING. Duplicate resource(s).
ERror (0): Type 10 (RCDATA), ID TFORM1:.

Error (0): File Unit1.DFM resource kept; file jpeg.DFM resource discarded.

------ code ----------

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, Jpeg;

type
  TForm1 = class(TForm)
    Image1: TImage;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
var
Bitmap : TBitmap;

implementation

{$R *.DFM}


procedure TForm1.FormCreate(Sender: TObject);
begin
Bitmap := TBitmap.Create;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
With Image1 do begin
Bitmap.LoadFromFile('d:\homepage\blueline.jpg');
Picture.Bitmap := Bitmap;
end;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Bitmap.Free;
end;

end.


0
Monroe406Author Commented:
To: meikl


Your code will not compile under Delphi 3.

0
DrDelphiCommented:
You have a couple of issues here.. for one thing, it is apparent that you have a form file named jpeg, hence the "jpeg.dfm" discarded error. Secondly, you are still trying to assign the loadfromfile method to the bitmap property of the Timage. It should be Picture.LosdFromFile... observe:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, Jpeg;

type
  TForm1 = class(TForm)
    Image1: TImage;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}


procedure TForm1.FormCreate(Sender: TObject);
begin
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
With Image1 do begin
Picture.LoadFromFile('d:\homepage\blueline.jpg');
end;

end.



Use this code and only this code in a brand new project and you will see that it DOES compile in Delphi 3. The error is yours, not mine. However, if you insist on using a bitmap in the Timage, then you might consider:


type
  TForm1 = class(TForm)
    Image1: TImage;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  jpg:Tjpegimage;
implementation

{$R *.DFM}


procedure TForm1.FormCreate(Sender: TObject);
begin
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Jpg:=Tjpegimage.create;
  With jpg do
  begin
    LoadFromFile ('d:\homepage\blueline.jpg');
   end;
With Image1 do begin
Picture.Bitmap.Assign(jpg);
jpg.free;
end;

end.



Good luck!!





0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Fonts Typography

From novice to tech pro — start learning today.