[英]C# Casting object to System.Drawing.Color
I am trying to convert a color setting that is read from a registry key into System.Drawing.Color. 我正在尝试将从注册表项读取的颜色设置转换为System.Drawing.Color。 I am casting the object that is returned from the registry to System.Drawing.Color, following this article: Convert OBJECT to System.Drawing.Color 根据本文,我正在将从注册表返回的对象转换为System.Drawing.Color : 将OBJECT转换为System.Drawing.Color
System.Drawing.Color color = (System.Drawing.Color)result;
Here are a couple of examples of these registry values that I am getting as an object: 以下是我作为对象获取的这些注册表值的几个示例:
Type Data
REG_SZ Color [Olive]
REG_SZ Color [A=255, R=255, G=128, B=128]
Casting the first registry key to System.Drawing.Color works fine, but when casting the second key, I get a "Specified cast is not valid" error. 将第一个注册表项强制转换为System.Drawing.Color可以正常工作,但是在强制转换第二个注册表项时,出现“指定的转换无效”错误。 What is the best way to be able to cast both of these keys as System.Drawing.Color? 能够将这两个键都转换为System.Drawing.Color的最佳方法是什么?
EDIT: I think the easiest way to solve this is when writing to the registry, I convert the Color to an ARGB (int32), that way when I read it I can just use Color.FromArgb
(unless there is another solution I'm completely missing). 编辑:我认为解决此问题的最简单方法是在写入注册表时,将Color转换为ARGB(int32),这样当我阅读它时,我就可以使用Color.FromArgb
(除非有另一种解决方案,完全丢失)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.