Board index » delphi » Drawing over a JPEG image

Drawing over a JPEG image

Hi,

  I've a TImage which is loaded with a JPEG file (with the LoadFormFile
method).
  When, in the Form OnPaint event, I try to draw over the image using the
Image.Canvas object (for example Image1.Canvas.Ellipse(x1,y1,x2,y2)), it
raises an exception with the following message: "Can only modify an Image if
it contains a bitmap".
  Of course, if I load the TImage with a BMP file, there's no problem.

  Thanks in advance,

Aldo

 

Re:Drawing over a JPEG image


so why not convert the JPEG to a bitmap (in code) and use that?

or Draw the JPEG onto the bitmap (create the bitmap yourself, and set the
size appropriately.  Then use the JPEG's Draw method to put the pixel data
from the JPEG onot the bitmap).  Check the TJPEGImage in the help file:

"TJPEGImage implements the protected Draw method introduced in TGraphic, so
it can draw itself on the canvas of another object."

David

Quote
"Aldo Caruso" <juancar...@yahoo.com> wrote in message

news:3a7c08ba_2@dnews...
Quote
> Hi,

>   I've a TImage which is loaded with a JPEG file (with the LoadFormFile
> method).
>   When, in the Form OnPaint event, I try to draw over the image using the
> Image.Canvas object (for example Image1.Canvas.Ellipse(x1,y1,x2,y2)), it
> raises an exception with the following message: "Can only modify an Image
if
> it contains a bitmap".
>   Of course, if I load the TImage with a BMP file, there's no problem.

>   Thanks in advance,

> Aldo

Re:Drawing over a JPEG image


Aldo,

The trick is to convert the jpeg to a bitmap, do any drawing on the
bitmap and then convert back to a jpeg. Below is something I put
together when I was working through these things.

Bob
----

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Image1: TImage;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  JPEG: TJPEGImage;
  BMP : TBitmap;
begin
  JPEG := TJPEGImage.create;
  BMP := TBitmap.create;

  JPEG.LoadFromFile( 'factory.jpg' );
  {If you need to draw on a jpeg, convert to bmp and draw on the bitmap
canvas
  BMP.Height := JPEG.Height;
  BMP.Width := JPEG.Width;
  BMP.Canvas.Draw(10,10, JPEG); }
  BMP.Assign(JPEG);
  Image1.Picture.Graphic:= BMP;
  JPEG.free;
  Bmp.free;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
   jp: TJpegImage;
   bmp: TBitmap;
const filename = 'factory.bmp';
 begin
   bmp:= Tbitmap.Create;
   try
     bmp.Loadfromfile(filename);
     jp:= TJpegImage.Create;
     try
       jp.Assign( bmp );
       jp.SaveToFile( ChangeFileExt( filename, '.JPG' ));
       ShowMessage('Bitmap converted to JPeg');
     finally
       jp.free
     end;
   finally
     bmp.free
   end;
 end;  

end.

Re:Drawing over a JPEG image


Thanks for your advice.

        Aldo

Re:Drawing over a JPEG image


Bob,

  Thanks for your code. But there's something I want to ask you. You've put
two alternatives (one commented and the other not) about implementing it:

{ First Alternative }

{If you need to draw on a jpeg, convert to bmp and draw on the bitmap
canvas
  BMP.Height := JPEG.Height;
  BMP.Width := JPEG.Width;
  BMP.Canvas.Draw(10,10, JPEG); }

{ Second Alternative }

  BMP.Assign(JPEG);

Why don't use allways the second one ? Is there any difference ?

Thanks in advance,
                                                            Aldo

Re:Drawing over a JPEG image


Aldo,

Example. Drawing a cross on a JPEG.

var jpg: TJpegImage;
    bmp: TBitmap;
begin
   jpg:= TJpegImage.Create;
   bmp:= TBitmap.Create;
   try
     jpg.LoadFromFile('test1.jpg');
     bmp.width:= jpg.width;
     bmp.Height:= jpg.Height;
     with bmp.Canvas do
     begin
       Draw(0,0,jpg);
       pen.color:= clRed;
       MoveTo(0,0);
       LineTo(bmp.width,bmp.height);
       moveto(bmp.width,0);
       lineTo(0,bmp.height);
     end;
     jpg.Assign(bmp);
     jpg.SaveToFile('test2.jpg');
   finally
     jpg.Free;
     bmp.free;
   end;
end;

Bob
---
Sent using Virtual Access 5.01 - download your freeware copy now
http://www.atlantic-coast.com/downloads/vasetup.exe

Re:Drawing over a JPEG image


Thanks Bob,
                                Aldo

Other Threads