Recent

Author Topic: Putting Text on a canvas over an image - what am I doing wrong???  (Read 7795 times)

wpflum

  • Sr. Member
  • ****
  • Posts: 287
Putting Text on a canvas over an image - what am I doing wrong???
« on: September 23, 2016, 04:23:50 pm »
I'm working on a part of a program which I thought would be easiy, loading a jpg into an image object and then putting text over it, the only problem is that I can't seem to get the text to be black or any color other than grey.

I Built an example to make sure I wasn't doing something wrong in the main program but get the same problem.  I'm guessing it's something stupid but for the life of me I'm not seeing what the problem is.

Here is the code.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}unit Unit1;
  4.  
  5. {$mode objfpc}{$H+}
  6.  
  7. interface
  8.  
  9. uses
  10.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  11.   ExtCtrls;
  12.  
  13. type
  14.  
  15.   { TForm1 }
  16.  
  17.   TForm1 = class(TForm)
  18.     Button1: TButton;
  19.     Invoice: TImage;
  20.     procedure Button1Click(Sender: TObject);
  21.  
  22.   private
  23.     { private declarations }
  24.   public
  25.     { public declarations }
  26.   end;
  27.  
  28. var
  29.   Form1: TForm1;
  30.  
  31. implementation
  32.  
  33. {$R *.lfm}
  34.  
  35. { TForm1 }
  36.  
  37. procedure TForm1.Button1Click(Sender: TObject);
  38. begin
  39.  
  40.   with Invoice do
  41.  
  42.   begin
  43.     Picture.LoadFromFile('Images\test.jpg');
  44.     Picture.Bitmap.Canvas.Font.Color := clBlack;
  45.     Picture.Bitmap.Canvas.Font.Size := 20;
  46.     Picture.Bitmap.Canvas.Brush.Style := bsClear;
  47.  
  48.     Picture.Bitmap.Canvas.TextOut(20, 20, 'HELLO WORLD');
  49.  
  50.   end;
  51.  
  52. end;
  53.  
  54. end.

What I expected to see is HELLO WORLD in black on the upper left of the image when I click the button and the TImage object is loaded with the picture and written to with textout but what I get is very light grey text.  I thought it might be something with the image itself, I converted it from a pdf of a blank invoice, so I grabbed a sample Blank Invoice image off the web and tried it and got the same thing.

So what stupid thing am I doing????

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Putting Text on a canvas over an image - what am I doing wrong???
« Reply #1 on: September 23, 2016, 05:22:24 pm »
This works here as expected (Linux gtk2, using a different .jpg file).
What widgetset do you target?

wpflum

  • Sr. Member
  • ****
  • Posts: 287
Re: Putting Text on a canvas over an image - what am I doing wrong???
« Reply #2 on: September 23, 2016, 05:36:49 pm »
Windows 10 32 bit.  I attached a screen clip of what I get.


Laurie

  • New Member
  • *
  • Posts: 19
Re: Putting Text on a canvas over an image - what am I doing wrong???
« Reply #3 on: September 25, 2016, 02:28:28 am »
Hi wpflum, I have a copy of your image file and save it as both a JPG and a BMP file.
If I load it as jpeg file then I get exactly the same result as you.
I am using Win 10 32bit Laz 1.6.

BUT..if the image is loaded a BMP file then the text is black as expected.

The only change I made to your code was to allow file name selection:

procedure TForm1.Button1Click(Sender: TObject);
begin
  with Invoice do begin
    If OpenPictureDialog1.Execute then begin
      Picture.LoadFromFile(OpenPictureDialog1.FileName{'c:\Laz\Projects\test.bmp'});
      Picture.Bitmap.Canvas.Font.Color := clBlack;
      Picture.Bitmap.Canvas.Font.Size := 20;
      Picture.Bitmap.Canvas.Brush.Style := bsClear;
      Picture.Bitmap.Canvas.TextOut(20, 20, 'HELLO WORLD');
      end;
    end; //with
end;

(Oh and I cleaned up the duplicate name of the unit a the start of the unit code)

I'm not sure what is going on here. 

Laurie

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Putting Text on a canvas over an image - what am I doing wrong???
« Reply #4 on: September 25, 2016, 05:37:05 am »
The why seems clear, i just don't understand the reason why it happens :-/

The jpg is internally stored as 32-bit transparent (tmAuto) bitmap with transparent color = $0040362C. All pixels in the picture seems marked with 255 alpha value.

The written text however uses alpha mask of zero, ergo it will only 'clear' the current visible pixels (clear = make them transparent).

A simply scanline routine that sets textout pixels to use another alpha value displays the text in the correct color (for my example i used red to be able to distinguish between available pixels and textout pixels).

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Putting Text on a canvas over an image - what am I doing wrong???
« Reply #5 on: September 25, 2016, 08:58:48 am »
Ah, i've found an interesting thread about this issue. Read and cry for your pleasure.

Seems this is not possible to do without resorting to a workaround.

ASerge

  • Hero Member
  • *****
  • Posts: 2242
Re: Putting Text on a canvas over an image - what am I doing wrong???
« Reply #6 on: September 25, 2016, 03:19:18 pm »
Ah, i've found an interesting thread about this issue. Read and cry for your pleasure.
As result this work
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   Pic: TPicture;
  4.   Bmp: TBitmap;
  5. begin
  6.   Bmp := Invoice.Picture.Bitmap;
  7.   Pic := TPicture.Create;
  8.   try
  9.     Pic.LoadFromFile('Images\test.jpg');
  10.     Bmp.Width := Pic.Width;
  11.     Bmp.Height := Pic.Height;
  12.     Bmp.PixelFormat := pf24bit;
  13.     Bmp.Canvas.Draw(0, 0, Pic.Graphic);
  14.   finally
  15.     Pic.Free;
  16.   end;
  17.   with Bmp.Canvas do
  18.   begin
  19.     Font.Color := clBlack;
  20.     Font.Size := 20;
  21.     Brush.Style := bsClear;
  22.     TextOut(20, 20, 'HELLO WORLD');
  23.   end;
  24. end;
  25.  

wpflum

  • Sr. Member
  • ****
  • Posts: 287
Re: Putting Text on a canvas over an image - what am I doing wrong???
« Reply #7 on: September 26, 2016, 02:48:50 pm »
I had seen something about a 32 bit issue so I had converted the jpeg to a 24 bit and still got the problem, probably grabbed the wrong file or didn't get the conversion from GIMP correct. 

Thanks for the help I was pulling my hair out thinking I was missing something simple.

 

TinyPortal © 2005-2018