C# 如何使用c在windows通用应用程序中更改按钮图像颜色#
在iOS中,我可以在UIButton上使用setTintColor,它将更改文本和图标(png)的颜色。但我在使用windows universal app时找不到类似的方法 我尝试了一些方法来更改PNG文件的像素颜色,但它对我不起作用,BitmapImage在此之后变为空。代码出了什么问题,或者是否有其他我不知道的解决方案?我是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
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这样的设计工具。我也面临同样的问题,你们有办法改变图像的颜色吗?