2008-11-07 關於 RGB Color 轉 Hex Color 11216 0 VB.Net / C#.Net / 摘要:關於 RGB Color 轉 Hex Color 關於 RGB Color 轉 Hex Color<< VB.Net >> Private Sub Button1_Click(ByVal s As Object, ByVal e As EventArgs) Handles Button1.Click ' RGB Color 轉 Hex Color MessageBox.Show(RGB_2_HEX(195, 20, 0)) MessageBox.Show(RGB2HEX(195, 20, 0)) End Sub ' 方法1 Private Function RGB_2_HEX(ByVal R As Byte, ByVal G As Byte, ByVal B As Byte) As String Return "#" & Hex(Color.FromArgb(R, G, B).ToArgb).Substring(2) End Function ' 方法2 Private Function RGB2HEX(ByVal R As Byte, ByVal G As Byte, ByVal B As Byte) As String Return "#" & Convert.ToString(Color.FromArgb(R, G, B).ToArgb, 16).Substring(2) End Function<< C# .Net >> private void button1_Click(object sender, EventArgs e) { // RGB Color 轉 16 進位 Hex Color MessageBox.Show(RGB_2_HEX(195, 20, 0)); MessageBox.Show(RGB2HEX(195, 20, 0)); } // 方法1 private string RGB_2_HEX(byte R, byte G, byte B) { return "#" + String.Format("{0:X}",Color.FromArgb(R, G, B).ToArgb()).Substring(2); } // 方法2 private string RGB2HEX(byte R, byte G, byte B) { return "#" + Convert.ToString(Color.FromArgb(R, G, B).ToArgb(), 16).Substring(2); } 回首頁