Página 1 de 1

Add some text to JPEG images .....

Publicado: Mar Jul 25, 2017 12:14 pm
por ChrisGillard
Hi,

I would like to take some existing JPEG image files and overlay the filename and a 'Title' as text on them.

Can I do this using GDI, just by displaying the file on some sort of canvas, drawing some text on that canvas and then saving the image back to a file?

I see we have TImage and TImageEditor using FreeImage in the Enterprise Edition .... is this the way to go?

Any general thoughts or simple example code would be much appreciated.

..... I've just researched the Samples folder and there are some useful Image display and edit examples. I should have looked there first .....

Not seeing any text drawing though?

Thanks

Chris

Re: Add some text to JPEG images .....

Publicado: Mar Jul 25, 2017 1:08 pm
por ignacio
Hello,

If you just want to put the text on top of the image you can use a label with its property lTransparent set to true. Another way is to use its OnPainted event. Something like this:

Código: Seleccionar todo

METHOD Image1Painted( oSender, hDC, cPaintStruct ) CLASS TForm1

   LOCAL oCanvas AS CLASS TCanvas  
   oCanvas := TCanvas():NewFromDC( hDC )   
   oCanvas:TextOut(0,0, "Hello", 500, clBlack )   
   oCanvas:End()
   
RETURN Nil
In case you need to modify the image file adding some text information, I'm afraid you must deal with some 'C' code. This is the way to go:

- Create a compatible DC:
- Create a compatible Bitmap on memory with identical size of the original bitmap
- Copy (Bitblt function) the original bitmap to the compatible Bitmap
- Print the text on the compatilbe DC
- Save the compatible bitmap to file

If you need help on this please try to prototype the function you will need. Something like this:

Código: Seleccionar todo

PutTextOnBitmap( cSourceFile, cTargetFile, nX, nY, cText, nColor )
And we will give a budge for that function.

BTW you may try to use the event OnPainted and then use the:

Código: Seleccionar todo

XA_CaptureBitmap( <hWnd>, [<aRect>] | [<nLeft>, <nTop>, <nRight>, <nBottom>] ) --> hBitmap
Regards,

Re: Add some text to JPEG images .....

Publicado: Mar Jul 25, 2017 1:28 pm
por ChrisGillard
Hi Ignacio,

Thank you for your comprehensive reply.

Its the second example, the images are high resolution photos in .JPG files. I need to add the text to them and then re-save them at the same resolution.

I will work on your second example and see how far I get and then ask for more help.

I'm glad to see you released 5.0 a few days ago.

I have installed it and taken a quick look at some of the extra features but am continuing with 4.X for a little longer as I have a few projects which need to be done quickly.

I'm sure I will be back!

Regards

Chris