I am trying to create a textTool for my image editing app, so I have a button on the main form saying "Text tool". When I click it, a new form (modal) shows allowing me to select a font and enter a text in a RichEdit.
My idea is to have the user format his text in the RichEdit, and when he is satisfied, he should click on the modal button and the text (formatted) will be inserted in a new layer in my image. Also my idea is to treat the text as lines of text and render them separatelly in the new Bitmap32 and then assign the obtained bitmap to the new Layer.
For that I use this function
function GetTextWidth(const Text: string; const Font: TFont): Integer;
var
Canvas: TCanvas;
begin
Canvas := TCanvas.Create;
try
Canvas.Handle := GetDC(0);
try
Canvas.Font.Assign(Font);
Result := Canvas.TextWidth(Text);
finally
ReleaseDC(0, Canvas.Handle);
end;
finally
Canvas.Free;
end;
end;
and parse the lines of richedit and get the textWidth for each one like this:
for q := 0 to TextEditor.RichEdit1.Lines.Count-1 do
begin
latime:=GetTextWidth(TextEditor.RichEdit1.Lines[q], TextEditor.RichEdit1.Font);
Memo1.Lines.Add('Text row '+IntToStr(q)+' width='+IntToStr(latime));
end;
So I will easily obtain the largest width line (maxwidth), that I intend to use for generating my Bitmap32.
So for my Bitmap32 I use the following code (which does not show anything unfortunatelly)
// generating the Bitmap32 to place text on it
tmp := TBitmap32.Create;
tmp.SetSize(maxwidth, textLineHeight*TextLineCount);
for q := 0 to textLineCount-1 do
begin
tmp.RenderText(maxwidth,textLineHeight,TextEditor.RichEdit1.Lines[q],1,myFont.Color);
Memo1.Lines.Add('rendering line of text:'+TextEditor.RichEdit1.Lines[q]);
end;
// Generating the text layer
B := TBitmapLayer.Create(ImgView.Layers);
// Assigning the 'written' bitmap to the new layer
B.Bitmap.Assign(tmp);
B.Bitmap.DrawMode:=dmBlend;
// Positioning
with ImgView.GetViewportRect do
P := ImgView.ControlToBitmap(GR32.Point((Right + Left) div 2, (Top + Bottom) div 2));
// Sizing the layer
B.Location:=GR32.FloatRect(P.X - maxwidth, P.Y - textLineHeight, P.X + maxwidth, P.Y + textLineHeight);
// displaying the new text layer
imgView.Invalidate;
So I get no errors, but the new layer does not show... It's like nothing happens.
I tryied using fixed values for width and height (I used 200 bor both) but still nothing showed up on my ImageView, so I assume it must be something wrong with the Location maybe?
Please assist me in solving this problem.
Thank you very much