Recent

Author Topic: Lazbarcode 3of9 - half barcode  (Read 696 times)

Int3g3r

  • New Member
  • *
  • Posts: 27
Lazbarcode 3of9 - half barcode
« on: May 19, 2025, 09:54:56 am »
Hello

I am trying to generate a barcode with Lazbarcode (3of9).
As I am saving this to a database I don't need to see the barcode element on the form.
So I have put it on a PageControl and I will hide it.

About the problem:
If the barcode is not visible, only the "half" barcode is shown in the picture.
Can someone explain to me what is going on here?
How can I work around this?

(see attamchents, Lazarus 3.6, FPC 3.2.2, x86_64-win64)

Code: Pascal  [Select][+][-]
  1. procedure TformMain.btnGenerateClick(Sender: TObject);
  2. var
  3.   R: TRect;
  4. begin
  5.   barcode.Text:= RandomRange(1111111111,9999999999).ToString;
  6.   barcode.AutoSize:= true;
  7.   barcode.AdjustSize;
  8.   barcode.Generate;
  9.  
  10.   FreeAndNil(barcodeImage);
  11.   barcodeImage := TBitmap.Create;
  12.   R := Rect(0,0,barcode.Width,barcode.Height);
  13.   barcodeImage.SetSize(barcode.Width,barcode.Height);
  14.   barcodeImage.Canvas.Brush.Color:= clWhite;
  15.   barcodeImage.Canvas.FillRect(R);
  16.   barcode.PaintOnCanvas(barcodeImage.Canvas, R);
  17.  
  18.   bgImage.Picture.Assign(barcodeImage);
  19. end;  
  20.  


Regards  Int3g3r
« Last Edit: May 19, 2025, 02:24:06 pm by Int3g3r »

paule32

  • Hero Member
  • *****
  • Posts: 516
  • One in all. But, not all in one.
Re: Lazbarcode 3of9 - half barcode
« Reply #1 on: May 19, 2025, 10:59:14 am »
you could place a TScrollBox onto a TabSheet Page with Align := alClient;
- also TabSheet is the Parent of TScrollBox, and within this ScrollBox, you add your Elements.
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

paweld

  • Hero Member
  • *****
  • Posts: 1422
Re: Lazbarcode 3of9 - half barcode
« Reply #2 on: May 19, 2025, 11:55:57 am »
Code: Pascal  [Select][+][-]
  1. procedure TformMain.Button1Click(Sender: TObject);
  2. var
  3.   b39: TBarcode3of9;
  4.   ms: TMemoryStream;
  5. begin
  6.   ms := TMemoryStream.Create;
  7.   b39 := TBarcode3of9.Create(nil);
  8.   b39.Text := RandomRange(1111111111,9999999999).ToString;
  9.   b39.Generate;
  10.   b39.SaveToStream(ms);
  11.   b39.Free;
  12.   ms.Position := 0;
  13.   bgImage.Picture.LoadFromStream(ms);
  14.   ms.Free;
  15. end;    
Best regards / Pozdrawiam
paweld

wp

  • Hero Member
  • *****
  • Posts: 12871
Re: Lazbarcode 3of9 - half barcode
« Reply #3 on: May 19, 2025, 12:17:36 pm »
IIRC, the barcode components in the LazBarcode package do some final sizing calculations during the painting process. Therefore, when the barcode is not visible, it is not drawn and the final size is not available. Your idea is to draw to a bitmap, but you need the size first to dimension the bitmap - a hen/egg problem... But you can save the barcode to a stream as a bitmap where you do not explicitely have to create a TBitmap instance (TBarcode.SaveToStream(stream, TBitmap)), and this works:

Code: Pascal  [Select][+][-]
  1. procedure TformMain.Button1Click(Sender: TObject);
  2. var
  3.   stream: TMemoryStream;
  4. begin
  5.   barcode.Text:='0123456789'; // RandomRange(1111111111,9999999999).ToString;
  6.   barcode.Generate;
  7.   stream := TMemoryStream.Create;
  8.   try
  9.     barcode.SaveToStream(stream, TBitmap);
  10.     stream.Position := 0;
  11.     bgImage.Picture.LoadFromStream(stream);
  12.   finally
  13.     stream.Free;
  14.   end;
  15. end;
This is even possible when you hide the barcode - this way you do not need to "hide" in on a pagecontrol .

Since you do not want to see the barcode you could also try the TFPDrawBarcode with comes with FPC in fcl-image; you would not have to install a third-party package this way...

Code: Pascal  [Select][+][-]
  1. program demo_3of9;
  2. uses
  3.   Types, fpImage, fpBarCode, fpImgBarCode, fpWritePng;
  4. const
  5.   HEIGHT = 60;
  6.   NUMERIC_TEXT = '0123456789';
  7.   ENCODING = be39;    // or be39Extended?
  8.   UNIT_WIDTH = 2;
  9.   WEIGHT = 4;
  10.   FILE_NAME = '3of9.png';
  11. var
  12.   barcode: TFPDrawBarcode;
  13.   width: Integer;
  14. begin
  15.   barcode := TFPDrawBarcode.Create;
  16.   try
  17.     barcode.Encoding := ENCODING;
  18.     barcode.UnitWidth := UNIT_WIDTH;
  19.     barcode.Weight := WEIGHT;
  20.     barcode.Text := NUMERIC_TEXT;
  21.     width := barcode.CalcWidth;
  22.     barcode.Rect := Rect(0, 0, width, HEIGHT);
  23.     barcode.Image := TFPCompactImgGray8Bit.Create(barcode.Rect.Width, barcode.Rect.Height);
  24.     try
  25.       if barcode.Draw then
  26.       begin
  27.         barcode.Image.SaveToFile(FILE_NAME);
  28.         WriteLn('Saved to file ', FILE_NAME);
  29.       end else
  30.         WriteLn('Error generating barcode.');
  31.     finally
  32.       barcode.Image.Free;
  33.     end;
  34.   finally
  35.     barcode.Free;
  36.   end;
  37. end.

[EDIT]
paweld was faster than me.
« Last Edit: May 19, 2025, 12:19:55 pm by wp »

Int3g3r

  • New Member
  • *
  • Posts: 27
Re: Lazbarcode 3of9 - half barcode
« Reply #4 on: May 19, 2025, 02:23:41 pm »
Thanks to everyone for their help.
Many thanks for the explanation.  :)

Regards Int3g3r

 

TinyPortal © 2005-2018