ImageEn, unit imageenview

TImageEnView.OnDrawCanvas

TImageEnView.OnDrawCanvas


Declaration

property OnDrawCanvas: TIEOnDrawCanvas;


Description

Occurs whenever the component canvas is updated.


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\ImageEditing\SelectAndCrop\ImageEn_Crop.dpr


Examples

// Show cursor as horizontal and vertical lines
// Muast call Invalidate() in ImageEnView1 MouseMove event
// Works best with ImageEnView1.Cursor := crNone;
procedure TForm1.ImageEnView1DrawCanvas(Sender: TObject; ACanvas: TCanvas; ARect: TRect);
var
  P: TPoint;
begin
  GetCursorPos( P );
  P := ImageEnView1.ScreenToClient( P );

  ACanvas.Pen.Color := clYellow;
  ACanvas.MoveTo( P.X, 0 );
  ACanvas.LineTo( P.X, ImageEnView1.ClientHeight - 1 );
  ACanvas.MoveTo( 0, P.Y );
  ACanvas.LineTo( ImageEnView1.ClientWidth - 1, P.Y );
end;


// Show a 100x100px box in the center of the image (does not modify the actual image, only the view)
procedure TForm1.ImageEnView1DrawCanvas(Sender: TObject; ACanvas: TCanvas; ARect: TRect);
const
  Box_Width  = 100;
  Box_Height = 100;
var
  boxL, boxT, boxR, boxB: Integer;
begin
  if ImageEnView1.IsEmpty() or
     ( ImageEnView1.IEBitmap.Width < Box_Width ) or
     ( ImageEnView1.IEBitmap.Height < Box_Height ) or
     not chkDrawBox.Checked then
    exit;

  boxL := ImageEnView1.XBmp2Scr( ImageEnView1.IEBitmap.Width  div 2 - Box_Width  div 2 );
  boxT := ImageEnView1.YBmp2Scr( ImageEnView1.IEBitmap.Height div 2 - Box_Height div 2 );
  boxR := ImageEnView1.XBmp2Scr( ImageEnView1.IEBitmap.Width  div 2 + Box_Width  div 2 );
  boxB := ImageEnView1.YBmp2Scr( ImageEnView1.IEBitmap.Height div 2 + Box_Height div 2 );

  ACanvas.Pen.Color := clRed;
  ACanvas.Brush.Style := bsClear;
  ACanvas.Rectangle( boxL, boxT, boxR, boxB );
end;


// Draw "thirds" within the selection box
procedure TForm1.ImageEnView1DrawCanvas(Sender: TObject; ACanvas: TCanvas; ARect: TRect);
var
  selRect: TIERectangle;
  //
  procedure _DrawLine(x1, y1, x2, y2: Integer); // bmp values
  begin
    ACanvas.Pen.Color := clGray;
    ACanvas.MoveTo( ImageEnView1.XBmp2Scr( x1 ), ImageEnView1.YBmp2Scr( y1 ) );
    ACanvas.LineTo( ImageEnView1.XBmp2Scr( x2 ), ImageEnView1.YBmp2Scr( y2 ) );
  end;
  //
begin
  if not ( ImageEnView1.Selected and chkDrawBox.Checked ) then
    exit;

  selRect := ImageEnView1.SelectedRect;

  // Horz Thirds
  _DrawLine( selRect.x, selRect.y + MulDiv( selRect.height, 1, 3 ),
             selRect.x + selRect.Width, selRect.y + MulDiv( selRect.height, 1, 3 ) );
  _DrawLine( selRect.x, selRect.y + MulDiv( selRect.height, 2, 3 ),
             selRect.x + selRect.Width, selRect.y + MulDiv( selRect.height, 2, 3 ) );

  // Vert Thirds
  _DrawLine( selRect.x + MulDiv( selRect.Width, 1, 3 ), selRect.y,
             selRect.x + MulDiv( selRect.Width, 1, 3 ), selRect.y + selRect.height );
  _DrawLine( selRect.x + MulDiv( selRect.Width, 2, 3 ), selRect.y,
             selRect.x + MulDiv( selRect.Width, 2, 3 ), selRect.y + selRect.height );
end;


// Show image split into three parts horizontally
procedure TfrmMain.ImageEnView1DrawCanvas(Sender: TObject; ACanvas: TCanvas; ARect: TRect);
  //
  procedure DrawBox(Text: string; bmpL, bmpT, bmpR, bmpB: Integer; Color: TColor);
  begin
    ACanvas.Pen.Style := psSolid;
    ACanvas.Pen.Color := Color;
    ACanvas.Brush.Style := bsClear;

    // Convert bitmap values to screen values and draw rect to canvas
    ACanvas.Rectangle( ImageEnView1.XBmp2Scr( bmpL ),
                       ImageEnView1.YBmp2Scr( bmpT ),
                       ImageEnView1.XBmp2Scr( bmpR ),
                       ImageEnView1.YBmp2Scr( bmpB ));

    ACanvas.Font.Color := Color;
    ACanvas.Font.Style := [fsBold];
    ACanvas.TextOut( ImageEnView1.XBmp2Scr( bmpL ) + 5, 5, Text )
  end;
  //
var
  bw, bh: Integer;
begin
  if ImageEnView1.IsEmpty() then
    exit;

  bw := ImageEnView1.IEBitmap.Width;
  bh := ImageEnView1.IEBitmap.Height;

  // Image 1
  DrawBox( 'Image 1', 0, 0, bw div 3, bh - 1, clRed );

  // Image 2
  DrawBox( 'Image 2', bw div 3, 0, bw - bw div 3, bh - 1, clYellow );

  // Image 3
  DrawBox( 'Image 3', bw - bw div 3 div 3, 0, bw - 1, bh - 1, clBlue );
end;