Some commonly used picture viewing software have a function that can enlarge local images. This example was developed to simulate this function.
Add two TImage components to the form, with one of the TImage components set to Image1, which acts as the carrier for the original image display. Another TImage component has the Name property set to Image2, which can display the enlarged image. The form after adding components is shown in Figure 1.
Figure 1 The form after adding components
The core of this example is the StretchBlt function, which uses the StretchBlt function to achieve local image amplification. The response code is as follows:
PRocedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
Begin
StretchBlt(Image2.Canvas.Handle,0,0,Image2.Width,Image2.Height,
Image1.Canvas.Handle, X-20,Y-20,40,40,SRCCOPY);
Image2.Refresh;
Screen.Cursors[1]:=LoadCursorFromFile('MAGNIFY.CUR');
Self.Cursor:=1;
end;
The program will first call the StretchBlt function, use the current position of the mouse as the center point, select the local image on the Image1 component with a side length of 40, and enlarge the local image on the Image2 component. Then, the display of the Image2 component is refreshed by calling the Refresh method of the Image2 component. Finally, set the mouse pointer to the new shape.
The program code is as follows:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
Image1: TImage;
Image2: TImage;
procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);
Private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Implementation
{$R *.dfm}
procedure TForm1.Image1MouseMove(Sender:TObject;Shift:TShiftState;X,Y: Integer);
Begin
StretchBlt(Image2.Canvas.Handle,0,0,Image2.Width,Image2.Height,Image1.Canvas.Handle,X-20,Y-20,40,40,SRCCOPY);
Image2.Refresh;
Screen.Cursors[1]:=LoadCursorFromFile('MAGNIFY.CUR');
Self.Cursor:=1;
end;
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);
Begin
Screen.Cursors[1]:=crDefault;
Self.Cursor:=1;
end;
end.
Save the file, then press F9 to run the program. The program run results are shown in Figure 2.
Figure 2 Program operation results
Zoom in images is a necessary feature of an excellent picture viewing software. This example provides a very simple and easy method, not only with small codes, but also with high execution efficiency.