gpt4 book ai didi

delphi - Canvas 绘图 - 如何改进这个 Alpha 绘图例程?

转载 作者:行者123 更新时间:2023-12-03 14:58:56 35 4
gpt4 key购买 nike

我正在具有不透明度(Alpha 透明度)功能的 Canvas 上绘图,如下所示:

var
Form1: TForm1;

IsDrawing: Boolean;

implementation

{$R *.dfm}

procedure DrawOpacityBrush(ACanvas: TCanvas; X, Y: Integer; AColor: TColor; ASize: Integer; Opacity: Byte);
var
Bmp: TBitmap;
I, J: Integer;
Pixels: PRGBQuad;
ColorRgb: Integer;
ColorR, ColorG, ColorB: Byte;
begin
Bmp := TBitmap.Create;
try
Bmp.PixelFormat := pf32Bit; // needed for an alpha channel
Bmp.SetSize(ASize, ASize);

with Bmp.Canvas do
begin
Brush.Color := clFuchsia; // background color to mask out
ColorRgb := ColorToRGB(Brush.Color);
FillRect(Rect(0, 0, ASize, ASize));
Pen.Color := AColor;
Pen.Style := psSolid;
Pen.Width := ASize;
MoveTo(ASize div 2, ASize div 2);
LineTo(ASize div 2, ASize div 2);
end;

ColorR := GetRValue(ColorRgb);
ColorG := GetGValue(ColorRgb);
ColorB := GetBValue(ColorRgb);

for I := 0 to Bmp.Height-1 do
begin
Pixels := PRGBQuad(Bmp.ScanLine[I]);
for J := 0 to Bmp.Width-1 do
begin
with Pixels^ do
begin
if (rgbRed = ColorR) and (rgbGreen = ColorG) and (rgbBlue = ColorB) then
rgbReserved := 0
else
rgbReserved := Opacity;
// must pre-multiply the pixel with its alpha channel before drawing
rgbRed := (rgbRed * rgbReserved) div $FF;
rgbGreen := (rgbGreen * rgbReserved) div $FF;
rgbBlue := (rgbBlue * rgbReserved) div $FF;
end;
Inc(Pixels);
end;
end;

ACanvas.Draw(X, Y, Bmp, 255);
finally
Bmp.Free;
end;
end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
case Button of
mbLeft:
begin
IsDrawing := True;
DrawOpacityBrush(Form1.Canvas, X, Y, clRed, 50, 85);
end;
end;
end;

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if (GetAsyncKeyState(VK_LBUTTON) <> 0) and
(IsDrawing) then
begin
DrawOpacityBrush(Form1.Canvas, X, Y, clRed, 50, 85);
end;
end;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
IsDrawing := False;
end;

绘制 DrawOpacityBrush() 过程是 Remy Lebeau 对我最近提出的上一个问题的更新:How to paint on a Canvas with Transparency and Opacity?

虽然这有效,但结果并不能满足我现在的需要。

目前,每次在 MouseMove 中调用 DrawOpacityBrush() 过程时,它都会继续绘制画笔椭圆形状。这很糟糕,因为根据您在 Canvas 上移动鼠标的速度,输出并不如预期。

这些示例图像应该能更好地说明这一点:

enter image description here

- 第一个红色画笔我将鼠标从 Canvas 底部快速移动到顶部。
-第二个红色笔刷我移动得慢了很多。

正如您所看到的,不透明度已正确绘制,只是圆圈也继续重复绘制。

我希望它做的是:

(1) 在椭圆周围绘制不透明线。

(2) 有一个选项可以防止绘制任何省略号。

这个模拟示例图像应该让我了解我希望如何绘制它:

enter image description here

3 条紫色画笔线演示了选项 (1)

为了实现选项(2),画笔线内的圆圈不应该出现。

这样您就可以在绘图时花些时间,而不是在 Canvas 上疯狂地移动鼠标以期获得所需的结果。只有当您决定返回刚刚绘制的笔触时,该区域的不透明度才会变暗等。

如何实现这些类型的绘图效果?

我希望能够在 TImage 上绘图,因为这就是我当前正在做的事情,因此将 TCanvas 作为函数或过程中的参数传递将是理想的选择。我还将在绘图中使用 MouseDown、MouseMove 和 MouseUp 事件。

这是我使用 NGLN 提供的方法得到的输出:

enter image description here

不透明度似乎也应用于图像,它应该只是多段线。

最佳答案

为什么不直接画一条折线呢?

unit Unit1;

interface

uses
Windows, Classes, Graphics, Controls, Forms, ExtCtrls;

type
TPolyLine = record
Count: Integer;
Points: array of TPoint;
end;

TPolyLines = array of TPolyLine;

TForm1 = class(TForm)
PaintBox: TPaintBox;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure PaintBoxMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure PaintBoxMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure PaintBoxPaint(Sender: TObject);
private
FBlendFunc: BLENDFUNCTION;
FBmp: TBitmap;
FPolyLineCount: Integer;
FPolyLines: TPolyLines;
procedure AddPoint(APoint: TPoint);
function LastPoint: TPoint;
procedure NewPolyLine;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.AddPoint(APoint: TPoint);
begin
with FPolyLines[FPolyLineCount - 1] do
begin
if Length(Points) = Count then
SetLength(Points, Count + 64);
Points[Count] := APoint;
Inc(Count);
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
FBmp := TBitmap.Create;
FBmp.Canvas.Brush.Color := clWhite;
FBmp.Canvas.Pen.Width := 30;
FBmp.Canvas.Pen.Color := clRed;
FBlendFunc.BlendOp := AC_SRC_OVER;
FBlendFunc.SourceConstantAlpha := 80;
DoubleBuffered := True;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
FBmp.Free;
end;

procedure TForm1.FormResize(Sender: TObject);
begin
FBmp.Width := PaintBox.Width;
FBmp.Height := PaintBox.Height;
end;

function TForm1.LastPoint: TPoint;
begin
with FPolyLines[FPolyLineCount - 1] do
Result := Points[Count - 1];
end;

procedure TForm1.NewPolyLine;
begin
Inc(FPolyLineCount);
SetLength(FPolyLines, FPolyLineCount);
FPolyLines[FPolyLineCount - 1].Count := 0;
end;

procedure TForm1.PaintBoxMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if ssLeft in Shift then
begin
NewPolyLine;
AddPoint(Point(X, Y));
PaintBox.Invalidate;
end;
end;

procedure TForm1.PaintBoxMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if ssLeft in Shift then
if Sqr(LastPoint.X - X) + Sqr(LastPoint.Y - Y) > 30 then
begin
AddPoint(Point(X, Y));
PaintBox.Invalidate;
end;
end;

procedure TForm1.PaintBoxPaint(Sender: TObject);
var
R: TRect;
I: Integer;
begin
R := PaintBox.ClientRect;
FBmp.Canvas.FillRect(R);
for I := 0 to FPolyLineCount - 1 do
with FPolyLines[I] do
FBmp.Canvas.Polyline(Copy(Points, 0, Count));
Windows.AlphaBlend(PaintBox.Canvas.Handle, 0, 0, R.Right, R.Bottom,
FBmp.Canvas.Handle, 0, 0, R.Right, R.Bottom, FBlendFunc);
end;

end.

Blended polylines

第二张图片展示了如何将其与背景结合起来,并通过对代码进行以下少量添加而获得,而 FGraphic 是运行时加载的图片:

procedure TForm1.PaintBoxPaint(Sender: TObject);
var
R: TRect;
I: Integer;
begin
R := PaintBox.ClientRect;
FBmp.Canvas.FillRect(R);
for I := 0 to FPolyLineCount - 1 do
with FPolyLines[I] do
FBmp.Canvas.Polyline(Copy(Points, 0, Count));
PaintBox.Canvas.StretchDraw(R, FGraphic);
Windows.AlphaBlend(PaintBox.Canvas.Handle, 0, 0, R.Right, R.Bottom,
FBmp.Canvas.Handle, 0, 0, R.Right, R.Bottom, FBlendFunc);
end;

或者,要合并已绘制的作品(例如您的Image),请将其 Canvas 复制到PaintBox:

procedure TForm1.PaintBoxPaint(Sender: TObject);
var
R: TRect;
I: Integer;
begin
R := PaintBox.ClientRect;
FBmp.Canvas.FillRect(R);
FBmp.Canvas.Polyline(Copy(FPoly, 0, FCount));
for I := 0 to FPolyLineCount - 1 do
with FPolyLines[I] do
FBmp.Canvas.Polyline(Copy(Points, 0, Count));
Windows.AlphaBlend(PaintBox.Canvas.Handle, 0, 0, R.Right, R.Bottom,
FBmp.Canvas.Handle, 0, 0, R.Right, R.Bottom, FBlendFunc);
end;

但就像 David 在评论中提到的那样,我也强烈建议在 PaintBox 上绘制所有内容:这就是它的用途。

关于delphi - Canvas 绘图 - 如何改进这个 Alpha 绘图例程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10371787/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com