消除颜色的透明度 [英] Removing transparency from color
问题描述
当前,我正在使用此代码将RGB字符串转换为一种颜色,以将其设置为文本框的背景.
Currently I am using this code to convert my RGB string to a color to set as a the background for a Text Box.
ColorConverter colorConverter = new ColorConverter();
colorTextBox1.BackColor = (Color)colorConverter.ConvertFromString(displayColor);
但是运行此代码时出现此错误.
当displayColor = "#16776960"
的值时.
But I get this error when I run this code.
when the value of displayColor = "#16776960"
.
An unhandled exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll
Additional information: Control does not support transparent background colors.
关于如何从颜色中获取透明度的任何想法吗?
Any idea on how I can take out transparency from the color?
我要做的就是使文本框的背景变为彩色.
All I want it to do is make the background of the text box that color.
推荐答案
控件不支持半透明颜色,并且您的十六进制字符串的开头为16
,即alpha分量.要将颜色应用于控件,您将需要从中去除Alpha.
Controls do not support semi-transparent colors, and your hex string has 16
at the beginning, which is the alpha component. To apply the color to a control, you will need to strip the alpha from it.
ColorConverter colorConverter = new ColorConverter();
Color color = (Color)colorConverter.ConvertFromString(displayColor);
color = Color.FromARGB(255, color.R, color.G, color.B);
colorTextBox1.BackColor = color;
如果字符串的长度超过7个字符(6个颜色字符和1个#),您也可以从字符串中简单地删除它.
You can also simply remove the alpha from the string if it is more than 7 characters long (6 color chars and 1 #)
string hex = "#16776960";
if (hex.Length > 7)
hex = hex.Remove(1,2);
这篇关于消除颜色的透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!