gpt4 book ai didi

delphi - 使TImage中的每个像素变暗

转载 作者:行者123 更新时间:2023-12-03 18:25:41 25 4
gpt4 key购买 nike

我在窗体上放置了一个TImage并在其中设置了PNG图像。

是否可以在运行时更改PNG图像每个像素的不透明度?我想根据应用程序中的特定操作更改不透明度。

我正在使用以下代码使像素变暗,其想法是将该功能应用于TImage中的每个像素:

function DarkerColor(thisColor: TColor; thePercent: Byte): TColor;
var
(* a TColor is made out of Red, Green and blue *)
cRed,
cGreen,
cBlue: Byte;
begin
(* get them individually *)
cRed := GetRValue(thisColor);
cGreen := GetGValue(thisColor);
cBlue := GetBValue(thisColor);
(* make them darker thePercent *)
(* we need a byte value but the "/" operator
returns a float value so we use Round function
because type mismatch *)
cRed := Round(cRed * thePercent / 100);
cGreen := Round(cGreen * thePercent / 100);
cBlue := Round(cBlue * thePercent / 100);
(* return them as TColor *)
Result := RGB(cRed, cGreen, cBlue);
end;


我发现了如何访问BMP图像中的每个像素。事实是,我正在使用加载了PNG的TImage。

谢谢!

最佳答案

在将其分配给ScanLine组件之前,只需使用PNG图像的TImage将其变暗即可。然后应该更快,然后将TBitmapTImage转换为PNG,执行操作并将其转换回TBitmap,然后再次将其分配给TImage组件。同样,将PNG分配给TBitmap后,将无法重新获得PNG的部分透明度。至少,我过去的所有尝试都没有成功,这就是为什么我更喜欢将PNG文件存储在我的小程序中并在运行时更改(调整大小,混合等)它们的副本的原因。

我更改了源代码,以排除带浮点的操作。它被替换为MulDiv函数。

function DarkerColor(thisColor: TColor; thePercent: Byte): TColor;
var
(* a TColor is made out of Red, Green and blue *)
cRed,
cGreen,
cBlue: Byte;
begin
(* get them individually *)
cRed := GetRValue(thisColor);
cGreen := GetGValue(thisColor);
cBlue := GetBValue(thisColor);

(* make them darker thePercent *)
cRed := MulDiv(cRed, thePercent, 100);
cGreen := MulDiv(cGreen, thePercent, 100);
cBlue := MulDiv(cBlue, thePercent, 100);

(* return them as TColor *)
Result := RGB(cRed, cGreen, cBlue);
end;


现在,您可以进一步使用 ScanLine图片的 PNG了:

procedure MakePNGDarker(APNGInOut: TPNGImage; AValue: Integer);
type
TRGBArray = Array [0..65535 - 1] of WinAPI.Windows.tagRGBTRIPLE;
PRGBArray = ^TRGBArray;
var
RowInOut: PRGBArray;
SourceColor: TColor;
ResultColor: TColor;
X: Integer;
Y: Integer;
begin
if not Assigned(APNGInOut) or (AValue < 0) then
Exit;

for Y:=0 to APNGInOut.Height - 1 do
begin
RowInOut := APNGInOut.ScanLine[Y];
for X:=0 to APNGInOut.Width - 1 do
begin
SourceColor := RGB(RowInOut[X].rgbtRed, RowInOut[X].rgbtGreen, RowInOut[X].rgbtBlue);
ResultColor := DarkerColor(SourceColor, AValue);

RowInOut[X].rgbtRed := GetRValue(ResultColor);
RowInOut[X].rgbtGreen := GetGValue(ResultColor);
RowInOut[X].rgbtBlue := GetBValue(ResultColor);
end;
end;
end;


这是功能完成的工作的结果。
图A代表原始图像。从B到F的数字是修改后图像的灰度等级,其暗度设置为25到0到100。

注意
彩色图像的边缘模糊不清,因为这些图像的边缘模糊不清。这不是功能工作的结果!

example

有用的资源


MulDiv function

关于delphi - 使TImage中的每个像素变暗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53724413/

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