Delphi旋转位图


本文整理自网络,侵删。

 验证码识别之旋转位图

1. 函数

//   Bitmaps   must   be   24   bit   pixel   format.
//   Angle   is   in   degrees.
function   RotateBitmap(Bitmap:   TBitmap;   Angle:   Double;   Color:   TColor):   TBitmap;
const
    MaxPixelCount   =   32768;
type
    PRGBTripleArray   =   ^TRGBTripleArray;
    TRGBTripleArray   =   array[0..MaxPixelCount]   of   TRGBTriple;
var
    CosTheta:   Extended;
    SinTheta:   Extended;
    xSrc,   ySrc:   Integer;
    xDst,   yDst:   Integer;
    xODst,   yODst:   Integer;
    xOSrc,   yOSrc:   Integer;
    xPrime,   yPrime:   Integer;
    srcRow,   dstRow:   PRGBTripleArray;
begin
    Result   :=   TBitmap.Create;
    SinCos(Angle   *   Pi   /   180,   SinTheta,   CosTheta);
    if   (SinTheta   *   CosTheta)   <   0   then
    begin
        Result.Width   :=   Round(Abs(Bitmap.Width   *   CosTheta   -   Bitmap.Height   *   SinTheta));
        Result.Height   :=   Round(Abs(Bitmap.Width   *   SinTheta   -   Bitmap.Height   *   CosTheta));
    end
    else
    begin
        Result.Width   :=   Round(Abs(Bitmap.Width   *   CosTheta   +   Bitmap.Height   *   SinTheta));
        Result.Height   :=   Round(Abs(Bitmap.Width   *   SinTheta   +   Bitmap.Height   *   CosTheta));
    end;
    with   Result.Canvas   do
    begin
        Brush.Color   :=   Color;
        Brush.Style   :=   bsSolid;
        FillRect(ClipRect);
    end;
    Result.PixelFormat   :=   pf24bit;
    Bitmap.PixelFormat   :=   pf24bit;
    xODst   :=   Result.Width   div   2;
    yODst   :=   Result.Height   div   2;
    xOSrc   :=   Bitmap.Width   div   2;
    yOSrc   :=   Bitmap.Height   div   2;
    for   ySrc   :=   Max(Bitmap.Height,   Result.Height)-1   downto   0   do
    begin
        yPrime   :=   ySrc   -   yODst;
        for   xSrc   :=   Max(Bitmap.Width,   Result.Width)-1   downto   0   do
        begin
            xPrime   :=   xSrc   -   xODst;
            xDst   :=   Round(xPrime   *   CosTheta   -   yPrime   *   SinTheta)   +   xOSrc;
            yDst   :=   Round(xPrime   *   SinTheta   +   yPrime   *   CosTheta)   +   yOSrc;
            if   (yDst   >=   0)   and   (yDst   <   Bitmap.Height)   and
                  (xDst   >=   0)   and   (xDst   <   Bitmap.Width)   and
                  (ySrc   >=   0)   and   (ySrc   <   Result.Height)   and
                  (xSrc   >=   0)   and   (xSrc   <   Result.Width)   then
            begin
                srcRow   :=   Bitmap.ScanLine[yDst];
                dstRow   :=   Result.Scanline[ySrc];
                dstRow[xSrc]   :=   srcRow[xDst];
            end;
        end;
    end;
end;

2. 调用例子:




Image2.Picture.Assign(RotateBitmap(Image1.Picture.Bitmap, 30, clWhite));


//将Image1的位图旋转30度后显示在Image2上

相关阅读 >>

Delphi 把exe可执行文件等作为资源包含在Delphi编译文件中

Delphi tmemo控件滚动条scrollbar末尾插入字符串一点都会闪烁的轻松实现

Delphi 执行一个外部程序,当外部程序结束后言主程序立即响应

Delphi tmemo 可以显示、编辑多行文本

Delphi中设置系统时间方法

Delphi 如何将图片转换成文本

Delphi firedac 另存json

Delphi base64, quoted-printable 的解码与编码函数

Delphi利用系统时间产生随机数的函数

Delphi 根据文本高度确定richedit高度

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...