代码之家  ›  专栏  ›  技术社区  ›  SAMPro

Delphi:GR32混合alpha通道取决于Clear()颜色

  •  0
  • SAMPro  · 技术社区  · 9 年前

    我使用了一张带有alpha通道的图片,图像的某些部分不是完全不透明的。当我把它画在 TImgView32 从中获取一点颜色的对象 Clear() 颜色让我们通过示例图片来展示:

    这是您通常在Photoshop中看到的原始图片:

    enter image description here

    这是我在GR32对象中的一个新图层上绘制的结果图像,我之前用它清除了这个图层 Clear($00FF0000); :

    enter image description here

    结果像photohop一样透明,但红色光环是问题所在。

    或其他颜色 Clear($000000FF); :

    enter image description here

    请注意,正如您所知,我使用的颜色是完全透明的。

    下面是我用来创建上述结果的代码:

    begin
      ImageList.Bitmaps.Add;     //ImageList is a TBitmap32List object
      LoadPNGintoBitmap32(ImageList.Bitmap[0], ImgPath+'test.png', DummyBool);
    
      BL:= TBitmapLayer.Create(ImgView.Layers);
      ImgID:=0;
      ScaleFactor:= 1;
      LayerPos:= Point(100, 100);
    
      with ImageList.Bitmap[ImgID] do
        ImageRect:= Rect(0, 0, Width, Height);
    
      {Auxilary variables}
      CanvasWidth:= ImageRect.Right{*2};   //Extending width for two icon.
      CanvasHeight:= ImageRect.Bottom;
    
      BL.Location:= FloatRect(
        LayerPos.X,
        LayerPos.Y,
        LayerPos.X + CanvasWidth * ScaleFactor,
        LayerPos.Y + CanvasHeight * ScaleFactor
      );
    
      with BL.Bitmap do
      begin
        {Init}
        SetSize(CanvasWidth, CanvasHeight);
        DrawMode:= dmBlend;
        Clear($00FF0000);
    
        {Insert image}
        Draw(0, 0, ImageRect, ImageList.Bitmap[ImgID]);
      end;
    
      if ScaleFactor<>1 then
        TDraftResampler.Create(BL.Bitmap);  //BL.Bitmap, because we used "Draw" method
    end;
    

    如何避免这种情况,并获得像photoshop一样的清晰结果?

    2 回复  |  直到 9 年前
        1
  •  1
  •   AndersMelander    6 年前

    据我所知,只要您使用 cmBlend combine mode . cm混合 没有考虑背景的alpha the documentation 状态,不应用于混合位图:

    cmBlend-使用提供的alpha快速混合前景颜色和背景颜色。此方法不适用于工作和保存alpha通道。如果要直接混合到显示缓冲区,请使用此选项。

    使用 cmMerge 当前景和背景alpha都很重要时,改为组合模式。

        2
  •  0
  •   penarthur66    9 年前

    我最近也遇到过类似的问题

    TImageList不支持透明度,所以当我想在运行时将透明png图像加载到控件中时,我不得不使用TPngImageList。

    TPngImageList可通过谷歌搜索获得!