Bob
Bob

Reputation: 177

Delete rectangle on TBitmap in Delphi XE

  bmp := TBitmap.Create;
  bmp.SetSize(Screen.Width,Screen.Height);

  bmp.canvas.Brush.Style := bsClear; 
  bmp.Canvas.Pen.Color := clLime;
  bmp.Canvas.Rectangle(startPos.X,startPos.Y,stopPos.X,stopPos.Y); <-draw rectangle

Is there a simple way to delete the Old rectangle without refreshing the entire image?

I want to select a region of this image, like they do in grafic programs

Upvotes: 2

Views: 1934

Answers (4)

ffert2907
ffert2907

Reputation: 370

If you are using Delphi XE with FireMonkey, you don't need to care about it, just use a TSelection component that allows you to show a region, modify and get information from the region...

Upvotes: 1

MohsenB
MohsenB

Reputation: 1921

Simply way is work with TShape to select, and when done selected hide it and draw shape by
coordinate of this TShape .

Upvotes: 0

NGLN
NGLN

Reputation: 43669

I assume you want to draw a selection rectangle which is typically bound by mouse movement, thus deleting the previously drawn rectangle and drawing a new one at the current mouse position. This can be done by drawing with the pen in XOR mode:

function NegativeColor(AColor: TColor): TColor;
begin
  Result := RGB(255 - GetRValue(AColor), 255 - GetGValue(AColor),
    255 - GetBValue(AColor));
end;

procedure TForm1.Button14Click(Sender: TObject);
var
  Bmp: TBitmap;
begin
  Bmp := TBitmap.Create;
  try
    Bmp.SetSize(100, 100);
    Bmp.Canvas.Brush.Style := bsClear;
    Bmp.Canvas.Pen.Color := NegativeColor(clLime);
    Bmp.Canvas.Pen.Mode := pmXor;
    Bmp.Canvas.Rectangle(10, 10, 50, 50);
    Canvas.Draw(0, 0, Bmp);
    Bmp.Canvas.Rectangle(10, 10, 50, 50); // "Erase" previous rectangle
    Bmp.Canvas.Rectangle(10, 10, 90, 90); // Draw new rectangle
    Canvas.Draw(0, 100, Bmp);
  finally
    Bmp.Free;
  end;
end;

Upvotes: 0

David Heffernan
David Heffernan

Reputation: 613432

You cannot delete things from a raster image. Each pixel must have a value. All you can do is draw something else over what is already there.

So if you want to restore what was there before, you must remember what it was, and draw it again.

Drawing programs maintain layers, and merge those layers into a single image for rendering. You could do that too, but you have to do it yourself with multiple bitmaps, one per layer.

If you want to draw a selection rectangle you don't need to draw on the underlying bitmap. When you need to paint you paint the bitmap to the screen and then paint the rectangle on top. That way you don't let the selection rectangle spoil the actual image.

Upvotes: 0

Related Questions