Dennis van Vugt
Dennis van Vugt

Reputation: 21

Copy text drawn on TImage to another TImage

The following is done by using Delphi 5, for your information.

I have a small issue with copying the content of one TImage to another.

The first TImage contains drawn text that I have loaded from a text file. See the below code:

function TMainForm.GetText(Img: TImage; v: string; dosize: boolean): boolean;
  var f:extended;
    p:timage;
    fl: textfile;
    s, t:string;
    crect:Trect;
    ds: Longint;
    ws: widestring;
begin
  v      := GetImgFile(v);
  if not fileexists(v) then
    result := false
  else try
    p := img;
    dosize := dosize and img.Stretch;
    AssignFile(fl, v);
    reset(fl);
    crect                := p.ClientRect;
    p.canvas.Font        := Refline.font;
    p.canvas.Brush.Style := bsClear;
    p.canvas.Brush.Color := clBtnFace;
    p.Canvas.Font.Size   := 8;
    p.canvas.FillRect(crect);
    ds                   := DT_NOPREFIX or DT_EXPANDTABS or DT_WORDBREAK;
    while not eof(fl) do begin
        Readln(fl, s);
        t := t + s;
    end;
    ws := StrToWide(t);
    DrawTextW(p.canvas.Handle, pwidechar(ws), length(ws), crect, ds);
    result := true;
  except
    result := false;
  end;
end;

The line v := GetImgFile receives the complete path to the text file.

img is the TImage that I have placed on the form. By using DrawTextW, I can draw the text on this TImage.

Before that, I have prepared a TRect for the TImage with the following code:

myimage.tag                   := 1;
myimage.canvas.Brush.Color    := clBtnFace;
myimage.canvas.Brush.Style    := bsClear;
myimage.Canvas.Font           := refline.font;
crect2                        := Rect(myimage.left,myimage.top,myimage.left + myimage.width,myimage.top + myimage.Height);
myimage.canvas.FillRect(crect2);
myimage.visible := GetText(MyImage, copy(ws, 4, length(ws)), copy(ws, 2, 1) = 'p');

This part works, and this shows my text in the TImage.

Now, I have another TImage named myimage2, and I want to copy the drawn text into this TImage.

I am having issues with the copying.

I tried the easy way by using: myimage2.picture := myimage.picture.

I have tried to assign the image from myimage to myimage2.

The last thing I have tried is a CopyRect function:

myimage2.Canvas.CopyRect(crect3,myimage.Canvas,crect2);

Where crect2 is the Rect from myimage, while crect3 is the Rect from myimage2.

All 3 of the above possibilities have failed to copy the drawn text to the 2nd TImage.

I am obviously doing something wrong here, and it might be something very small, but I am not seeing the issue.

What am I doing wrong?

Upvotes: 0

Views: 200

Answers (1)

Dennis van Vugt
Dennis van Vugt

Reputation: 21

Solution found!

It helps making sure myimage2.visible is set to true... Then using

myimage2.picture := myimage.picture; 

the drawn text is copied...

Upvotes: 1

Related Questions