从PNG或GIF图像中屏蔽掉白色,使用任何颜色将其快速复制到画布上



Source是PNG或GIF,其中应该"着色"的像素是白色的。背景可以是黑色或透明的,以最容易的为准。

现在我想剪下源代码的一个矩形部分,并将其与"画笔"的调色板颜色(gif)或RGB颜色(png)进行and运算,以将其"压印"在具有该颜色的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

但这仅用于位图图像。

如果这很有用的话,试着将图像转换为位图,然后对其进行操作。

最新更新