Registriert seit: 7. Aug 2008
Ort: Brandenburg
1.477 Beiträge
Delphi 12 Athens
|
AW: PNG Image Gradient
8. Sep 2021, 10:56
Transparente abgerundete Ecken:
Delphi-Quellcode:
function CreateGrayScalePalette: HPalette;
var
LogPalette: TMaxLogPalette;
b: Byte;
begin
LogPalette.palVersion := $300;
LogPalette.palNumEntries := 256;
for b := 0 to 255 do
begin
with LogPalette.palPalEntry[b] do
begin
peRed := b;
peGreen := b;
peBlue := b;
end;
end;
Result := CreatePalette(@LogPalette);
end;
procedure Set8BitGrayScale(ABitmap: TBitmap);
begin
ABitmap.PixelFormat := pf8Bit;
ABitmap.Palette := CreateGrayScalePalette;
end;
procedure SetAlphaRoundRect(AImage: TPngImage; dx, dy: Integer);
var
BmpAlpha: TBitmap;
y: Integer;
begin
BmpAlpha := TBitmap.Create;
try
Set8BitGrayScale(BmpAlpha);
{Hintergrund Schwarz}
BmpAlpha.Canvas.Brush.Color := clBlack;
BmpAlpha.SetSize(AImage.Width, AImage.Height);
{sichtbaren Bereich Weiss füllen}
BmpAlpha.Canvas.Pen.Style := psClear;
BmpAlpha.Canvas.Brush.Color := clWhite;
BmpAlpha.Canvas.RoundRect(BmpAlpha.Canvas.ClipRect, dx, dy);
{Daten aus der Bitmap in den Alphachannel übernehmen}
AImage.CreateAlpha;
for y := 0 to AImage.Height - 1 do
begin
Move(BmpAlpha.ScanLine[y]^, AImage.AlphaScanline[y]^, AImage.Width);
end;
finally
BmpAlpha.Free;
end;
end;
procedure TForm1.TestClick(Sender: TObject);
var
Image: TPngImage;
begin
Image := TPngImage.Create;
try
Image.LoadFromFile('Background_Blue.png');
SetAlphaRoundRect(Image, 20, 20);
PaintBox1.Canvas.Draw(0, 0, Image);
finally
Image.Free;
end;
end;
|
|
Zitat
|