如何将这段代码从php重新编译为c#

时间:2010-07-20 06:33:07

标签: c# php

此脚本来自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);
    }
}

1 个答案:

答案 0 :(得分:1)

imagealphablending,imagesavealpha,imageColorAllocateAlpha的功能来自GDlib。因此,要解决您的问题,您必须了解如何在C#中使用GDlib或找到类似的功能。

可以在此处找到PHP函数的文档,例如:http://de3.php.net/imagealphablending

我认为你需要GD-sharp lib:http://gd-sharp.sourceforge.net/