Rotate Canvas in Delphi

Asked

Viewed 816 times

0

I need to rotate an object inside a canvas at Delphi.

Example rotate by 90 degrees. Text I get is easy, but I have to be able to do for any type drawn object.

As for an image drawn on canvas or a geometric figure drawn by canvas.

Example:

Image1.canvas.font.orientation := 900; //equivale rotacionar em 90 gráus.

Only I wanted to for any object drawn on the canvas, not just texts. I can only with text as example above.

2 answers

1

I did once the following way, I add a text to it and rotate the image.

  procedure TForm2.ConvTextOut(CV: TCanvas; const sText: String; x, y,
      angle: integer);
    var
      LogFont: TLogFont;
      SaveFont: TFont;
    begin
      SaveFont := TFont.Create;
      SaveFont.Assign(CV.Font);
      GetObject(SaveFont.Handle, sizeof(TLogFont), @LogFont);
      with LogFont do
      begin
        lfEscapement := angle *10;
        lfPitchAndFamily := FIXED_PITCH or FF_DONTCARE;
      end;
      CV.Font.Handle := CreateFontIndirect(LogFont);
      SetBkMode(CV.Handle, TRANSPARENT);
      CV.TextOut(x, y, sText);
      CV.Font.Assign(SaveFont);
      SaveFont.Free;
    end;

    ConvTextOut(Origem.Canvas, label1.caption, 0, 0, 0);
      Resultado.Width := Origem.Height;
      Resultado.Height := Origem.Width;
      Resultado.Update;
      for k := 0 to Origem.Width do
        for Y := 0 to Origem.Height do
          Resultado.Canvas.Pixels[Origem.Height-Y, k] := Origem.Canvas.Pixels[k,Y];

1

Dude, a way I found on google is bit by bit. I didn’t know it had to be like this.

procedure Rotate90(Source: TGraphic; Target: TJpegImage);
var
    SourceBmp, TargetBmp: TBitmap;
    r, c: Integer;
    x, y: Integer;
begin
    SourceBmp := TBitmap.Create;
    SourceBmp.Assign(Source);
    TargetBmp := TBitmap.Create;
    TargetBmp.Width := SourceBmp.Height;
    TargetBmp.Height := SourceBmp.Width;

    for r := 0 to SourceBmp.Height - 1 do
    begin
        for c := 0 to SourceBmp.Width - 1 do
        begin
            //x := (SourceBmp.Height-1) - r; // -90
            //y := c; //-90
            x := r; //90
            y := (SourceBmp.Width-1) - c; //90
            // look into Bitmap.ScanLine for faster pixel access
            TargetBmp.Canvas.Pixels[x, y] := SourceBmp.Canvas.Pixels[c, r];
        end;
    end;

    Target.Assign(TargetBmp);
    SourceBmp.Free;
    TargetBmp.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
    Jpeg: TJPEGImage;
begin
    Jpeg := TJPEGImage.Create;
    Rotate90(Image1.Picture.Graphic, Jpeg);
    Image1.Picture.Assign(Jpeg);
    Jpeg.Free;
end;

I didn’t test it but it seems correct.

Source: http://www.delphigroups.info/2/12/317243.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.