Solved

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

Posted on 1998-10-07
31
245 Views
Last Modified: 2013-11-19
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..
0
Comment
Question by:menorcanet
  • 16
  • 11
  • 4
31 Comments
 

Author Comment

by:menorcanet
ID: 1342172
Edited text of question
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1342173
No form involved at all?
I.E. paint directly onto the screen DC? or on a form with no border or caption?

/// John
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1342174
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1342175
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
 
LVL 4

Expert Comment

by:erajoj
ID: 1342176
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1342177
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1342178
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
 

Author Comment

by:menorcanet
ID: 1342179
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1342180
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
 
LVL 4

Expert Comment

by:erajoj
ID: 1342181
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
 

Author Comment

by:menorcanet
ID: 1342182
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
 

Author Comment

by:menorcanet
ID: 1342183
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
 

Author Comment

by:menorcanet
ID: 1342184
I mean, the code to make a new component and install it,
add it to the components library.

k,

0
 

Author Comment

by:menorcanet
ID: 1342185
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
 

Author Comment

by:menorcanet
ID: 1342186
It's ok, but I have more questions.. Look above ..

I Increased the points.

k, thanx ya.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:menorcanet
ID: 1342187
so ?
0
 

Author Comment

by:menorcanet
ID: 1342188
hi ? anyone there ?
0
 

Author Comment

by:menorcanet
ID: 1342189
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1342190
Hello menorcanet....

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

Cheers,
Viktor
0
 

Author Comment

by:menorcanet
ID: 1342191
.... ?     how ?

0
 
LVL 4

Expert Comment

by:erajoj
ID: 1342192
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1342193
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
 

Author Comment

by:menorcanet
ID: 1342194
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1342195
Use something like ...

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

or something like that...

Cheers,
Viktor
0
 

Author Comment

by:menorcanet
ID: 1342196
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
 

Author Comment

by:menorcanet
ID: 1342197
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1342198
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
 

Author Comment

by:menorcanet
ID: 1342199
so.. ?

0
 
LVL 10

Expert Comment

by:viktornet
ID: 1342200
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
 

Author Comment

by:menorcanet
ID: 1342201
well, ok, drop a comment as an answer, I'll accept it with an A.


0
 
LVL 10

Accepted Solution

by:
viktornet earned 20 total points
ID: 1342202
Sorry I did't have the time to help you all the way...
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In this second Micro Tutorial in the series,  viewers will learn more basic shortcuts and functions of Illustrator. In this tutorial you will learn about the eyedropper tool, zoom , ellipses and more.
Learn how to use the remote presentation tool in Prezi to allow you to harness the power of this cloud based presentation platform. You can show your presentation with a remote audience using this free tool.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now