显示TImage部分暗淡

时间:2012-12-03 18:51:34

标签: delphi delphi-7

我正在尝试制作一个看起来如下的裁剪工具:

原始图片:

enter image description here

裁剪工具 - 这就是我想要的:

enter image description here

请注意,裁剪区域显示的是原始颜色,并且颜色周围都是暗淡的。


我所做的是在我的TShape上添加TImage个属性:

object Shape1: TShape
  Brush.Color = clSilver
  Pen.Mode = pmMask
  Pen.Style = psDot
end

我计划使用TShape进行重新调整大小/应对控制。 这就是它在Delphi中的表现:

enter image description here

正如你所看到的,它看起来不太好(调色板看起来很抖动),但主要的问题是我需要昏暗区域在裁剪区域周围,而不是在中心。我试图用另一个TShpae来覆盖整个TImage,尝试了不同的Pen.Mode组合,但是没有好的结果,我认为我的方法/方法很糟糕。

您对如何实现理想的行为有什么想法吗?

1 个答案:

答案 0 :(得分:6)

这里缺少一小部分,但添加不应该是一个问题...

unit Unit3;
// 20121108 by Thomas Wassermann
interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, jpeg;

type
  TForm3 = class(TForm)
    Image1: TImage;
    PaintBox1: TPaintBox;
    procedure FormCreate(Sender: TObject);
    procedure PaintBox1Paint(Sender: TObject);
    procedure PaintBox1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  private
    { Private-Deklarationen }
    FDownPoint, FCurrentPoint: TPoint;
  public
    { Public-Deklarationen }
  end;

var
  Form3: TForm3;

implementation

uses Math;
{$R *.dfm}

procedure TForm3.FormCreate(Sender: TObject);
begin
  PaintBox1.BringToFront;
end;

type
  pRGBQuadArray = ^TRGBQuadArray;
  TRGBQuadArray = ARRAY [0 .. $EFFFFFF] OF TRGBQuad;

Procedure SetAlpha(bmp: TBitMap; Alpha: Byte; R: TRect);
var
  pscanLine32: pRGBQuadArray;
  i, j: Integer;
begin
  bmp.PixelFormat := pf32Bit;
  bmp.HandleType := bmDIB;
  bmp.ignorepalette := true;
  bmp.alphaformat := afDefined;
  for i := 0 to bmp.Height - 1 do
  begin
    pscanLine32 := bmp.Scanline[i];
    for j := 0 to bmp.Width - 1 do
    begin
      if (j >= R.Left) and (j <= R.Right) and (i >= R.Top) and (i <= R.Bottom) then
      begin
        pscanLine32[j].rgbReserved := 0;
        pscanLine32[j].rgbBlue := 0;
        pscanLine32[j].rgbRed := 0;
        pscanLine32[j].rgbGreen := 0;
      end
      else
      begin
        pscanLine32[j].rgbReserved := Alpha;
        pscanLine32[j].rgbBlue := Alpha;
        pscanLine32[j].rgbRed := Alpha;
        pscanLine32[j].rgbGreen := Alpha;
      end;
    end;
  end;
end;

procedure TForm3.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  FDownPoint.X := X;
  FDownPoint.Y := Y;
  FCurrentPoint := FDownPoint;
  PaintBox1.Invalidate;
end;

procedure TForm3.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
  if ssLeft in Shift then
  begin
    FCurrentPoint.X := X;
    FCurrentPoint.Y := Y;
    PaintBox1.Invalidate;
  end;
end;

procedure TForm3.PaintBox1Paint(Sender: TObject);
var
  bmp: TBitMap;
  SelRect: TRect;
begin
  bmp := TBitMap.Create;
  try
    bmp.Width := PaintBox1.Width;
    bmp.Height := PaintBox1.Height;
    if (FCurrentPoint.X = FDownPoint.X) and (FCurrentPoint.Y = FDownPoint.Y) then
      SelRect := PaintBox1.BoundsRect
    else
    begin
      SelRect.Left := Min(FCurrentPoint.X, FDownPoint.X);
      SelRect.Top := Min(FCurrentPoint.Y, FDownPoint.Y);
      SelRect.Right := Max(FCurrentPoint.X, FDownPoint.X);
      SelRect.Bottom := Max(FCurrentPoint.Y, FDownPoint.Y);
    end;
    SetAlpha(bmp, 140, SelRect);
    PaintBox1.Canvas.Draw(0, 0, bmp);
  finally
    bmp.Free;
  end;
end;

end.

此解决方案的尝试是使用覆盖的绘图框,与图像相同的客户端,用于所有绘图和选择。通过使用鼠标/向下/移动生成的坐标,创建半透明位图,该位图在所选矩形中是完全透明的。一代又一代,它被涂在油漆箱上。可以在那里进行进一步的绘画,框架,锚,十字线。根据所选部分的不同,任何用户操作都必须在mousedown中捕获,例如。一个锚可以完成一个矩形的大小。 通常我更喜欢GDI +这样的请求,但如图所示,不需要额外的单位。资料来源:http://www.bummisoft.de/download/transparenteauswahl.zip Demo

相关问题