WPF怎么实现为图片去背景?我这两种方案错在哪了?请大神指点指点!!!!!

Jane_sl 2017-03-01 02:49:19
如题,不知道大家有谁做过这一方面的功能,我的思路是这样的:鼠标点击一点,取出该点的颜色,然后将图片上的这个颜色替换成透明的;如果鼠标滑动,就将所划过的颜色存储成一个列表,遍历图片,将图片中与列表相同的颜色替换成透明的。不知道这个思路对不对?
我将代码贴在下面,第一种方法,出来的图片带条纹,不能完全去除,我怀疑是不是我对byte数组解读的不对,第二种方法原有的颜色倒是去掉了,但是背景色变成了白色,而不是透明色,很伤脑筋!还望大神指点一二,非常感谢!

 #region 方法一:修改byte[]的颜色

//将图片转成byte数组存入内存中,在数组中修改鼠标点击的颜色, i:B i+1:G i+2:R i+3:Alpha 颜色一致的话改为 R:0xff,B:0xff,G:0xff Alpha:0
BitmapData bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);

byte[] maskImageRGBData = new byte[bmpData.Stride * bmpData.Height];

System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, maskImageRGBData, 0, maskImageRGBData.Length);

//鼠标选中的当前颜色
byte[] curColorBytes = new byte[3];
//curColorBytes[0] = 0xff;
//curColorBytes[1] = 0xff;
//curColorBytes[2] = 0xff;
//curColorBytes[3] = 0;
curColorBytes[0] = currentColor.B;
curColorBytes[1] = currentColor.G;
curColorBytes[2] = currentColor.R;


for (int j = 0; j + 2 < maskImageRGBData.Length; j += 4)
{
if(curColorBytes[0]==maskImageRGBData[j]&&curColorBytes[1]==maskImageRGBData[j+1]&&curColorBytes[2]==maskImageRGBData[j+2])
{
maskImageRGBData[j] = 0xff;
maskImageRGBData[j+1] = 0xff;
maskImageRGBData[j+2] = 0xff;
maskImageRGBData[j+3] = 0;
}
}

System.Runtime.InteropServices.Marshal.Copy(maskImageRGBData, 0, bmpData.Scan0, maskImageRGBData.Length);
bmp.UnlockBits(bmpData);
#endregion

#region 方法二:直接修改该像素点的值 currentColor即为鼠标获取到的像素点

if (bmp != null)
{
for (int i = 0; i < bmp.Width; i++)
{
for (int j = 0; j < bmp.Height; j++)
{
cur = bmp.GetPixel(i, j);
if (cur.Equals(currentColor))
{
bmp.SetPixel(i, j, System.Drawing.Color.FromArgb(0x00, 0xff, 0xff, 0xff));//System.Drawing.Color.Transparent); //
bmp.MakeTransparent(cur);
}
}
}
}
#endregion
bmp.Save("d://111.png");
...全文
982 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
天外怪魔 2017-03-10
  • 打赏
  • 举报
回复
引用 4 楼 Jane_sl 的回复:
方法一这种做法是对的吗?有了解的吗?
不对。首先图片的PixelFormat要支持透明才行。 其次,即使支持,你的代码也有问题,ARGB的时候,第0个字节是A,而你当成R来用了。设置透明的话,需要设置A为0,而你设置成了0xff。
Jane_sl 2017-03-07
  • 打赏
  • 举报
回复
方法一这种做法是对的吗?有了解的吗?
Jane_sl 2017-03-03
  • 打赏
  • 举报
回复
引用 1 楼 Libby1984 的回复:
你好像没有用到wpf的图像处理类,wpf的图像处理大多是是用BitmapSource以及它的子类。 你的第二种方法应该这么用
private void MakeTransparent_Example2(PaintEventArgs e)
{

    // Create a Bitmap object from an image file.
    Bitmap myBitmap = new Bitmap("Grapes.gif");

    // Draw myBitmap to the screen.
    e.Graphics.DrawImage(
        myBitmap, 0, 0, myBitmap.Width, myBitmap.Height);

    // Get the color of a background pixel.
    Color backColor = myBitmap.GetPixel(1, 1);

    // Make backColor transparent for myBitmap.
    myBitmap.MakeTransparent(backColor);

    // Draw the transparent bitmap to the screen.
    e.Graphics.DrawImage(
        myBitmap, myBitmap.Width, 0, myBitmap.Width, myBitmap.Height);
}
这种方法也试过,但是替换后的颜色还是有底色
  • 打赏
  • 举报
回复
你好像没有用到wpf的图像处理类,wpf的图像处理大多是是用BitmapSource以及它的子类。 你的第二种方法应该这么用
private void MakeTransparent_Example2(PaintEventArgs e)
{

    // Create a Bitmap object from an image file.
    Bitmap myBitmap = new Bitmap("Grapes.gif");

    // Draw myBitmap to the screen.
    e.Graphics.DrawImage(
        myBitmap, 0, 0, myBitmap.Width, myBitmap.Height);

    // Get the color of a background pixel.
    Color backColor = myBitmap.GetPixel(1, 1);

    // Make backColor transparent for myBitmap.
    myBitmap.MakeTransparent(backColor);

    // Draw the transparent bitmap to the screen.
    e.Graphics.DrawImage(
        myBitmap, myBitmap.Width, 0, myBitmap.Width, myBitmap.Height);
}

8,735

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧