从RGB整数转换为十六进制 [英] Converting from RGB ints to Hex

查看:105
本文介绍了从RGB整数转换为十六进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所拥有的是R:255 G:181 B:178,并且我正在C#中工作(更具体地说,对于WP8)

What I have is R:255 G:181 B:178, and I am working in C# (for WP8, to be more specific)

我想将其转换为十六进制数字以用作颜色(以设置WriteableBitmap的像素颜色).我正在做的事情如下:

I would like to convert this to a hex number to use as a color (to set the pixel color of a WriteableBitmap). What I am doing is the following:

int hex = (255 << 24) | ((byte)R << 16) | ((byte)G << 8) | ((Byte)B<<0);

但是当我这样做时,我只会变蓝.

But when I do this, I just get blue.

有任何想法我在做什么错吗?

Any ideas what I am doing wrong?

此外,要撤消此操作,要检查RGB值,我要进行以下操作:

Also, to undo this, to check the RGB values, I am going:

int r = ((byte)(hex >> 16)); // = 0
int g = ((byte)(hex >> 8)); // = 0
int b = ((byte)(hex >> 0)); // = 255

推荐答案

尝试以下操作:

using System.Drawing;
Color myColor = Color.FromArgb(255, 181, 178);
string hex = myColor.R.ToString("X2") + myColor.G.ToString("X2") + myColor.B.ToString("X2");

这篇关于从RGB整数转换为十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆