开发者

howto restate this code from php to c#

开发者 https://www.devze.com 2023-01-07 20:20 出处:网络
this script is from here: $r = /* get R value from querystring */; $g = /* get G value from querystring */;

this script is from 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);
    }
} 

now ... basically, all the lines with // ? i'm blank with translating them to c#.

that's where i'm stuck right now (left out usings etc...)

var image = Image.FromFile(fileName); // ImageCreateFromPng
var bitmap = new Bitmap(image);
var canvas = Graphics开发者_JAVA技巧.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);
    }
}


The functions imagealphablending, imagesavealpha, imageColorAllocateAlpha are from GDlib. So to solve your problem you have to find out how to use GDlib in C# or find similar functions.

Documentation of the PHP functions can be found here for example: http://de3.php.net/imagealphablending

I think what you need it the GD-sharp lib: http://gd-sharp.sourceforge.net/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号