我正在开发窗口电话应用程序。我想要删除图像的背景。我正在使用setpixel()方法。但它删除背景非常慢。这是我的代码。
private void Canvas_MouseMove_1(object sender, System.Windows.Input.MouseEventArgs e)
{
wrt = new WriteableBitmap(imag, null);
try
{
System.Windows.Media.Color c = new System.Windows.Media.Color();
c.A = 0;
c.B = 0; c.R = 0; c.G = 0;
currentPoint = e.GetPosition(this.imag);
for (int degrees = 0; degrees <= 360; degrees++)
{
for (int distance = 0; distance <= erasersize; distance++)
{
//double angle = Math.PI * degrees / 180.0;
double x = currentPoint.X + (distance * Math.Cos(degrees));
double y = currentPoint.Y + (distance * Math.Sin(degrees));
wrt.SetPixel(Convert.ToInt32(x), Convert.ToInt32(y) - offset, c);
}
}
}
我在google搜索了很多文章,但没有人为我工作。 Bitmap.LockBits方法之一向我建议。但问题是我们无法将system.drawing添加到window phone app中,因为dll不受支持。 任何人都可以帮我解决这个问题。 提前谢谢。
答案 0 :(得分:1)
WriteableBitmap
具有名为Pixels
的不错属性。它只是一个整数数组,但它可以更快地操作像素。
首先,您需要快速的color-to-int conventer:
public static int ColorToInt(Color color)
{
return unchecked((int)((color.A << 24) | (color.R << 16) | (color.G << 8) | color.B));
}
然后你可以改变你的代码:
wrt = new WriteableBitmap(imag, null);
try
{
System.Windows.Media.Color c = new System.Windows.Media.Color();
c.A = 0;
c.B = 0; c.R = 0; c.G = 0;
currentPoint = e.GetPosition(this.imag);
int width = wrt.PixelWidth;
for (int degrees = 0; degrees <= 360; degrees++)
{
for (int distance = 0; distance <= erasersize; distance++)
{
//double angle = Math.PI * degrees / 180.0;
double x = currentPoint.X + (distance * Math.Cos(degrees));
double y = currentPoint.Y + (distance * Math.Sin(degrees));
wrt.Pixels[(int)(y - offset) * width + (int)x] = ColorToInt(c);
}
}
}