Rotar un bitmap x grados (usando Pixel)  

Enviado Por: Nacho Urenda
Web : N.A.
Email: nurenda@stonebirds.8m.com
Fecha: 25/08/00

Truco accedido 89 veces

 


  • Primero, añade Math en el uses de tu form
  • Ahora, pon un TImage (Image1) y carga un bitmap en ella
  • Pon un TButton (Button1) y mete este código en su evento OnClick:

     procedure TForm1.Button1Click(Sender: TObject);
    
       procedure RotateBitmap(OrigBmp: TBitmap; Angle: real; DestBmp: TBitmap);
       var
         ang,
         sinang, cosang,
         xori, yori,
         xrot, yrot,
         dx, dy           : extended;
         i, j             : integer;
         tmpbmp           : TBitmap;
       begin
           {
           Creamos un bitmap temporal. Esto nos permite usar el mismo bitmap para
           entrada y salida
           Create a temporal bitmap. This allows to use the same bitmap
           as input or outpuy
           }
         tmpbmp := TBitmap.Create;
    
         try
           {Asignamos al bitmap temporal las características del original
           Assign the temporal bitmap the same characteristics as the original}
           tmpbmp.Width := OrigBmp.Width;
           tmpbmp.Height := OrigBmp.Height;
           tmpbmp.PixelFormat := OrigBmp.PixelFormat;
    
           {Convertimos grados a radianes.
            Convert degrees to radians. DegToRad() is defined in math.pas}
           ang := DegToRad(Angle);
    
           {Calculamos valores para la traslación
           Calculate translation values}
           dx := (OrigBmp.Width-1) / 2;
           dy := (OrigBmp.Height-1) / 2;
    
           {Calculamos seno y coseno
            SinCos (math.pas) es más rápida que usar sin() y cos() por separado
            Compute sine and cosine
            SinCos (math.pas) is faster than calling sin() and cos() separately
           }
           SinCos(ang, sinang, cosang);
    
           {Rotación de coordenadas
            Para cada pixel del bitmap de destino, hallamos el correspondiente el el
            bitmap de origen
            Rotate coordinates
            For each pixel in the destination bitmap, we find the corresponding
            pixel in the original bitmap}
    
           {Para cada fila / for each row}
           for i := 0 to tmpbmp.Height -1 do
           begin
               {Para cada columna / for each column}
             for j := 0 to tmpbmp.Width - 1 do
             begin
                 {Trasladamos la coordenada con relación al centro del bitmap
                  Translate coordinate relative to the centre of the bitmap}
               xori := j - dx;
               yori := i - dy;
    
                 {Calculamos las coordenadas rotadas
                  Calculate rotated coordinates}
               xrot := (xori * cosang) - (yori * sinang);
               yrot := (xori * sinang) + (yori * cosang);
    
                 {Deshacemos la traslación
                  Undo translation}
               xrot := xrot + dx;
               yrot := yrot + dy;
    
                 {Copiamos el pixel del bitmap de origen al bitmap temporal
                  Copy pixel from origin bitmap to temporal bitmap}
               tmpbmp.Canvas.Pixels[j, i] :=
                 OrigBmp.Canvas.Pixels[Round(xrot), Round(yrot)];
             end;
           end;
    
             {Asignamos el bitmap rotado al bitmap de destino
              Assign the rotated bitmap to the destination bitmap}
           DestBmp.Assign(tmpbmp);
         finally
             {Destruimos bitmap temporal / Destroy temp bitmap}
           tmpbmp.Free;
         end;
       end;
    
     begin
       RotateBitmap(Image1.Picture.Bitmap,-10,Image1.Picture.Bitmap);
     end;
    



    Y ya está :)


    Actualizado el 27/08/2000