为什么我的返回位图没有正确返回?

时间:2012-12-06 23:22:11

标签: c# image bitmap

我正在将图像传递给此方法并淡化其右边缘,它正在工作,突然不再了。它正在返回我传入的位图,但没有保留修改过的像素,它们又回归原始状态。我哪里错了?

private Bitmap fadedEdge(Bitmap bmp)
{
    //img.Save("i.bmp");
    //Bitmap bmp = (Bitmap)img;
    int howfartofade = bmp.Width / 4;
    int i = 0;
    if (howfartofade > 255) i = howfartofade / 255;
    else i = 255 / howfartofade;
    if (i == 0) i = 1;
    int alp = 255;
    int counter = 0;
    for (int x = bmp.Width - howfartofade; x < bmp.Width; x++)
    {
        if (howfartofade > 255)
        {
            counter++;
            if (counter == i + 1)
            {
                alp -= 1;
                counter = 0;
            }
        }
        else
        {
            alp -= i;
        }
        for (int y = 0; y < bmp.Height; y++)
        {
            if (alp >= 0)
            {
                Color clr = bmp.GetPixel(x, y);
                clr = Color.FromArgb(alp, clr.R, clr.G, clr.B);
                bmp.SetPixel(x, y, clr);
            }
            else
            {
                Color clr = bmp.GetPixel(x, y);
                clr = Color.FromArgb(0, clr.R, clr.G, clr.B);
                bmp.SetPixel(x, y, clr);
            }
        }
    }
    return bmp;
}

1 个答案:

答案 0 :(得分:0)

我明白了。愚蠢的我。我将其更改为传入图像并返回图像,并从传入图像的图像中创建新的位图。

private Image fadedEdge(Image input)
    {
        //img.Save("i.bmp");
        //Bitmap bmp = (Bitmap)img;
        Bitmap output = new Bitmap(input);
        int howfartofade = input.Width / 8;
        int i = 0;
        if (howfartofade > 255) i = howfartofade / 255;
        else i = (255 / howfartofade) + 1;
        if (i == 0) i = 1;
        int alp = 255;
        int counter = 0;
        for (int x = input.Width - howfartofade; x < input.Width; x++)
        {
            if (howfartofade > 255)
            {
                counter++;
                if (counter == i + 1)
                {
                    alp -= 1;
                    counter = 0;
                }
            }
            else
            {
                alp -= (i);
            }
            for (int y = 0; y < input.Height; y++)
            {
                if (alp >= 0)
                {
                    Color clr = output.GetPixel(x, y);
                    clr = Color.FromArgb(alp, clr.R, clr.G, clr.B);
                    output.SetPixel(x, y, clr);
                }
                else
                {
                    Color clr = output.GetPixel(x, y);
                    clr = Color.FromArgb(0, clr.R, clr.G, clr.B);
                    output.SetPixel(x, y, clr);
                }
            }
        }

        return output;
    }