从 PNG 或 GIF 图像中屏蔽白色,使用任何颜色将其传输到画布

发布于 2024-11-15 03:20:32 字数 280 浏览 3 评论 0原文

源是 PNG 或 GIF,其中应“着色”的像素为白色。背景可以是黑色或透明,以最简单的为准。

现在我想剪下源的矩形部分,并将其与“画笔”的调色板颜色(gif)或RGB颜色(png)相结合,以将其“标记”在TImage/TCanvas上颜色。

可能是 RTFM 会做的那些懒惰问题之一。但如果你有一个好的解决方案,请分享:)

我尝试了 Daud 的 PNGImage 库,但我什至无法让它加载源图像。使用起来有什么技巧吗?

该解决方案需要在 D7 及更高版本、XP 及更高版本上运行。

Source is either PNG or GIF where the pixels that should be "colorized" are white. Background can be either black or transparent, whichever is easiest.

Now I'd like to cut out a rectangular part of the source, and AND it with the palette color (gif) or RGB color (png) of the "brush", to "stamp" it out on a TImage/TCanvas with that color.

Probably one of those lazy questions where RTFM would do. But if you have a nice solution please share :)

I tried Daud's PNGImage lib, but I can't even get it loading the source image. Is there a trick to using it?

The solution needs to work on D7 and up, XP and up.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

椒妓 2024-11-22 03:20:32

我明白你想用其他颜色改变白色吗?
如果是这样,我认为你应该逐像素检查图像并检查像素是什么颜色,如果是白色则更改它。

这就是循环浏览图像的方法。

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

但这仅适用于位图图像。

如果这有用,请尝试将图像转换为位图,然后对其进行操作。

do i understand you want to change the white color with some other color?
if that is so i think you should check the image pixel by pixel and check what color is the pixel and change it if is white.

thats how you can loop through image

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;

Here's code that show how to reads the Red and Blue values and switched them.

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

but this is for bitmap image only.

if this is useful try to convert your image to bitmap and from then manipulate it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文