Rotate Canvas in Delphi

0

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

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

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

Example:

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

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

    
asked by anonymous 29.07.2016 / 01:05

2 answers

1

I did it once in the following way, I added a text in it and rotated 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];
    
29.07.2016 / 01:45
1

Dude, one way I found on google is bit by bit. I did not know it had to be this way.

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 did not test but it seems correct.

Source: link

    
29.07.2016 / 02:39