0

我正在将图像传递给这个方法并淡化它的右边缘,它正在工作,但突然不再起作用了。它正在返回我传递给它的位图,但不保留修改后的像素,它们会恢复原状。我哪里错了?

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;
}
4

1 回答 1

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;
    }
于 2012-12-06T23:55:59.343 回答