Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用c在windows通用应用程序中更改按钮图像颜色#_C#_Wpf_Windows - Fatal编程技术网

C# 如何使用c在windows通用应用程序中更改按钮图像颜色#

C# 如何使用c在windows通用应用程序中更改按钮图像颜色#,c#,wpf,windows,C#,Wpf,Windows,在iOS中,我可以在UIButton上使用setTintColor,它将更改文本和图标(png)的颜色。但我在使用windows universal app时找不到类似的方法 我尝试了一些方法来更改PNG文件的像素颜色,但它对我不起作用,BitmapImage在此之后变为空。代码出了什么问题,或者是否有其他我不知道的解决方案?我是windows开发新手,希望有人能提供帮助。提前谢谢 public async void LoadIconWithColor(string name, Soli

在iOS中,我可以在UIButton上使用setTintColor,它将更改文本和图标(png)的颜色。但我在使用windows universal app时找不到类似的方法

我尝试了一些方法来更改PNG文件的像素颜色,但它对我不起作用,BitmapImage在此之后变为空。代码出了什么问题,或者是否有其他我不知道的解决方案?我是windows开发新手,希望有人能提供帮助。提前谢谢

    public async void LoadIconWithColor(string name, SolidColorBrush color)
    {
        Uri uri = new Uri(this.IconsUri, name);
        BitmapImage image = new BitmapImage(uri);
        int height = 28; // the pixelHeight not working as well
        int width = 30;

        StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uri);

        using (var stream = await file.OpenReadAsync())
        {
            WriteableBitmap bitmap = new WriteableBitmap(width, height);
            await bitmap.SetSourceAsync(stream);

            using (var buffer = bitmap.PixelBuffer.AsStream())
            {
                Byte[] pixels = new Byte[4 * width * height];
                buffer.Read(pixels, 0, pixels.Length);

                for (int x = 0; x < width; x++)
                {

                    for (int y = 0; y < height; y++)
                    {
                        int index = ((y * width) + x) * 4;

                        Byte b = pixels[index + 0];
                        Byte g = pixels[index + 1];
                        Byte r = pixels[index + 2];
                        Byte a = pixels[index + 3];

                        pixels[index + 0] = color.Color.B;
                        pixels[index + 1] = color.Color.G;
                        pixels[index + 2] = color.Color.R;
                        pixels[index + 3] = a;
                    }
                }

                buffer.Position = 0;
                buffer.Write(pixels, 0, pixels.Length);

                MemoryStream ms = new MemoryStream(pixels);
                image.SetSource(ms.AsRandomAccessStream());
            }
        }

        // the image become blank
    }
public异步void LoadIconWithColor(字符串名称,SolidColorBrush颜色)
{
Uri=新Uri(this.IconsUri,name);
BitmapImage=新的BitmapImage(uri);
int height=28;//像素高度也不起作用
整数宽度=30;
StorageFile文件=等待StorageFile.GetFileFromApplicationUrisync(uri);
使用(var stream=await file.OpenReadAsync())
{
WriteableBitmap位图=新的WriteableBitmap(宽度、高度);
等待位图。设置源同步(流);
使用(var buffer=bitmap.PixelBuffer.AsStream())
{
字节[]像素=新字节[4*宽度*高度];
读取(像素,0,像素,长度);
对于(int x=0;x
为了显示颜色可变的图标,最好使用路径元素而不是图像。路径有一个
Data
属性,用于保存图标的几何图形,还有一个
Fill
属性用于填充笔刷,您可以指定一个SolidColorBrush。@Clemens这是否意味着我需要用代码绘制图标?如何从PNG文件中获取路径信息?不一定。你们可以使用像@CodingRhymath这样的设计工具。我也面临同样的问题,你们有办法改变图像的颜色吗?