简体   繁体   English

Windows Phone 8 - 更改位图中的颜色

[英]Windows Phone 8 - change color in bitmap

如何将位图的一种颜色更改为另一种颜色?

Your solution uses GetPixel and SetPixel methods which are really slow.您的解决方案使用了非常慢的GetPixelSetPixel方法。 You can get the same result much faster working on pixels directly.直接处理像素可以更快地获得相同的结果。 But before that we have to know how to convert Color to int, because pixels in WriteableBitmap are represented by int array.但在此之前我们必须知道如何将Color转换为int,因为WriteableBitmap中的像素是用int数组表示的。

Some of my apps use this method to manipulate pixels and I wanted to do it as fast as possible, so never use SetPixel or GetPixel (eg. MC Skin Editor , MC Skin Viewer ).我的一些应用程序使用这种方法来操作像素,我想尽可能快地完成它,所以永远不要使用SetPixelGetPixel (例如MC Skin EditorMC Skin Viewer )。

To convert Color to int I made this simple extension:要将Color转换为 int 我做了这个简单的扩展:

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

So now, your method could look like this:所以现在,您的方法可能如下所示:

public static WriteableBitmap ChangeColor(WriteableBitmap writeableBitmapOriginal, Color originalColor, Color newColor)
{
    var writeableBitmapNew = new WriteableBitmap(writeableBitmapOriginal);
    originalColorInt = originalColor.ToInt();
    newColorInt = newColor.ToInt();

    for (int i = 0; i < writeableBitmapNew.Pixels.Length; i++)
        if (writeableBitmapNew.Pixels[i] == originalColorInt)
            writeableBitmapNew.Pixels[i] = newColorInt;
    return writeableBitmapNew;
}

First, this is method which takes WritableBitmap a change color of your choice with another.首先,这是一种将 WritableBitmap 更改为您选择的颜色的方法。

    public static WriteableBitmap ChangeColor(WriteableBitmap writeableBitmapOriginal, Color originalColor, Color newColor)
    {
        var writeableBitmapNew = new WriteableBitmap(writeableBitmapOriginal);

        for (int i = 0; i < writeableBitmapNew.PixelWidth; i++)
        {
            for (int j = 0; j < writeableBitmapNew.PixelHeight; j++)
            {                    
                if (writeableBitmapOriginal.GetPixel(i, j).Equals(originalColor))
                {
                    writeableBitmapNew.SetPixel(i, j, newColor);
                }
            }
        }
        return writeableBitmapNew;
    }  

The imports for this method:此方法的导入:

using System.Windows.Media;
using System.Windows.Media.Imaging;

I also add how to instantly use this method, because loading of BitmapImage can be quite annoying (it is not loaded instantly from URI, therefore you can easily get NullPointer exception, thats why I am using things like StreamResourceInfo)我还添加了如何立即使用此方法,因为加载 BitmapImage 可能非常烦人(它不是从 URI 立即加载的,因此您很容易获得 NullPointer 异常,这就是我使用 StreamResourceInfo 之类的东西的原因)

Uri uri = new Uri("Assets/Icons/ic_black_star_fav.png", UriKind.Relative);
StreamResourceInfo resourceInfo = Application.GetResourceStream(uri);
BitmapImage img = new BitmapImage();
img.SetSource(resourceInfo.Stream);
WriteableBitmap wbm = new WriteableBitmap(img);
WriteableBitmap newWbm = YourClassWithThisMethod.ChangeColor(wbm,Color.FromArgb(255, 0, 0, 0), Color.FromArgb(255, 255, 0, 0));

The code above load picture, and changes black non-transparent color into the red and put it into variable newWbm .上面的代码加载图片,将不透明的黑色变为红色,放入变量newWbm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM