此脚本来自
here
:
$r = /* get R value from querystring */;
$g = /* get G value from querystring */;
$b = /* get B value from querystring *;
$mask = ImageCreateFromPng(/* magical path */);
$w = /* width of the mask */;
$h = /* height of the mask*/;
$im = /* create a new image with width and height of mask */;
imagealphablending($im, true); // ??
imagesavealpha($im, true); // ?
if ($r == 255) {
$trans = imageColorAllocateAlpha($im, 0, 255, 255, 127); // ?
}
else {
$trans = imageColorAllocateAlpha($im, 255, 255, 255, 127); // ?
}
imageFill($im, 0, 0, $trans); // fill the whole newly created image with this new color
for ($x = 0; $x < $w; $x++) {
for ($y = 0; $y < $h; $y++) {
$index = imageColorAt($mask, $x, $y); // retrieve the color of the mask
$maskRgba = imageColorsForIndex($mask, $index); // ?
if ($maskRgba['red'] == $r
&& $maskRgba['green'] == $g
&& $maskRgba['blue'] == $b) {
continue; // if they all match, continue
}
$alpha = 127 - ($maskRgba['red'] / 2); // calculate some alpha
$color = imageColorAllocateAlpha($im, $r, $g, $b, $alpha); // ?
imageSetPixel($im, $x, $y, $color);
}
}
现在。。。基本上,所有带有
// ?
我对把它们翻译成c#一无所知。
这就是我现在被困的地方
using
等等……)
var image = Image.FromFile(fileName); // ImageCreateFromPng
var bitmap = new Bitmap(image);
var canvas = Graphics.FromImage(bitmap);
var color = new Color(...);
var brush = new SolidBrush(color);
canvas.FillRectangle(brush, 0, 0, image.Width, image.Height); // $im instantiation
// ? imagealphablending
// ? imagesavealpha
// ? $trans
for (var rowCounter = 0; rowCounter < image.Height; rowCounter++)
{
for (var columnCounter = 0; columnCounter < image.Width; columnCounter++)
{
var pixel = bitmap.GetPixel(columnCounter, rowCounter);
// $index = ...
var pixelR = pixel.R;
var pixelG = pixel.G;
var pixelB = pixel.B;
// $maskRgba ??
if (pixelR == maskR)
{
continue;
}
if (pixelG == maskG)
{
continue;
}
if (pixelB == maskB)
{
continue;
}
var alpha = 127 - pixelR / 2;
pixel = Color.FromArgb(alpha, pixelR, pixelG, pixelB);
bitmap.SetPixel(columnCounter, rowCounter, pixel);
}
}