加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 直接使用GDI绘制TeeChart

发布时间:2020-12-15 09:23:37 所属栏目:大数据 来源:网络整理
导读:如何在TChart上直接使用GDI旋转和绘制transperent PNG图像? 目前我将PNG图像加载到位图并使用两个位图进行GDI旋转(它工作得非常快)然后使目标位图透明并在TChart的画布上绘制(当透明度打开时它会很慢). procedure TPitchDisplay._chartAfterDraw(Sender: TO
如何在TChart上直接使用GDI旋转和绘制transperent PNG图像?

目前我将PNG图像加载到位图并使用两个位图进行GDI旋转(它工作得非常快)然后使目标位图透明并在TChart的画布上绘制(当透明度打开时它会很慢).

procedure TPitchDisplay._chartAfterDraw(Sender: TObject);
var
  c: TCanvas3D;
  pw,ph: Integer;
  r: TRect;
  bmp: TBitmap;
  rotAngle: Integer;
begin
  _setKnobBounds();
  c := _chart.Canvas;
  _drawCircle(c,_knob.CenterPoint.X,_knob.CenterPoint.Y,Round(_knob.YRadius * 0.9));
  c.StretchDrawQuality := sqHigh;
  pw := 450;
  ph := Round((pw / _shipBitmap.Width) * _shipBitmap.Height);
  r.Left := _chart.ChartXCenter - pw div 2;
  r.Top := Round(_chart.Height / 3);
  r.Width := pw;
  r.Height := ph;

  //Draw water
  c.StretchDraw(Rect(0,r.Top + Round(ph / 1.5),_chart.Width,_chart.Height),_waterBitmap);

  //Draw rotated ship over it
  bmp := TBitmap.Create();
  try
    bmp.Width := Round(_shipBitmap.Width * 1.2);
    bmp.Height := Round(_shipBitmap.Height * 1.7);
    bmp.TransparentColor := clBlack;
    bmp.Transparent := True;
    bmp.TransparentMode := TTransparentMode.tmFixed;
    if VarIsNumeric(_pitchBox.Value) then
      rotAngle := _pitchBox.Value
    else
      rotAngle := 0;
    TGraphUtils.RotateBitmap(_shipBitmap,bmp,0.5,0.7,rotAngle,False,clBlack);
    c.StretchDraw(r,bmp);
  finally
    bmp.Free();
  end;
end;

轮换程序:

class procedure TGraphUtils.RotateBitmap(srcBmp,tgtBmp: TBitmap; rotateAtX,rotateAtY: Single; Degs: Integer; AdjustSize: Boolean;
  BkColor: TColor = clNone);
var
  Tmp: TGPBitmap;
  Matrix: TGPMatrix;
  C: Single;
  S: Single;
  NewSize: TSize;
  Graphs: TGPGraphics;
  P: TGPPointF;
  attr: TGPImageAttributes;
begin
  Tmp := TGPBitmap.Create(srcBmp.Handle,srcBmp.Palette);
  Matrix := TGPMatrix.Create();
  try
    Matrix.RotateAt(Degs,MakePoint(rotateAtX * srcBmp.Width,rotateAtY * srcBmp.Height));
    if AdjustSize then begin
      C := Cos(DegToRad(Degs));
      S := Sin(DegToRad(Degs));
      NewSize.cx := Round(srcBmp.Width * Abs(C) + srcBmp.Height * Abs(S));
      NewSize.cy := Round(srcBmp.Width * Abs(S) + srcBmp.Height * Abs(C));
      tgtBmp.Width := NewSize.cx;
      tgtBmp.Height := NewSize.cy;
    end;
    Graphs := TGPGraphics.Create(tgtBmp.Canvas.Handle);
    attr := TGPImageAttributes.Create;
    try
      Graphs.Clear(ColorRefToARGB(ColorToRGB(BkColor)));
      Graphs.SetTransform(Matrix);
      //attr.SetColorKey($FFFFFF {TGPColor.Blue},$FFFFFF {TGPColor.Blue},ColorAdjustTypeBitmap);
      Graphs.DrawImage(Tmp,(Cardinal(tgtBmp.Width) - Tmp.GetWidth) div 2,(Cardinal(tgtBmp.Height) - Tmp.GetHeight) div 2);
    finally
      attr.Free();
      Graphs.Free();
    end;
  finally
    Matrix.Free();
    Tmp.Free();
  end;
end;

解决方法

enter image description here

procedure TForm1.Chart1AfterDraw(Sender: TObject);
var
  GPImage: TGPImage;
  GPGraphics: TGPGraphics;
  Matrix: TGPMatrix;
  c: TCanvas3d;
 begin
  c := Chart1.Canvas;
  GPImage := TGPImage.Create('e:2.png');
  GPGraphics :=  TGPGraphics.Create(C.Handle);
  Matrix := TGPMatrix.Create;
  Matrix.Rotate(30);
  Matrix.Scale(0.5,0.5);
  GPGraphics.SetTransform(Matrix);
  GPGraphics.DrawImage(GPImage,150,0);
  Matrix.Free;
  GPGraphics.Free;
  GPimage.Free;
end;

输出到表单中心的输出不精确:

IM := TGPMatrix.Create;
  IM := Matrix.Clone;
  IM.Invert;
  pc := MakePoint(Width div 2 - GPImage.GetWidth() div 2,Height div 2 - Integer(GPImage.GetHeight) div 2);
  Im.TransformPoints(pgppoINT(@pc));
  GPGraphics.DrawImage(GPImage,pc);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读