...-= Using JPEG =-...

Hey, great work you are doing here.. really.. keep it up !!

k, I need a function that puts in the screen a 640x480 JPG image, in the centre, just giving the file name, something like this:

PutJpgImage('file1.jpg);

Ok, I think it's easy for you.. but I've just started with Delphi.. THANX..
menorcanetAsked:
Who is Participating?
 
viktornetConnect With a Mentor Commented:
Sorry I did't have the time to help you all the way...
0
 
menorcanetAuthor Commented:
Edited text of question
0
 
erajojCommented:
No form involved at all?
I.E. paint directly onto the screen DC? or on a form with no border or caption?

/// John
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
viktornetCommented:
uses Graphics;

procedure PutJpg(Can : TCanvas; JPGFile : string; x,y : Integer);
var
  JPG : TJpegImage;
begin
  JPG := TJpeg.Create(self);
  try
    JPG.LoadFromFile(JPGFile);
    Canvas.Draw(x,y, JPG);
  finally  
    JPG.Free;
  end;
end;

Example Call:

PutJPG(Form1.Canvas, 'C:\windows\desktop\test.jpg', 100, 100);

This will draw the jpg image on the form's canvas...\

Regards,
Viktor Ivanov
0
 
viktornetCommented:
replace JPG := TJpeg.Create(self); with JPG := TJpegImage.Create(self);

Haven't tested that so if it doesn't work please leavea comment so I can answer your question..

Regards,
Viktor Ivanov
0
 
erajojCommented:
And here's my solution:

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  JPEG; // << DON'T FORGET TO INCLUDE

type
  TForm2 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormPaint(Sender: TObject);
    procedure FormClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    jpgImg: TJPEGImage; // here's the image declared
    { Private declarations }
  public
    function PutJpgImage( sFilename: string ): Boolean;
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.DFM}

procedure TForm2.FormCreate(Sender: TObject);
begin
  BorderStyle := bsNone;
  FormStyle   := fsStayOnTop; // above other windows
  jpgIMG      := TJPEGImage.Create; // create jpeg image
end;

function TForm2.PutJpgImage( sFilename: string ): Boolean;
begin
  Result := False;
  try
    jpgImg.LoadFromFile( sFilename );
    with jpgImg do SetBounds(
      ( Screen.Width - Width ) div 2,
      ( Screen.Height - Height ) div 2,
      Width, Height
    ); // center window on screen
    Show; // show window
    Result := True; // all went well!
  except
    // nothing to handle right now
  end;
end;

procedure TForm2.FormPaint(Sender: TObject);
begin
  Canvas.Draw( 0, 0, jpgImg ); // draw image every time the window is painted
end;

procedure TForm2.FormClick(Sender: TObject);
begin
  Close; // close window if clicked
end;

procedure TForm2.FormDestroy(Sender: TObject);
begin
  jpgImg.Free; // clean up
end;

end.

This is how you use it:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Form2.PutJpgImage( 'D:\Graphics\Images\Animals\Water_Mammals\dolph1.jpg' );
end;

Hi Vik, you were fast!

/// John
0
 
viktornetCommented:
Oh, that was it.... I forgot whether I need to include JPEG or Graphics, so John told you which one to use ... 10x John
0
 
viktornetCommented:
Hello ya,...

I wrote this from my school so I don't know if it works but it is suppose to be working... I didnt
have Delphi to check it out... now I do, but I guess if you have problemns just leave a comment...
0
 
menorcanetAuthor Commented:
Hey there, Sorry, but that didn't work.

The first answer, from viktornet: I included the JPEG in the uses declaration,
 and it didn't work,, then I replaced the "JPG := TJpeg.Create(self); with JPG := TJpegImage.Create(self); " as you said, then Delphi days " Not enough actual parameters " and stops in this line :  "  JPG := TJpegImage.Create(self)   ".

The second one, from erajoj, when I start the program, is stops and says there's a program exception or an access violation at address ... and stops at the FormPaint line.

And... well... what's a " Canvas " ? ..

k, thanx for your time again, thanx

0
 
viktornetCommented:
Ok, here it is now tested with Delphi, and it works....

//Add to uses clause
uses Jpeg;

procedure PutJpg(Can : TCanvas; JPGFile : string; x,y : Integer);
var
  JPG : TJpegImage;
begin
  if FileExists(JPGFile) then begin
    JPG := TJpegImage.Create;
    try
      JPG.LoadFromFile(JPGFile);
      Can.Draw(x,y, JPG);
    finally
      JPG.Free;
    end;
  end;
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
  PutJPG(Form1.Canvas, 'C:\WINDOWS\DESKTOP\VIKTOR\Sad.jpg', 100, 100);
end;

This one works.....

Regards,
Viktor Ivanov

0
 
erajojCommented:
Hi,
The "Canvas" is where all graphics and text are drawn.
There are several different canvases, such as printer canvas, screen canvas, metafile canvas, memory canvas... All canvases encasulate windows DC's (device contexts).
The code I send you works, I've tested it. If you show the form before loading the JPEG, you will get an exception since you can't draw something that is not there.

Do something like this to avoid the exception:

        function TForm2.PutJpgImage( sFilename: string ): Boolean;
        begin
          Result := False;
          try
            jpgImg.LoadFromFile( sFilename );
            with jpgImg do SetBounds(
              ( Screen.Width - Width ) div 2,
              ( Screen.Height - Height ) div 2,
              Width, Height
            ); // center window on screen
            Show; // show window
            Result := True; // all went well!
          except
            // nothing to handle right now
          end;
          // CHANGE HERE!...
          Tag := Integer( Result );
        end;

        procedure TForm2.FormPaint(Sender: TObject);
        begin
          // CHANGE HERE!...
          if ( Tag = 1 ) then Canvas.Draw( 0, 0, jpgImg ); // draw image every time the window is painted
        end;

/// John
0
 
menorcanetAuthor Commented:
hey !

yeah, the last answer from viktornet works !
thanx !

but now... how can I have more than 1 JPG image on the screen ?
Can you write the code to make a component that does the same as the IMAGE component, but with JPG ?

I increased the points, ok, thanx again.

0
 
menorcanetAuthor Commented:
hey !

yeah, the last answer from viktornet works !
thanx !

but now... how can I have more than 1 JPG image on the screen ?
Can you write the code to make a component that does the same as the IMAGE component, but with JPG ?

I increased the points, ok, thanx again.

0
 
menorcanetAuthor Commented:
I mean, the code to make a new component and install it,
add it to the components library.

k,

0
 
menorcanetAuthor Commented:
hey !  , one more thing..

I have a second form, a very little form, and when I move it
 on the screen ( at run-time ), it's VERY slow, it doesn't
  move well,. and if I don't put a JPG image, it moves well.

why is it ?
( Note: the program is an images viewer ).
0
 
menorcanetAuthor Commented:
It's ok, but I have more questions.. Look above ..

I Increased the points.

k, thanx ya.
0
 
menorcanetAuthor Commented:
so ?
0
 
menorcanetAuthor Commented:
hi ? anyone there ?
0
 
menorcanetAuthor Commented:
hey Boris, if u ever see this, I saw the page u made, and I have a component to
 play MP3 files, can I send it ?

0
 
viktornetCommented:
Hello menorcanet....

Why not use a simple TIMage and just draw the JPG in OnPaint()

Cheers,
Viktor
0
 
menorcanetAuthor Commented:
.... ?     how ?

0
 
erajojCommented:
Hi again,
Just want to know; are you trying to play with us, or don't you really know these things?
I can't see the problem!?

Drawing a TJPEGImage on OnPaint: Image1.Canvas.Draw( 0, 0, JPEGImage );

/// John
0
 
viktornetCommented:
uses JPEG;
//some stuff..
type TJPGImage = class(TImage)
private
FJPGImage : TJpegImage;
procedure SetJPG(JPG : TJpegImage);
procedure Paint;override;
//otherstuff....
public
constructor Create(AOwner : TComponent);override;
destructor Destroy;override;
//stuff
publish
property JPGImage : TJpegImage read FJPGImage write SetJPG;
//other stuff////
procedure TJPGImage.Paint;
begin
  Canvas.Draw(0, 0, FJPGImage);
end;
procedure TJPGImage.SetJPG(JPG : TJpegImage);
begin
  FJPGImage := JPG;
  Invalidate;
end;
constructor TJPGImage.Create(AOwner ; TComponent);
begin
  Inherited Create(AOwner);
  FJPGImage := TJpegImage.Create;
  FJPGImage := nil;
end;
destructor TJPGImage.Destroy;
begin
  FJPGImage.Free;
  FJPGImage := nil;
  inherited Destroy;
end;

Dunno if it works but you got the idea.... Bye

Cheers,
Viktor
0
 
menorcanetAuthor Commented:
erajoj: No, I'm not playing with anyone, as I said before, I've just starred with Delphi,
 and have no idea of how components work.

well, thanx, that worked, ( i did this: Load a JPG file, and Image1.Canvas.Draw(0,0,JPG) )
Is there any way to do something like this:   Image1.Picture:= LoadFromFile('c:\a.jpg)
 to avoid having to load it first to a Jpeg component.

___________________

hey viktornet:

I have no idea on how to make components, so I opened a pas file fom a component
 that I found and installed, and I saved the pas file like this:

unit JpegImage;

interface

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

type
    TJPGImage = class(TImage)
    private
      FJPGImage : TJpegImage;
      procedure SetJPG(JPG : TJpegImage);
      procedure Paint;override;
      //otherstuff....
     protected
    { Protected declarations }
    public
      constructor Create(AOwner : TComponent);override;
      destructor Destroy;override;
       //stuff
    published
      property JPGImage : TJpegImage read FJPGImage write SetJPG;
      //other stuff////
end;

procedure Register;

implementation

constructor TJPGImage.Create(AOwner : TComponent);
begin
  Inherited Create(AOwner);
  FJPGImage := TJpegImage.Create;
  FJPGImage := nil;
end;

destructor TJPGImage.Destroy;
begin
  FJPGImage.Free;
  FJPGImage := nil;
  inherited Destroy;
end;

procedure TJPGImage.Paint;
begin
  Canvas.Draw(0, 0, FJPGImage);
end;

procedure TJPGImage.SetJPG(JPG : TJpegImage);
begin
  FJPGImage := JPG;
  Invalidate;
end;

procedure Register;
begin
  RegisterComponents('Standard', [TJPGImage]);
end;

end.




and I installed it.
Everything seemed to be working alright, then I went to the standard palette,
 dropped a TJPGImage on the form, and when I loaded an image, I had this error:

    " Can only modify an Image if it contains a bitmap "

thanx
0
 
viktornetCommented:
Use something like ...

if FJPGImage <> nil then
  Canvas.Draw(0, 0, FJPGImage);

or something like that...

Cheers,
Viktor
0
 
menorcanetAuthor Commented:
I replaced

         Canvas.Draw(0, 0, FJPGImage);
with
         if FJPGImage <> nil then
in the procedure:            procedure TJPGImage.Paint;

when I load an image, delphi gives no error, but I can't see the image.


0
 
menorcanetAuthor Commented:
About my comment on 25-Oct:

That error (  " Can only modify an Image if it contains a bitmap "  )
appeared if I loaded the image with the Picture property.
If I load it wih the JPGImage, I get an Access violation, and the message
" canvas doesn't allow drawing ".


0
 
viktornetCommented:
Sorry, I really don't have time to do this thing right now..... Maybe in a few days I'll try to see what I can do... Bye.

Cheers,
Viktor
0
 
menorcanetAuthor Commented:
so.. ?

0
 
viktornetCommented:
so??? Don't really have time to play with this one right now... Probably delete the question and try asking it again so other can take a look at it... I've got other work to do...
0
 
menorcanetAuthor Commented:
well, ok, drop a comment as an answer, I'll accept it with an A.


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.