×
您的位置: 首页 > 编程笔记

不同的结果Bitmap.Maketransparent功能(Different results for Bitmap.Maketransparent function)

Java 时间:2020-06-15  查看:810   收藏

My problem is that i want to make an image background transparent. And the following function works fine for me but while testing on a different machine I found out that there are a lot of artifact colors and the transparency is not as clear as it is on my machine and some other machines.      I was working with the debug build and the test was done on release build. But even with release build we see different results on different machines.

Image CreateTransparentBackgroundImage(Image image)
        {
            using (Bitmap tempBitmap = new Bitmap(image.Width, image.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb))
            {
                var g = Graphics.FromImage(tempBitmap);
                Rectangle rec = new Rectangle(0 ,0,image.Width, image.Height);
                g.DrawImage(image,rec, 0, 0,image.Width,image.Height, GraphicsUnit.Pixel);
                tempBitmap.MakeTransparent(System.Drawing.Color.White);
                g.Dispose();
                return (Image)tempBitmap.Clone();
            }
        }

Any help would be really appreciated.

解决方案

Your problem seems to come from very slightly differing sources with very slight differences in the target color.

MakeTransparent doesn't provide for any tolerance.

Here is a function with the necessary tolerance. It will make every pixel tranparent that is closer than delta to the target color. (The distance is the simple sum of the three channels' deltas..)

public static Bitmap MakeTransparent(Bitmap bmp, Color col, int delta)
{
    // we expect a 32bpp bitmap!
    var bmpData = bmp.LockBits(
                            new Rectangle(0, 0, bmp.Width, bmp.Height),
                            ImageLockMode.ReadWrite, bmp.PixelFormat);

    long len = bmpData.Height * bmpData.Stride;
    byte[] data = new byte[len];
    Marshal.Copy(bmpData.Scan0, data, 0, data.Length);

    for (int i = 0; i < len; i += 4)
    {
        int dist = Math.Abs(data[i + 0] - col.B);
        dist += Math.Abs(data[i + 1] - col.G);
        dist += Math.Abs(data[i + 2] - col.R);
        if (dist <= delta) data[i + 3] = 0;
    }
    Marshal.Copy(data, 0, bmpData.Scan0, data.Length);
    bmp.UnlockBits(bmpData);
    return bmp;
}

Here are four images: 1) the source you gave and 2) - 3) results of calling the function with deta values of 5, 10 and 50. I have put them on top of a solid red background so you can see the tranparent pixels..:four versions

我的问题是,我想使图像背景透明。而下面的功能工作正常,但对我来说,同时在不同的机器上测试我发现有很多的伪色和,因为它是我的机器和其他一些机器上透明度不清晰。
      我与调试版本的工作和测试是在发布版本完成。但是,即使发布版本中,我们看到在不同的机器不同的结果。

 图像CreateTransparentBackgroundImage(形象画像)
        {
            使用(位图tempBitmap =新位图(image.Width,image.Height,System.Drawing.Imaging.PixelFormat.Format32bppPArgb))
            {
                变种G = Graphics.FromImage(tempBitmap);
                矩形REC =新的Rectangle(0,0,image.Width,image.Height);
                g.DrawImage(图像,REC,0,0,image.Width,image.Height,GraphicsUnit.Pixel);
                tempBitmap.MakeTransparent(System.Drawing.Color.White);
                g.Dispose();
                回报(图)tempBitmap.Clone();
            }
        }

任何帮助将是非常美联社preciated。


解决方案

您的问题似乎来自很略有不同来源与目标色非常细微的差别。

MakeTransparent 不提供的任何的耐受性。

下面是必要的耐受性的功能。这将使每个像素tranparent比三角洲更接近目标颜色。 (距离是三通道的三角洲的简单叠加。)

 公共静态位图MakeTransparent(BMP位图,颜色关口,INT三角洲)
{
    //我们预计32bpp的位图!
    VAR bmpData = bmp.LockBits(
                            新的Rectangle(0,0,bmp.Width,bmp.Height)
                            ImageLockMode.ReadWrite,bmp.PixelFormat);    长LEN = bmpData.Height * bmpData.Stride;
    字节[]数据=新的字节[LEN]
    Marshal.Copy(bmpData.Scan0,数据,0,data.Length);    的for(int i = 0; I&LT; LEN; I + = 4)
    {
        INT DIST = Math.Abs(数据[I + 0]  -  col.B);
        DIST + = Math.Abs(数据[I + 1]  -  col.G);
        DIST + = Math.Abs(数据第[i + 2]  -  col.R);
        如果(DIST&下; =三角形)数据第[i + 3] = 0;
    }
    Marshal.Copy(数据,0,bmpData.Scan0,data.Length);
    bmp.UnlockBits(bmpData);
    返回BMP;
}

下面是四个图像:1)源你给2) -  3)调用与5,10和50我已经把它们放在了坚实的红色背景之上,所以你可以看到二乙烯三胺值函数的结果在tranparent像素..:

 

0% (0)
0% (0)