Convert .Net Color Objects to HEX codes and Back

后端 未结 5 571
广开言路
广开言路 2020-12-06 05:26

As per the question title, How could I take a hex code and convert it to a .Net Color object, and do it the other way?

I googled and keep getting the same way which

5条回答
  •  暖寄归人
    2020-12-06 05:52

    If you will convert the color back with {ColorTranslator.FromHTML}, 'White' is a valid string for a HTML color. But I've found that this implementation needs a # symbol before the string that represents the color if it isn't common (for example, it works with White, and #FFFFFF, but it doesn't with #White or FFFFFF). So I tried putting the conversion inside a try/catch block, made the conversion adding '#'to the string and if it threw an exception then I caught it and made the conversion without #.

提交回复
热议问题