Hi,

I have a Timage on a panel and I want to draw a cross on the image as I move my mouse on the image. I am drawing the cross using the canvas of the image. The drawing of the mouse works fine but the problem I am encountring is that when I move the mouse again the previous cross that was drawn is still there. I have tried to refresh the Image and the canvas but that hasn't helped. Any suggestions...

Thanks in advance.

Dani AI

Generated

Brief summary and a slightly more robust pattern.

and were right — drawing directly onto the image’s canvas leaves the old pixels in place unless you explicitly clear or restore them. The simplest fix is to clear the drawing area (FillRect) before drawing the new cross, but a more reliable approach is to keep an untouched copy of the image and restore it on each mouse move, then draw the transient cross on top. That avoids leaving artifacts after repaints and works if the control is invalidated for any reason.

Example pattern (C++Builder 6):

// form header:
TBitmap *origBmp;

// form OnCreate:
origBmp = new TBitmap();
origBmp->Assign(Image1->Picture->Bitmap);

// form OnDestroy:
delete origBmp;

// Image OnMouseMove:
void __fastcall TForm1::Image1MouseMove(TObject *Sender, TShiftState Shift, int X, int Y)
{
    // restore original image, draw cross, repaint
    Image1->Picture->Bitmap->Assign(origBmp);
    TCanvas *c = Image1->Picture->Bitmap->Canvas;
    c->Pen->Color = clRed;
    c->MoveTo(0, Y); c->LineTo(Image1->Width, Y);
    c->MoveTo(X, 0); c->LineTo(X, Image1->Height);
    Image1->Repaint();
}

Practical tips and alternatives:

  • To reduce flicker and work faster, restore/draw only a small rect around the old and new cross instead of the whole bitmap. Use TCanvas::CopyRect to copy that region from origBmp.
  • If you only need an ephemeral overlay, consider placing a transparent TPaintBox above the TImage and draw the cross in the PaintBox’s OnPaint (store mouse coords and call Invalidate). That keeps the source image untouched.
  • Avoid relying on XOR/R2_NOTXORPEN for erase-on-draw; it’s unreliable with modern color depths.
  • Remember to manage origBmp lifetime and match pixel formats to avoid color/alpha surprises.

This gives a clean, flicker-minimized cross that won’t accumulate draws or disappear on repaint — a practical step beyond the FillRect suggestion that confirmed worked for .

Recommended Answers

All 4 Replies

TCanvas is something like HDC (wrapper on it).
When you begin to draw cross or something you want, you may fill full client area of TImage with background color.
Use method FillRect of TCanvas class.

what libraries, compiler, and operating system are you using to do all that drawing? If you want the previous cross to disappear when you move the mouse you will have to redraw the cross in the same color as the canvas background. It doesn't disappear on its own.

[edit]Or, more likey what ^^^ he said [/edit]

Don't forget to make call of Update method of TImage class after all.

Notes:
BCB 6 is Builder C++ Version 6.
TCanvas is builder class, like CDC in MFC.

TCanvas is something like HDC (wrapper on it).
When you begin to draw cross or something you want, you may fill full client area of TImage with background color.
Use method FillRect of TCanvas class.

Thanks,

This worked fine. I should have tried this earlier but just thought that there was a another way. What a waste of time. This was such an easy fix.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.