Einzelnen Beitrag anzeigen

bernhard_LA

Registriert seit: 8. Jun 2009
Ort: Bayern
1.138 Beiträge
 
Delphi 11 Alexandria
 
#1

Mouse click um einige Pixel versetzt ....

  Alt 19. Apr 2024, 07:46
ich möchte in einer Bitmap ein Pixel mit hoher Genauigkeit über eine GUI auswählen können.
Dafür lade ich das Bitmap in ein Form , rechts und links sind Scrollbars definiert, über die Position der scrollbars berechne ich ein rotes Kreuz und somit das ausgewählte Pixel. Passt.
Meine Funktion um über einen Double-Click auf das Image dann das rote Kreuz zu setzen ist leider (oft) um einige Pixel vesetzt, was ist falsch in meinem Algo die Mouse Click Pixel Werte umzu rechnen ?


Delphi-Quellcode:
type
  /// <summary>
  /// new , new
  /// </summary>
  TSelectPixelfromImageForm = class(TForm)
    pnl_mainview: TPanel;
    scrlbr_bottom: TScrollBar;
    img_zoom: TImage;
    pnl_top: TPanel;
    edt_position: TEdit;
    scrlbr_right_side: TScrollBar;
    procedure FormResize(Sender: TObject);
    procedure img_zoomDblClick(Sender: TObject);
    procedure scrlbr_bottomChange(Sender: TObject);
    procedure scrlbr_right_sideChange(Sender: TObject);
  private
    FBitmap: TBitmap;

    procedure DrawRedCrosslines(center_x, center_y: Integer);
    procedure SetBitMap(const Value: TBitmap);
    { Private declarations }
  public
    { Public declarations }
    property Bitmap: TBitmap read FBitmap write SetBitMap;
  end;

var
  SelectPixelfromImageForm: TSelectPixelfromImageForm;

implementation

{$R *.dfm}

procedure TSelectPixelfromImageForm.FormResize(Sender: TObject);
var
  xsize, ysize: Integer;
  bordermargin: Integer;
  bordermargin2: Integer;
begin
  xsize := pnl_mainview.Width;
  ysize := pnl_mainview.Height;
  bordermargin := 30;
  bordermargin2 := bordermargin * 2;

  img_zoom.Width := xsize - bordermargin2;
  img_zoom.Height := ysize - bordermargin2;
  img_zoom.Left := bordermargin;
  img_zoom.Top := bordermargin;

  scrlbr_bottom.Width := xsize - 2 * bordermargin;
  scrlbr_bottom.Height := bordermargin;
  scrlbr_bottom.Left := bordermargin;
  scrlbr_bottom.Top := ysize - bordermargin;

  scrlbr_right_side.Height := ysize - bordermargin2;
  scrlbr_right_side.Width := bordermargin;
  scrlbr_right_side.Left := xsize - bordermargin;
  scrlbr_right_side.Top := bordermargin;

  pnl_top.Align := alTop;
  pnl_top.Width := xsize;
  pnl_top.Height := bordermargin;
end;

procedure TSelectPixelfromImageForm.DrawRedCrosslines(center_x,
  center_y: Integer);
var
  halfCrossSize: Integer;
begin
  halfCrossSize := Min(img_zoom.Width, img_zoom.Height) div 2;

  img_zoom.Canvas.Pen.Color := clRed;
  img_zoom.Canvas.Pen.Width := 1; // Set the pen width for the cross lines

  // Clear the previous drawing on the canvas
  img_zoom.Picture.Bitmap.Assign(FBitmap);

  img_zoom.Canvas.MoveTo(0, center_y);
  img_zoom.Canvas.LineTo(img_zoom.Width, center_y);
  img_zoom.Canvas.MoveTo(center_x, 0);
  img_zoom.Canvas.LineTo(center_x, img_zoom.Height);

  edt_position.text := 'POS=' + FPointToStr(ToFPoint(center_x, center_y)) +
    ' BMP=' + FPointToStr(ToFPoint(FBitmap.Width, FBitmap.Height)) + ' IMG=' +
    FPointToStr(ToFPoint(img_zoom.Width, img_zoom.Height));

end;

procedure TSelectPixelfromImageForm.img_zoomDblClick(Sender: TObject);
var
  Last: TPoint;
begin
  // Get the screen coordinates of the mouse cursor TImage
  // Pos relative to Timage img_zoom
  Last := img_zoom.ScreenToClient(Mouse.CursorPos);

  Last.X := round((Last.X - img_zoom.Left) / img_zoom.Width *
    img_zoom.Picture.Bitmap.Width);
  Last.Y := round((Last.Y - img_zoom.Top) / img_zoom.Height *
    img_zoom.Picture.Bitmap.Height);

  DrawRedCrosslines(Last.X, Last.Y);

  ///
  /// Update GUI
  ///

  scrlbr_bottom.Position := Last.X;

  scrlbr_right_side.Position := Last.Y;

end;

procedure TSelectPixelfromImageForm.scrlbr_bottomChange(Sender: TObject);
begin
  DrawRedCrosslines(scrlbr_bottom.Position, scrlbr_right_side.Position);
end;

procedure TSelectPixelfromImageForm.scrlbr_right_sideChange(Sender: TObject);
begin
  DrawRedCrosslines(scrlbr_bottom.Position, scrlbr_right_side.Position);
end;

procedure TSelectPixelfromImageForm.SetBitMap(const Value: TBitmap);
begin
  FBitmap := Value;


  ///
  /// set GUI values
  ///
  img_zoom.Picture.Bitmap.Assign(Value);

  scrlbr_bottom.Max := Value.Width - 1;

  scrlbr_right_side.Max := Value.Height - 1;

end;
Miniaturansicht angehängter Grafiken
screenshot-2024-04-19-074441.png  
  Mit Zitat antworten Zitat