代码之家  ›  专栏  ›  技术社区  ›  Andreas Niedermair

如何将这段代码从php重述到c#

  •  0
  • Andreas Niedermair  · 技术社区  · 14 年前

    此脚本来自 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 回复  |  直到 14 年前
        1
  •  1
  •   Alex    14 年前

    函数imagealphablending、imagesavealpha、imagecoloralallocatealpha来自GDlib。因此,要解决您的问题,您必须了解如何在C中使用GDlib或找到类似的函数。

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

    http://gd-sharp.sourceforge.net/