Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

working with image

38 views
Skip to first unread message

Sam

unread,
Sep 23, 2004, 10:31:36 PM9/23/04
to
Hello,

I would like to do some classic things with an image such as have the cursor
become a zoom and then zoom into the portion of the image the cursor is
over. Or be able to drag the image around so as to view areas of the image
that are outside the viewable container (such as an image component inside a
panel component).

Can someone please point me in the right direction?

Thanks
Sam


valentin tihomirov

unread,
Sep 24, 2004, 5:31:47 AM9/24/04
to
Look at TBitmap. I beleive you can directly access the pixels, stretch and
viewport the picture (possibly in conjunction with TImage) but I'm not sure.


Peter Below (TeamB)

unread,
Sep 24, 2004, 7:32:57 AM9/24/04
to
In article <415385f4$1...@newsgroups.borland.com>, Sam wrote:
> I would like to do some classic things with an image such as have the cursor
> become a zoom and then zoom into the portion of the image the cursor is
> over. Or be able to drag the image around so as to view areas of the image
> that are outside the viewable container (such as an image component inside a
> panel component).
>

Here are a few bits and pieces that show some of the principles involved.
Note that you do not want to draw something like a selection rectangle on an
image.canvas. If you do that it becomes part of the image and the image redraws
on every change, which causes flicker. Instead draw on the canvas of the images
parent. You can create a TControlCanvas instance to get access to the parent
controls Canvas if it does not offer a Canvas property directly.
In fact you may think about using a TPaintbox inside a TScrollbox as display
surface, not a TImage.

Dragging and scrolling an image inside a scrollbox

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, JPEG, StdCtrls;

type
TForm1 = class(TForm)
ScrollBox1: TScrollBox;
Image1: TImage;
Label1: TLabel;
procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
private
{ Private declarations }
FLastDown: TPoint;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
GetCursorPos( FLastDown );
end;

procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
FLastDown := Point(-1,-1);
end;

procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var
pt: TPoint;
begin
If (ssLeft In Shift) and (FLastDown.X >= 0) Then Begin
GetCursorPos( pt );
Scrollbox1.VertScrollBar.Position :=
Scrollbox1.VertScrollBar.Position + FLastDown.Y - pt.Y;
Scrollbox1.HorzScrollBar.POsition :=
Scrollbox1.HorzScrollBar.Position + FLastDown.X - pt.X;
FLastDown:= pt;
label1.caption := format( '%d:%d',[pt.x,pt.y] );
End;
end;

end.

Drawing a selection rectangle on the form:

A good way to draw a rectangle that needs to be erased again is the Windows API
function DrawFocusRect, by the way. It draws using an XOR pen, if you call it
twice with the same rectangle it is erased on screen. Using two private fields
on your form declaration:

private
{ Private declarations }
fDragging: Boolean;
fRect: TRect;

it is fairly easy to implement a selection rectangle the user can drag on the
form, using handlers for the forms OnMouse... events:

{NormalizeRect contributed by Gordon Whittam }
function NormalizeRect(aRect: TRect): TRect;
var
i: integer;
begin
Result:= aRect;
with Result do
begin
if Right < Left then
begin
i:= Right;
Right:= Left;
Left:= i;
end;
if Bottom < Top then
begin
i:= Top;
Top:= Bottom;
Bottom:= i;
end;
end;
end;

procedure TForm2.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
If mbLeft = Button Then Begin
fDragging := True;
SetRect( fRect, X, Y, X, Y );
Canvas.DrawFocusrect( fRect );
end;
end;

procedure TForm2.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
If fDragging Then Begin
Canvas.DrawFocusrect( NormalizeRect(fRect));
fRect.Right := X;
fRect.Bottom := Y;
Canvas.DrawFocusrect( NormalizeRect(fRect));
End;
end;

procedure TForm2.FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
If fDragging Then Begin
Canvas.DrawFocusrect( NormalizeRect(fRect));
fDragging := False;
... here would go the code to mark all the controls in fRect as
selected.
End;
end;


--
Peter Below (TeamB)
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be


Sam

unread,
Sep 24, 2004, 5:01:59 PM9/24/04
to
Thanks Peter,

I was able to just cut & paste. Works great!

0 new messages