ImageEn, unit imageenview

TImageEnView.OnDrawBackBuffer

TImageEnView.OnDrawBackBuffer


Declaration

property OnDrawBackBuffer: TNotifyEvent;


Description

OnDrawBackBuffer provides access to the back buffer where ImageEn draws the image (and layers) just before the paint event.
Drawing to the back buffer will appear over the image and layers, but beneath screen controls such as selection rectangles.
You can draw on the BackBuffer by handling the OnDrawBackBuffer event to paint onto the Backbuffer canvas.
BackBuffer is updated whenever Update is called (not when only invalidating the control).


ImageEnView Paint Events

The main paint events of TImageEnView are OnDrawBackBuffer which occurs when the back buffer is painted and OnDrawCanvas which occurs every time the canvas is painted. Generally OnDrawBackBuffer is used for better performance, because the back buffer painting only occurs after the content is updated. If you need constant custom drawing (e.g. to draw a marker at the current cursor position) then use OnDrawCanvas.

In layer applications, OnBeforeDrawLayer and OnDrawLayer will occur while painting each individual layer. Use these methods if you need easy custom drawing upon each individual layer (e.g. a layer title). Generally OnBeforeDrawLayer is best, unless you need to paint to the whole image (i.e. outside the area of the layer).

Other drawing events:
OnDrawBackground occurs when the background (area behind the image is painted) is painted to allow custom background styling
OnDrawLayerBox occurs when the box around a layer is painted to allow custom styling of the box
OnDrawLayerGrip occurs when the grips of the selected layer are drawn to allow custom grip styling
OnTransitionPaint occurs while transitioning from one image to the next
OnPaint notifies whenever there is a paint event. It is not used for custom drawing
OnDrawPolygon notifies whenever a point of a polygonal selection is drawn


Demo

Demo  Demos\Other\CellsAndGrid\CellsAndGrid.dpr


Examples

Procedure Form1OnDrawBackBuffer(Sender: TObject);
Begin
  // Draw a red line on the back buffer
  With ImageEnView1.BackBuffer.Canvas do
  begin
    Pen.Color := clRed;
    MoveTo( 0, 0 );
    LineTo( 100, 100 );
  End;
End;


// Draw a box around all selected layers
// Note: To prevent the default layer selection box being drawn, create an empty OnDrawLayerBox event
procedure Tfmain.ImageEnView1DrawBackBuffer(Sender: TObject);
var
  cab: TRect;
  i, minX, minY, maxX, maxY : Integer;
begin
  minX := MAXINT;
  minY := MAXINT;
  maxX := 0;
  maxY := 0;

  for I := 0 to ImageEnView1.LayersCount - 1 do
    if ImageEnView1.Layers[ i ].Selected then
    begin
      cab := ImageEnView1.Layers[i].ClientAreaBox;
      minX := imin( minX, cab.Left );
      minY := imin( minY, cab.Top );
      maxX := imax( maxX, cab.Right );
      maxY := imax( maxY, cab.Bottom );
    end;

  // Draw green rect
  if minX < MAXINT then
    with ImageEnView1.BackBuffer.Canvas do
    begin
      Pen.Style := psSolid;
      Pen.Width := 2;
      Pen.mode := pmCopy;
      Pen.Color := clGreen;
      Brush.Style := bsClear;
      Rectangle( minX - 1, minY - 1, maxX + 1, maxY + 1 );
    end;
end;


// Use a trackbar to darken the image (call ImageEnView1.Update from the Trackbar's OnChange event)
procedure DarkenBitmap(Bitmap: TBitmap; Brightness: Integer);
type
  TRGBTripleArray = ARRAY[Word] of TRGBTriple;
  pRGBTripleArray = ^TRGBTripleArray;
var
  x, y: Integer;
  Row: PRGBTripleArray;
  R, G, B: Byte;
begin
  if ( Bitmap.PixelFormat <> pf24bit ) or ( Brightness < 0 ) or ( Brightness > 100 ) then
    raise Exception.create( 'Cannot darken' );

  for y := 0 to Bitmap.Height - 1 do
  begin
    Row := Bitmap.ScanLine[y];
    for x := 0 to Bitmap.Width - 1 do
    begin
      // Get RGB components
      R := Row[x].rgbtRed;
      G := Row[x].rgbtGreen;
      B := Row[x].rgbtBlue;

      // Apply brightness scaling
      Row[x].rgbtRed   := Round( R * Brightness / 100.0 );
      Row[x].rgbtGreen := Round( G * Brightness / 100.0 );
      Row[x].rgbtBlue  := Round( B * Brightness / 100.0 );
    end;
  end;
end;

procedure TForm1.ImageEnView1DrawBackBuffer(Sender: TObject);
begin
  DarkenBitmap( ImageEnView1.BackBuffer, TrackBar1.Position );  // 0=Black, 100=Full Brightness
end;