Wie färbe ich den Canvas-Bereich mit reinem GDI ein (colorize by specific alpha value)?
Ich möchte den Bereich einer Leinwand mit pure color mischen (colorize by specific alpha value)Windows GDI
(also ohne GDI +, DirectX oder ähnliches, kein OpenGL, kein Assembler oder Bibliotheken von Drittanbietern).
Ich habe die folgende Funktion erstellt und möchte wissen, ob es einen effizienteren oder einfacheren Weg gibt:
<code>procedure ColorBlend(const ACanvas: HDC; const ARect: TRect; const ABlendColor: TColor; const ABlendValue: Integer); var DC: HDC; Brush: HBRUSH; Bitmap: HBITMAP; BlendFunction: TBlendFunction; begin DC := CreateCompatibleDC(ACanvas); Bitmap := CreateCompatibleBitmap(ACanvas, ARect.Right - ARect.Left, ARect.Bottom - ARect.Top); Brush := CreateSolidBrush(ColorToRGB(ABlendColor)); try SelectObject(DC, Bitmap); Windows.FillRect(DC, Rect(0, 0, ARect.Right - ARect.Left, ARect.Bottom - ARect.Top), Brush); BlendFunction.BlendOp := AC_SRC_OVER; BlendFunction.BlendFlags := 0; BlendFunction.AlphaFormat := 0; BlendFunction.SourceConstantAlpha := ABlendValue; Windows.AlphaBlend(ACanvas, ARect.Left, ARect.Top, ARect.Right - ARect.Left, ARect.Bottom - ARect.Top, DC, 0, 0, ARect.Right - ARect.Left, ARect.Bottom - ARect.Top, BlendFunction); finally DeleteObject(Brush); DeleteObject(Bitmap); DeleteDC(DC); end; end; </code>
Für die Vorstellung, was diese Funktion tun sollte, siehe die folgenden (unterscheidenden :-) Bilder:
Und der Code, der rendern kannthis image
oben links im Formular wie oben gezeigt:
<code>uses PNGImage; procedure TForm1.Button1Click(Sender: TObject); var Image: TPNGImage; begin Image := TPNGImage.Create; try Image.LoadFromFile('d:\6G3Eg.png'); ColorBlend(Image.Canvas.Handle, Image.Canvas.ClipRect, $0000FF80, 175); Canvas.Draw(0, 0, Image); finally Image.Free; end; end; </code>
Gibt es eine effizientere Möglichkeit, dies mit GDI oder Delphi VCL zu tun?