powershell - 将 FromArgb 用于 Int32[] 值会导致交换红色和蓝色值
问题描述
我有这个Int32[]
值14508153
应该是:
R : 121
G : 96
B : 221
如果我使用:
[System.Drawing.Color]::FromArgb(14508153)
它返回:
R : 221
G : 96
B : 121
A : 0
IsKnownColor : False
IsEmpty : False
IsNamedColor : False
IsSystemColor : False
Name : dd6079
问题
这些值如何或为什么被交换
R
并B
使用该函数?是否有内置的 PowerShell 方法可以正确转换它们?
解决方案
我无法说明为什么需要重新排列字节(请参阅底部的想法),但您可以这样做:
Add-Type -AssemblyName System.Drawing
$bytes = [System.BitConverter]::GetBytes(14508153)
[byte[]] $rearrangedBytes = $bytes[2], $bytes[1], $bytes[0], $bytes[3]
[System.Drawing.Color]::FromArgb(
[System.BitConverter]::ToInt32($rearrangedBytes, 0)
)
见System.BitConverter.GetBytes()
,System.BitConverter.ToInt32()
。
以上产生:
R : 121
G : 96
B : 221
A : 0
IsKnownColor : False
IsEmpty : False
IsNamedColor : False
IsSystemColor : False
Name : 7960dd
您的( ) 值中似乎只有3个字节是相关的,并且它们是Big-Endian顺序(请参阅关于endianness的维基百科文章)。[int]
System.Int32
相比之下,System.Drawing.Color.FromArgb()
期望所有 4个字节都是相关的,按照Little-Endian顺序。
推荐阅读
- python - AttributeError:使用 coursera-dl 包时,“模块”对象在 Windows 上没有属性“PROTOCOL_TLSv1_2”
- if-statement - 我想避免在下面的代码中出现其他情况,有什么建议吗?
- python - 转换为 exe 和模块突然不起作用
- javascript - 当通过按钮单击javascript事件更改字体大小时,是否可以重新评估艺术指导?
- python - 如何从 csv 文件中排除某个组?
- python-3.x - 字节到字符串,无需解码
- macos - 在 mac (Mohave) 上安装 SRILM 时遇到问题
- index-error - 如何删除列表索引超出范围?
- c# - 在 WinForms 应用程序中通过 WebBrowser 控件登录 Atlassian 帐户
- ios - iOS 应用程序因 EXC_BREAKPOINT (SIGTRAP) 而崩溃