This article uses examples to describe Delphi's solution for realizing image and text rotation special effects. The controls used in this program are mainly Panel control, Image control, Edit control, Label control and Button control. The key to this program is to use Delphi's bmp_rotate() function to realize the function of rotating images. And cleverly call relevant Windows API functions to achieve text rotation effects.
The complete example code is as follows:
unit Unit1;interfaceusesWindows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,math,StdCtrls, ExtCtrls;typeTForm1 = class(TForm)Edit1: TEdit;Image1: TImage;bmprotate: TButton;Label1: TLabel;Panel1: TPanel ;Image2: TImage;Panel2: TPanel;Image3: TImage;Edit2: TEdit;RotatedText: TButton;Label2: TLabel;procedure bmprotateClick(Sender: TObject);procedure RotatedTextClick(Sender: TObject);procedure FormCreate(Sender: TObject);private{ Private declarations }public{ Public declarations }procedure bmp_rotate(src,dst:Tbitmap;angle:extended);Procedure DrawRotatedText(TheCanvas : TCanvas; TheAngle : Integer;TheText : String);end;varForm1: TForm1;implementation{$R *.DFM}procedure TForm1.bmp_rotate(src,dst:Tbitmap;angle:extended);//Image rotation varc1x,c1y,c2x,c2y:integer;p1x,p1y,p2x,p2y:integer;radius,n:integer;alpha:extended;c0 ,c1,c2,c3:tcolor;beginangle := (angle / 180) * pi; //Convert the content in the text box to the angle c1x := src.Width div 2;c1y := src.Height div 2;c2x := dst.Width div 2;c2y := dst.Height div 2;if c2x < c2y thenn := c2yelsen := c2x;dec (n,1); //make n The value is reduced by 1, n:=n-1for p2x := 0 to n do beginfor p2y := 0 to n do beginif p2x = 0 thenalpha:= pi/2elsealpha := arctan2(p2y,p2x);radius := round(sqrt ((p2x*p2x)+(p2y*p2y)));//Set the radius during rotation p1x := round(radius * cos(angle+alpha)); //Set the abscissa of the center of the circle during rotation p1y := round(radius * sin(angle+alpha)); //Set the ordinate of the center of the circle during rotation c0 := src.Canvas.pixels [c1x+p1x,c1y+p1y];//Replace the pixel c1 at the relevant coordinates of the image:= src.Canvas.pixels[c1x-p1x,c1y-p1y];c2 := src.Canvas.pixels[c1x+p1y,c1y-p1x];c3 := src.Canvas.pixels[c1x-p1y,c1y+p1x];dst.Canvas.pixels[c2x+p2x,c2y+p2y]:=c0; //Replace pixels at different positions dst.Canvas.pixels[c2x-p2x,c2y-p2y]:=c1;dst.Canvas.pixels[c2x+p2y,c2y-p2x]:=c2;dst.Canvas.pixels[ c2x-p2y,c2y+p2x]:=c3;end;application.processmessages //Respond to other message requestsend;end;procedure TForm1.bmprotateClick(Sender: TObject);VarRAngle : Extended;beginRAngle := StrToFloat(Edit1.Text); //Set the rotation angle bmp_rotate(Image1.Picture.bitmap,Image2 .Picture.bitmap, RAngle);//Run the rotation function and rotate the graphicsend;Procedure TForm1.DrawRotatedText(TheCanvas : TCanvas; TheAngle : Integer;TheText : String);varlf : TLogFont;tf : TFont;beginImage3.Canvas.refresh;with TheCanvas do beginFont.Name:='Arial';Font.Size:=18; Brush.Style:= bsClear;tf:= TFont.Create;trytf.Assign(Font);GetObject(tf.Handle, Sizeof(lf), @lf); //Assign the current instance handle to the tf Font object lf.lfEscapement:=TheAngle*10;lf.lfOrientation := TheAngle * 10; //Make the abscissa line and the horizontal line at the input form a specified angle tf.Handle := CreateFontIndirect(lf); //Create a Font specific object Font.Assign(tf); //Use this object to the program finallytf.Free; //Release the object end;TextOut(Image3.left, Image3.top+20, TheText); // Draw text to the specified areaend;end;procedure TForm1.RotatedTextClick(Sender: TObject);VarTheAngle : Integer;beginTheAngle := StrToInt(Edit2.Text); //Convert the text in the text box to angle DrawRotatedText(Image3.Canvas, TheAngle, 'Delphi Graphics Studio'); //Call the rotated text function end; procedure TForm1.FormCreate(Sender: TObject) ;vartf : TFont;theText:string;begintheText:='Delphi Graphics Studio'; //Set text with Image3.Canvas dobeginFont.Name := 'Arial'; //Set the font Font.Size := 18; //Set the font size Brush.Style := bsClear; //Set the brush style tf:= TFont.Create; // Create a Font specific object TextOut(Image3.left, Image3.top+20, TheText); //Draw text to the specified areaend;end;end.