无法擦除图像背景

无法擦除图像背景

本文介绍了SetPixel() 太慢,无法擦除图像背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发窗口电话应用程序.我想擦除图像的背景.我正在使用 setpixel() 方法.但是它擦除背景很慢.这是我的代码.

I'm working on window phone application. I want to erase background of an image. I'm using setpixel() method for it. But it erasing background very slow.Here is my code.

 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 应用程序中,因为不支持 dll.谁能帮我解决这个问题.提前致谢.

I've searched many articles in google , but noone worked for me. One of the method Bitmap.LockBits suggested to me. But the problem is we cannot add system.drawing into window phone app, because dll not supported.Can anyone help me to solve this out.Thanks in advance.

推荐答案

WriteableBitmap 有一个很好的属性叫做 Pixels.它只是一个整数数组,但它为您提供了更快的处理像素的方法.

WriteableBitmap has nice property called Pixels. It's just an array of integers but it gives you much faster way to manipulate pixels.

首先,您需要快速的 color-to-int 转换器:

First, you need fast color-to-int conventer:

public static int ColorToInt(Color color)
{
    return unchecked((int)((color.A << 24) | (color.R << 16) | (color.G << 8) | color.B));
}

然后您可以更改您的代码:

Then you can change your code:

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

这篇关于SetPixel() 太慢,无法擦除图像背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 22:40