ImageEn, unit imageenview

TImageEnView.XScr2Bmp

TImageEnView.XScr2Bmp


Declaration

function XScr2Bmp(x: integer; CurrentLayer: Boolean = False): integer;


Description

The XScr2Bmp and YScr2Bmp methods convert a window coordinate to the corresponding bitmap coordinate (considering Zoom and ViewX, ViewY status).

x is a window coordinate.
If CurrentLayer is true, then the result will be a coordinate relative to the current layer. If false, it will be a coordinate relative to the background layer (layer 0).

Note:
The result may be negative or greater than the bitmap width if x is beyond the displayed image's boundaries
To convert a coordinate of a non-current layer, use TImageEnView.Layers[].ConvXScr2Bmp
The screen value is relative to the control, i.e. as if the TImageEnView is positioned at 0,0 on the active screen.


Demo

Demo  Demos\Other\PixelView\PixelView.dpr


Examples

// X and Y are MOUSE coordinates
bx := ImageEnView1.XScr2Bmp( X, False );
by := ImageEnView1.YScr2Bmp( Y, False );
// ...now bx and by are Bitmap coordinates (of ImageEnView1.IEBitmap)

// But they may be out of the bitmap range, so check them...
if ( bx >= 0 ) and ( bx < ImageEnView1.IEBitmap.Width ) and ( by >= 0 ) and ( by < ImageEnView1.IEBitmap.Height ) then
begin
  ImageEnView1.IEBitmap.Pixels[ bx, by ] := TColor2TRGB( clRed );
  ImageEnView1.Update();
end;


// Allow images from a TImageEnMView to be dropped onto a TImageEnView as layers

procedure TMainForm.ImageEnMView1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
  if ssLeft in Shift then
  begin
    (Sender as TImageEnMView).MouseInteract := [];
    (Sender as TImageEnMView).IEBeginDrag(False, -1);
  end;
end;

procedure TMainForm.ImageEnMView1EndDrag(Sender, Target: TObject; X, Y: Integer);
begin
  (Sender as TImageEnMView).IEEndDrag();
  (Sender as TImageEnMView).MouseInteract := [mmiSelect];
end;

procedure TMainForm.ImageEnView1DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);
begin
  if (Source is TImageEnMView) then
    Accept := True;
end;

procedure TMainForm.ImageEnView1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
  bmp: TIEBitmap;
  idx: Integer;
begin
  idx := TImageEnMView(Source).SelectedImage;
  if idx >= 0 then
  begin
    bmp := ImageEnMView1.GetTIEBitmap( idx );
    ImageEnView1.LayersAdd( bmp );
    ImageEnView1.CurrentLayer.PosX := ImageEnView1.XScr2Bmp( X );
    ImageEnView1.CurrentLayer.PosY := ImageEnView1.YScr2Bmp( Y );
    TImageEnMView(Source).ReleaseBitmap(idx, False);
    ImageEnView1.Update();
  end;
end;


// At TTimer interval, paint the pixel at the cursor position white
// Note: Example only. MouseMove event would be better for this
procedure TForm1.Timer1Timer(Sender: TObject);
var
  Pos: TPoint;
begin
  // Get pos of cursor on screen
  GetCursorPos(Pos);

  // Convert it to position of the TImageEnView
  Pos := ImageEnView1.ScreenToClient(Pos);

  // Convert it to a bitmap value
  Pos.X := ImageEnView1.XScr2Bmp(Pos.X);
  Pos.Y := ImageEnView1.YScr2Bmp(Pos.Y);

  // Paint the pixel
  if ( Pos.X >= 0 ) and ( Pos.X < ImageEnView1.IEBitmap.Width ) and
     ( Pos.Y >= 0 ) and ( Pos.Y < ImageEnView1.IEBitmap.Height ) then
  begin
    ImageEnView1.IEBitmap.Pixels[ Pos.X, Pos.Y ] := TColor2TRGB( clWhite );
    ImageEnView1.Update();
  end;
end;


// Insert text when user clicks on control
procedure Tfmain.ImageEnView1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  mx, my: Integer;
begin
  mx := ImageEnView1.XScr2Bmp( x );
  my := ImageEnView1.XScr2Bmp( y );
  ImageEnView1.LayersAdd( ielkText, mx, my );
  TIETextLayer( ImageEnView1.CurrentLayer ).Text := format( 'Clicked at image pos: %d,%d', [ mx, my ]);
  TIETextLayer( ImageEnView1.CurrentLayer ).SizeToText();
  ImageEnView1.Update();
end;


See Also

YScr2Bmp
XBmp2Scr
YBmp2Scr
VisibleBitmapRect