Realization of Image Magnifier with Delphi

Source: Internet
Author: User
Tags response code

In some commonly used look at the picture software with a can enlarge the local image function, this example is to imitate this function development.

Add two timage components to the form, where the Name property of one Timage component is set to Image1, which acts as the carrier for the original picture display. The Name property of another Timage component is set to Image2, which can display the magnified image. The form after you add the component is shown in Figure 1.

Figure 1 form after adding a component

The core of this example is the StretchBlt function, using 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 first calls the StretchBlt function, takes the mouse's current position as the center point, edges 40 to select the local image on the Image1 component, and enlarges the local image to the Image2 component. The display of the Image2 component is then 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, and then press F9 to run the program, and the results of the program run as shown in Figure 2.

Figure 2 Program Run results

Magnified image is a good look at the graphics software necessary functions, this example provides a very easy way, not only the number of code is low, and the implementation of high efficiency.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.