问题描述
||
来源是PNG或GIF,其中应“着色”的像素为白色。背景可以是黑色,也可以是透明的,以最简单的为准。
现在,我想剪切出源的矩形部分,并将其与\“ brush \”的调色板颜色(gif)或RGB颜色(png)进行AND,以在其上\“ stamp \”具有该颜色的TImage / TCanvas。
RTFM可能会做的那些懒惰问题之一。但是,如果您有很好的解决方案,请分享:)
我尝试了Daud的PNGImage库,但是我什至无法加载源图像。有使用技巧吗?
该解决方案需要在D7及更高版本,XP及更高版本上运行。
解决方法
我知道您要用其他颜色更改白色吗?
如果是这样,我想您应该逐个像素检查图像,并检查像素是什么颜色,如果是白色则进行更改。
多数民众赞成在您如何遍历图像
var
iX : Integer;
Line: PByteArray;
...
Line := Image1.ScanLine[0]; // We are scanning the first line
iX := 0;
// We can\'t use the \'for\' loop because iX could not be modified from
// within the loop
repeat
Line[iX] := Line[iX] - $F; // Red value
Line[iX + 1] := Line[iX] - $F; // Green value
Line[iX + 2] := Line[iX] - $F; // Blue value
Inc(iX,3); // Move to next pixel
until iX > (Image1.Width - 1) * 3;
这是显示如何读取红色和蓝色值并进行切换的代码。
var
btTemp: Byte; // Used to swap colors
iY,iX: Integer;
Line : PByteArray;
...
for iY := 0 to Image1.Height - 1 do begin
Line := Image1.ScanLine[iY]; // Read the current line
repeat
btSwap := Line[iX]; // Save red value
Line[iX] := Line[iX + 2]; // Switch red with blue
Line[iX + 2] := btSwap; // Switch blue with previously saved red
// Line[iX + 1] - Green value,not used in example
Inc(iX,3);
until iX > (Image1.Width - 1) * 3;
end;
Image1.Invalidate; // Redraw bitmap after everything\'s done
但这仅适用于位图图像。
如果有用,请尝试将图像转换为位图,然后再对其进行操作。