画旋转位图还差一步就成功了,大侠们请进。。。。。。。。。。。。

dongliang110 2005-12-26 04:46:21
小弟最近在做一个画图的程序,现在做到任意旋转位图时遇到了一个问题,不知道怎么解决。

是这样的,我原来的图片是个正方形的图片,当我将图片旋转后画到画布上时,显示的还是正方形,但是图片的位置是倾斜的,
所以有一部分是灰色的部分。图片超出正方形的区域没有画上去,怎样画时使边框正好紧贴着我的图片,代码如下:
procedure SmoothRotate(Bmp,Dst:TBitmap;cx,cy:Integer;Angle:Extended);
type
TFColor = record b,g,r:Byte end;

var
Top,Bottom,Left,Right,eww,nsw,fx,fy,wx,wy: Extended;
cAngle,sAngle: Double;
xDiff,yDiff,ifx,ify,px,py,ix,iy,x,y: Integer;
nw,ne,sw,se, Tmp:^TFColor;
begin
if not assigned(bmp) or not assigned(dst) then exit;
bmp.pixelformat := pf24bit;//;
dst.pixelformat := pf24bit; // only work with 24-bit bitmap
// if u can sure they r all 24-bit, u can
// delete these 2 lines
Angle:=-Angle*Pi/180;
sAngle:=Sin(Angle);
cAngle:=Cos(Angle);
xDiff:=(Dst.Width-Bmp.Width)div 2;
yDiff:=(Dst.Height-Bmp.Height)div 2;
for y:=0 to Dst.Height-1 do
begin
tmp := dst.scanline[y];
py:=2*(y-cy)+1;
for x:=0 to Dst.Width-1 do
begin
px:=2*(x-cx)+1;
fx:=(((px*cAngle-py*sAngle)-1)/ 2+cx)-xDiff;
fy:=(((px*sAngle+py*cAngle)-1)/ 2+cy)-yDiff;
ifx:=Round(fx);
ify:=Round(fy);

if(ifx > -1)and(ifx < Bmp.Width)and(ify > -1)and(ify < Bmp.Height) then
begin
eww:=fx-ifx;
nsw:=fy-ify;
iy:=TrimInt(ify+1,0,Bmp.Height-1);
ix:=TrimInt(ifx+1,0,Bmp.Width-1);
nw := pointer(integer(bmp.scanline[ify])+ifx*3);
ne := pointer(integer(bmp.scanline[ify])+ix*3);
sw := pointer(integer(bmp.scanline[iy])+ifx*3);
se := pointer(integer(bmp.scanline[iy])+ix*3);

Top:=nw^.b+eww*(ne^.b-nw^.b);
Bottom:=sw^.b+eww*(se^.b-sw.b);
Tmp^.b:=(Round(Top+nsw*(Bottom-Top))) and $ff;

Top:=nw^.g+eww*(ne^.g-nw^.g);
Bottom:=sw^.g+eww*(se^.g-sw^.g);
Tmp^.g:=(Round(Top+nsw*(Bottom-Top))) and $ff;

Top:=nw^.r+eww*(ne^.r-nw^.r);
Bottom:=sw^.r+eww*(se^.r-sw^.r);
Tmp^.r:=(Round(Top+nsw*(Bottom-Top))) and $ff;
end;
tmp := pointer(integer(tmp)+3);
end;
end;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);

begin

dd.Width :=image1.Picture.Bitmap.Width;
dd.Height :=image1.Picture.Bitmap.Height;
SmoothRotate(image1.Picture.Bitmap,dd,(image1.Picture.Bitmap.Width div 2),(image1.Picture.Bitmap.Height div 2),round(bsSkinSpinEdit1.Value));
image2.Picture.Bitmap.Assign(dd);

dd.Transparent := true;
Repaint;

end;
procedure TForm1.FormPaint(Sender: TObject);

begin
dd.Transparent := true;
form1.Canvas.StretchDraw(dd.Canvas.ClipRect,dd); // dd.Canvas.ClipRect
end;
...全文
50 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi GAME,图形处理/多媒体
社区管理员
  • GAME,图形处理/多媒体社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧