【问题标题】:Graphics32 Layer RotationGraphics32 图层旋转
【发布时间】:2015-06-28 23:10:03
【问题描述】:

我正在尝试将 RotationLayer 与常规 TBitmapLayer 结合起来,以便能够尽可能地使用 ImgView32 图层。

所以我的想法是:

  1. 我有一个 TBitmapLayer(我需要它是 BitmapLayer,因为我正在做的不仅仅是旋转)。它加载了 BMP 图像
  2. 我在表单上放置了一个 TGaugeBar(就像 GR32 示例中的那些)
  3. 当鼠标按下该仪表时,我开始进行计算:我创建一个 RotationLayer,在其中放置原始 BitmapLayer.Bitmap 的内容
  4. 仪表的OnChange,我使用在MouseDown中创建的RotLayer对象并给它一个角度,并将Selection指定为TBitmap,RotLayer.Bitmap
  5. 在 MouseUp 上,我释放了使用的临时 RotationLayer 对象

所以基本上:将图像从实际层移动到临时旋转层,在那里进行旋转,然后,完成后,将旋转后的图像移回 BitmapLayer...

所以最后,我的逻辑似乎有效,除了我需要使用另一个 SO 问题(下面的链接)中提供的函数手动对 BitmapLayer 进行实际旋转。因为看起来rotationLayer实际上并没有旋转它的位图中的图像。它似乎只显示它旋转...

现在我的问题是:

  1. 我需要能够“调整”原始 TBitmapLayer 的大小,这样它就不会裁剪旋转的图像以适应旧的 BitmapLayer
  2. 显示rotationLayer 时,我使用BitmapCenter 将它显示在初始BitmapLayer 的顶部(我找不到另一种将它定位在我想要的位置的方法)。然而这个 BitmapCenter 似乎有两种用途:一种是定位图层,另一种是设置旋转的点。我怎样才能将旋转层准确地定位在原始 BitmapLayer 的顶部,并且 BitmapCenter(旋转中心)仍然位于 Bitmap 的中间?
  3. 似乎当我开始旋转时,旋转层被创建并加载了我的位图,我认为 alphaChannel 发生了一些事情,因为我认为图像变得有点暗和半透明,每次我分配 BitmapLayer.Bitmap到 RotationLayer.Bitmap。 我注意到通过注释 MasterAlpha:=200 行,图像不会失去它的亮度,但是现在当 RotationLayer 可见时,图层矩形的空白部分会变成黑色。所以它看起来很糟糕......而且当我执行 MouseUP 时(所以当我将旋转的位图分配给 BitmapLayer.Bitmap 时,一些黑线在旋转图像的外部仍然可见,因此在空白空间中)。有关如何保持原始图像清洁的任何建议?

请帮助我解决这三个问题。

目前的工作代码是:

procedure TMainForm.myrotMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  l,r,t,b:single;
  flrect:TFloatRect;
begin
    ro:=TRotlayer.Create(imgView.Layers);
    ro.Bitmap:=TBitmap32.Create;
    with ro.Bitmap do
    begin
      BeginUpdate;
      ro.Bitmap.Assign((Selection as TBitmapLayer).Bitmap);
      TLinearResampler.Create(ro.Bitmap);
      //ensure good looking edge, dynamic alternative to SetBorderTransparent
      TCustomResampler(ro.Bitmap.Resampler).PixelAccessMode := pamTransparentEdge;
      ro.BitmapCenter := FloatPoint(-(Selection as TBitmapLayer).Location.Left, -(Selection as TBitmapLayer).Location.Top);
//      MasterAlpha := 200;
      FrameRectS(BoundsRect, $FFFFFFFF);
      DrawMode := dmBlend;
      EndUpdate;
      Changed;
    end;
    ro.Scaled := True;
    (Selection as TBitmapLayer).Bitmap.Assign(ro.Bitmap);
end;

procedure TMainForm.myrotChange(Sender: TObject);
begin
  ro.Angle := myRot.Position;
  (Selection as TBitmapLayer).Bitmap.Assign(ro.Bitmap);
end;

procedure TMainForm.myrotMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
    bmx:=TBitmap32.Create;
    bmx.Assign((Selection as TBitmapLayer).Bitmap);
    RotateBitmap(bmx, -(Round(ro.Angle)), false, clWhite32, true);
    (Selection as TBitmapLayer).Bitmap.Assign(bmx);
    bmx.Free;
    ro.Free;
end;

RotateBitmap 函数就是从这个SO question 中挑选出来的

旋转透明图片的时候也有问题……用上面的代码自己测试一下,加载一些透明的PNG,你就会明白这个问题了。

【问题讨论】:

  • 我没有使用轮换,所以现在无法回答为什么您的第一次尝试不起作用。由于bmx 和图层位图之间的不断分配,第二次尝试搞砸了。仅在鼠标向下/向上分配如何?我很好奇,明天会看看这个。
  • 所以在我的第二次尝试中,即使我将分配移动到 MouseUp,位图仍然会变得混乱......所以不是那样的。请有空的时候试试我的代码,这样你就会明白真正的问题是什么。
  • 再想一想,如何保持原始图像不旋转但跟踪角度并且仅在显示/打印或最终与其他图层组合时应用旋转?我确信在每次分配给像素时都无法避免降级。我明天试试。
  • 问题是,现在,我的第一种方法开始起作用了......除了 ro.Bitmap 显然保持不旋转。我添加了一个带有 TImage32 的新表单 (StayOnTop),我在其中分配了 ro.Bitmap。而且无论我在做作业时的角度是什么,图像的显示方式与旋转图层之前的方式相同。所以我的假设是图像本身没有旋转,而是显示。
  • 所以我的第一种方法开始起作用,只是由于某种原因,如果我在其中加载一个小的 BMP 图像(可能颜色密度很小,可能分辨率我不知道确切),那么它的行为就很糟糕。如果我加载 JPG 图像(一些朋友的照片),旋转图层会显示并完成旋转工作,但是当我将鼠标向上并将旋转图层位图分配给 BitmapLayer 时,似乎什么也没发生.所以我决定使用常规的 image32 来放置生成的 rotationLayer.Bitmap 以确保图像得到旋转。可悲的是,位图似乎没有旋转......

标签: delphi delphi-xe graphics32


【解决方案1】:

答案(这些问题确实应该是单独的问题,但由于它们密切相关,我会处理它们。希望我不会被钉死。)

  1. 调整 TBitmapLayer 的大小以适应旋转的图像。 您可以通过设置 TBitmapLayer.Location 在您的 MyRotMouseUp 过程中执行此操作。旋转位图具有正确的WidthHeight 属性。有关示例,请参见下面的代码。如图所示设置位置也将其保持在ImgView的中心

  2. 如何将TRotationLayer 准确定位在TBitmapLayer 的顶部,并使旋转中心位于图像中心。 TRotationLayer 使用 Position 属性定位。位图旋转中心由BitmapCenter属性单独设置。
    定位TRotLayer 与定位其他层(例如TBitmapLayer)的不同之处在于它使用Position: TFloatPoint 属性与Location: TFloatRect 用于其他层。 Position 定义图层的中心点。
    与其他图层一样,参考坐标系取决于Scaled 属性。如果Scaled=False(默认)引用是TImgView32 边界。如果您随后将较大的图像加载到TimgView32.Bitmap,并使用滚动条滚动图像,TRotLayer 不会随图像一起移动。另一方面,如果Scaled=True 引用是TImgView32.Bitmap 并且TRotLayer 在滚动时跟随图像。
    要将旋转图层绑定到位图图层以便它们一起滚动,请将两个图层的Scaled 属性设置为True,并更改设置LocationPosition 的方式。我已经对代码进行了必要的修改。

  3. 在旋转层和原始位图之间来回分配位图时,图像颜色会发生变化。

是的,我清楚地明白您对原始代码的意思。这就是为什么我在评论中建议保持原始图像清洁(未旋转)并跟踪角度,以便原始图像仅用于显示一次变换。仍有一些可见的退化,但不会堆积到无法使用。

myRotMouseDown():原始图像(代码中的bmo: TBitmap32)分配给旋转层(rol: TRotationLayer)位图。 bml: TBitmapLayer 被隐藏 (Visible := False)。这里没有分配任何其他位图。

myRotChange():旋转层角度发生变化,表单全局变量更新为相同的角度数据。这里没有分配任何其他位图。

myRotMouseUp():原始图像被分配给位图层(bml: TBitmapLayer.Bitmap),并使用存储在表单中的角度通过RotateBitmap() 过程旋转位图。 bml.Location 已更新以适应旋转的图像。此处不分配任何其他位图(不需要bmx: TBitmap32)。 bml 再次可见。

我还建议不要保存旋转后的图像,而是只保存角度。这样,原始图像可以保持不变,并且可以在需要时以保存的角度简单地显示。

代码 (没有必要更改 RotateBitmap() 所以我在这里不包括在内)
表单字段

  private
    bmo: TBitmap32;     // original bitmap
    bml: TBitmapLayer;
//    bmx: TBitmap32;
    rol: TRotLayer;
    roa: single;        // rotation angle

方法

procedure TForm9.FormCreate(Sender: TObject);
var
  dstr, srcr: TRect;
  png: TPortableNetworkGraphic32;
begin
  png := TPortableNetworkGraphic32.Create;
  png.LoadFromFile('c:\tmp\imgs\arr-2.png');
  bmo:= TBitmap32.Create;
  bmo.Assign(png);
//  bmo.LoadFromFile('c:\tmp\imgs\arr.bmp');
  png.Free;
  bml := TBitmapLayer.Create(ImgView.Layers);
  bml.Bitmap.SetSize(bmo.Width, bmo.Height);
  bml.Scaled := True;  // !!! Changed to True for synching with rol !!!
  //bml.Location := FloatRect(
  //  (ImgView.Width  - bml.Bitmap.Width) * 0.5,
  //  (ImgView.Height - bml.Bitmap.Height)* 0.5,
  //  (ImgView.Width  + bml.Bitmap.Width) * 0.5,
  //  (ImgView.Height + bml.Bitmap.Height)* 0.5);
  // !!! Change follows to synch scrolling of bml and rol
  bml.Location := FloatRect(
    (ImgView.Bitmap.Width  - bml.Bitmap.Width) * 0.5,
    (ImgView.Bitmap.Height - bml.Bitmap.Height)* 0.5,
    (ImgView.Bitmap.Width  + bml.Bitmap.Width) * 0.5,
    (ImgView.Bitmap.Height + bml.Bitmap.Height)* 0.5);
  dstr := Rect(0, 0, bmo.Width, bmo.Height);
  srcr := Rect(0, 0, bmo.Width, bmo.Height);
  bml.Bitmap.DrawMode := dmBlend;
  bml.Bitmap.Draw(dstr, srcr, bmo.Handle);
end;

procedure TForm9.FormDestroy(Sender: TObject);
begin
  bmo.Free;
end;

procedure TForm9.myRotChange(Sender: TObject);
begin
  rol.Angle := myRot.Position * 3.6;
  roa := rol.Angle;
//  (Selection as TBitmapLayer).Bitmap.Assign(ro.Bitmap);
end;

procedure TForm9.myRotMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  l, r, t, b: single;
  flrect: TFloatRect;
begin
  rol := TRotLayer.Create(ImgView.Layers);
  rol.Scaled := True; // !!! Added for synching with bml
  with rol.Bitmap do
  begin
    BeginUpdate;
    Assign(bmo);
    // rol.Position := FloatPoint(ImgView.Width * 0.5, ImgView.Height* 0.5);
    // !!! Change follows to synch scrolling of bml and rol
    rol.Position := FloatPoint(
      (bml.Location.Right  + bml.Location.Left)*0.5,
      (bml.Location.Bottom + bml.Location.Top)*0.5);
//    rol.Bitmap.Assign((Selection as TBitmapLayer).Bitmap);
    TLinearResampler.Create(rol.Bitmap);
    // ensure good looking edge, dynamic alternative to SetBorderTransparent
    TCustomResampler(rol.Bitmap.Resampler).PixelAccessMode := pamTransparentEdge;
//    ro.BitmapCenter := FloatPoint(-(Selection as TBitmapLayer).Location.Left,
//      -(Selection as TBitmapLayer).Location.Top);
    rol.BitmapCenter := FloatPoint(Width * 0.5, Height * 0.5);
    // MasterAlpha := 200;
    FrameRectS(BoundsRect, $FFFFFFFF);
    DrawMode := dmBlend;
    rol.Angle := roa;
    EndUpdate;
    Changed;
  end;
  bml.Visible := False;
//  (Selection as TBitmapLayer).Bitmap.Assign(ro.Bitmap);
end;

procedure TForm9.myRotMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  bml.Bitmap.Assign(bmo);
  RotateBitmap(bml.Bitmap, -(Round(roa)), True, clWhite32, True);
  //bml.Location := FloatRect(
  //  (ImgView.Width  - bml.Bitmap.Width) * 0.5,
  //  (ImgView.Height - bml.Bitmap.Height)* 0.5,
  //  (ImgView.Width  + bml.Bitmap.Width) * 0.5,
  //  (ImgView.Height + bml.Bitmap.Height)* 0.5);
  // !!! Change follows to synch scrolling of bml and rol
  bml.Location := FloatRect(
    (ImgView.Bitmap.Width  - bml.Bitmap.Width) * 0.5,
    (ImgView.Bitmap.Height - bml.Bitmap.Height)* 0.5,
    (ImgView.Bitmap.Width  + bml.Bitmap.Width) * 0.5,
    (ImgView.Bitmap.Height + bml.Bitmap.Height)* 0.5);
  bml.Bitmap.DrawMode := dmBlend;
  rol.Free;
  bml.Visible := True;
end;

最后来几张截图:第一,没有旋转图片这么好。第二次旋转,边缘不是像素完美,但合理。

【讨论】:

    猜你喜欢
    • 2015-03-18
    • 1970-01-01
    • 2015-04-16
    • 1970-01-01
    • 1970-01-01
    • 2015-04-21
    • 1970-01-01
    • 2015-06-24
    • 2015-04-17
    相关资源
    最近更新 更多