procedure RotateBmp(bmp: TBitmap; Center: TPoint; angle: Integer);
var
tmpbmp: TBitmap;
i, j, x, y, px, py: Integer;
cAngle, sAngle: Extended;
p1, p2
![Stick Out Tongue :P :P](https://cdn.jsdelivr.net/joypixels/assets/8.0/png/unicode/64/1f61b.png)
ByteArray;
NextLine:Integer;
pOrigin1,pOrigin2
![Stick Out Tongue :P :P](https://cdn.jsdelivr.net/joypixels/assets/8.0/png/unicode/64/1f61b.png)
Byte;
logI,logJ:Integer;
begin
while angle < 0 do Inc(angle,360);
angle := angle mod 360;
sAngle := sin(- angle * pi / 180);
cAngle := cos(- angle * pi / 180);
tmpbmp := tbitmap.create;
try
tmpbmp.assign(bmp);
tmpbmp.PixelFormat :=pf24bit;
pOrigin1 := tmpbmp.scanline[0];
NextLine := Integer(tmpbmp.scanline[1]) -Integer(pOrigin1);//增量,此处的存储结构为负
//tmpbmp.SaveToFile('log.bmp');
bmp.FreeImage;
bmp.Width :=tmpbmp.Width;
bmp.Height:=tmpbmp.Height;
bmp.PixelFormat :=pf24bit;
bmp.Canvas.FillRect(bmp.Canvas.ClipRect);
pOrigin2 := bmp.scanline[0];
try
Integer(p1) :=Integer(pOrigin1);
for i := 0 to tmpbmp.height - 1 do
begin
py := 2 * (i - center.y) - 1;
for j := 0 to tmpbmp.width - 1 do
begin
px := 2 * (j - center.x) - 1;
x := (round(px * cAngle - py * sAngle) - 1) div 2 + center.x;
y := (round(px * sAngle + py * cAngle) - 1) div 2 + center.y;
if (x>=0)and(x<tmpbmp.width)and(y>=0)and(y<tmpbmp.height) then
begin
Integer(p2) := Integer(pOrigin2) + y *NextLine +x *3;
p2[0] :=p1[0];
p2[1] :=p1[1];
p2[2] :=p1[2];
end;
Integer(p1) :=Integer(p1) +3;
logJ :=j;
end;
Integer(p1) :=Integer(pOrigin1) +NextLine *i;
logI :=i;
end;
except on e:Exception do begin
ShowMessage(IntToStr(logI));
ShowMessage(IntToStr(logJ));
end;
end;
finally
tmpbmp.Destroy;
end;
end;
procedure RotateJpg(var jpg:TJPEGImage;const angle: Integer);
var
bmp :TBitmap;
center:TPoint;
begin
bmp :=TBitmap.Create;
try
jpg.DIBNeeded;
bmp.Width :=jpg.Width;
bmp.Height:=jpg.Height;
bmp.Canvas.StretchDraw(bmp.Canvas.ClipRect,jpg);
center.X :=(bmp.Canvas.ClipRect.Right -bmp.Canvas.ClipRect.Left) div 2;
center.Y :=(bmp.Canvas.ClipRect.Bottom-bmp.Canvas.ClipRect.Top) div 2;
RotateBmp(bmp,center,angle);
jpg.Assign(bmp);
finally
bmp.Destroy;
end;
end;
procedure TForm1.Image1DblClick(Sender: TObject);
var
jpg:TJPEGImage;
begin
jpg :=TJPEGImage.Create;
try
jpg.LoadFromFile('test.jpg');
//旋转30度
RotateJpg(jpg,30);
jpg.JPEGNeeded;
jpg.SaveToFile('log1.jpg');
Image1.Picture.Bitmap.FreeImage;
jpg.DIBNeeded;
Image1.Canvas.StretchDraw(Image1.ClientRect,jpg);
finally
jpg.Destroy;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Image1.Picture.LoadFromFile('test.jpg');
end;