Draw image

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Post Reply
Lena
BCBJ Master
BCBJ Master
Posts: 696
Joined: Sun Feb 06, 2011 1:28 pm

Draw image

Post by Lena »

Hi.
I want to draw a number in the upper right corner of the image. See attacments. For this purpose, I found a Pascal code.

Code: Select all

procedure TfMain.b1Click(Sender: TObject);
var
  b : TBitmap;
  r:TRectF;
begin
  Inc(n);
  b:=TBitmap.Create;
  b.SetSize(img1.Bitmap.Size);
  b.CopyFromBitmap(img1.Bitmap);
  b.Canvas.BeginScene();
  b.Canvas.Font.Style := [TFontStyle.fsBold];
  b.Canvas.Font.Size := b.Width * 1/4;
  b.Canvas.Fill.Color := TAlphaColorRec.Black;
  r := b.BoundsF;
  //for top corner
  r := rectf( bmp.width *0.5, 0, bmp.width, bmp.height*0.5)
  r.Height := r.Height * 0.75;
  b.Canvas.FillText(r, n.ToString, False, 1, [], TTextAlign.Center, TTextAlign.Center);

  b.Canvas.EndScene;
  img2.Bitmap := b;
  b.Free;
end;

end.
I am trying to translate the code to C++:

Code: Select all

	std::unique_ptr<TBitmap> NumberForImage(new TBitmap());
	NumberForImage->SetSize(Image1->Bitmap->Size);
	NumberForImage->CopyFromBitmap(Image1->Bitmap);
	NumberForImage->Canvas->BeginScene();
	NumberForImage->Canvas->Font->Style = TFontStyles() << fsBold;
	NumberForImage->Canvas->Font->Size = NumberForImage->Width * 1/4;
	NumberForImage->Canvas->Fill->Color = TAlphaColorRec::Black;
	TRectF r;
	r = NumberForImage->BoundsF;
	r = RectF(NumberForImage->Width *0.5, 0, NumberForImage->Width, NumberForImage->Height*0.5);
	r.Height = r.Height() * 0.75; //error
	NumberForImage->Canvas->FillText(r, L"4", false, 1, TFillTextFlags() << TFillTextFlag::RightToLeft, TTextAlign::Center, TTextAlign::Center);
	NumberForImage->Canvas->EndScene();
	Image2->Bitmap = NumberForImage.get();

[bcciosarm64 Error] UnitMain.cpp(96): assigning to 'float (__closure *)()' from incompatible type 'double'


How will it be correct?
Attachments
totalnum.png
totalnum.png (10.6 KiB) Viewed 975 times
total1.png
total1.png (10.41 KiB) Viewed 975 times
rlebeau
BCBJ Author
BCBJ Author
Posts: 1715
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA
Contact:

Re: Draw image

Post by rlebeau »

The error message says you are trying to assign a double to a function pointer, which is not legal.

In Delphi, the TRectF.Height member is a read/write property:

Code: Select all

property Height: Single read GetHeight write SetHeight;
But in C++, the Height member is a non-static method instead:

Code: Select all

float Height() const _ALWAYS_INLINE
  { return bottom - top ; }
You can't assign a value to a function, which is why you get the error. You will have to use the public SetHeight() method instead:

Code: Select all

r.SetHeight(r.Height() * 0.75);
I don't know why Embarcadero designed the TRectF struct in C++ with this discrepency. A C++ struct in Embarcadero's compilers is perfectly capable of using properties.
Remy Lebeau (TeamB)
Lebeau Software
Lena
BCBJ Master
BCBJ Master
Posts: 696
Joined: Sun Feb 06, 2011 1:28 pm

Re: Draw image

Post by Lena »

Thank You very much!!!
Post Reply