我没有低级编程的经验,我需要这段代码不使用StructLayout(LayoutKind.Explicit)。我的站点在共享主机上运行,并且处于中等信任状态。所以如果这段代码在里面,它就不会运行。
更新:,我在一个八叉树中使用它来量化一个png文件。
有人知道附近有什么工作吗?
更新这里的新问题=> 是否有任何方法可以在没有编组的情况下安全地进行图像量化?
/// <summary>
/// Struct that defines a 32 bpp colour
/// </summary>
/// <remarks>
/// This struct is used to read data from a 32 bits per pixel image
/// in memory, and is ordered in this manner as this is the way that
/// the data is layed out in memory
/// </remarks>
[StructLayout(LayoutKind.Explicit)]
public struct Color32
{
public Color32(IntPtr pSourcePixel)
{
this = (Color32)Marshal.PtrToStructure(pSourcePixel, typeof(Color32));
}
/// <summary>
/// Holds the blue component of the colour
/// </summary>
[FieldOffset(0)]
public byte Blue;
/// <summary>
/// Holds the green component of the colour
/// </summary>
[FieldOffset(1)]
public byte Green;
/// <summary>
/// Holds the red component of the colour
/// </summary>
[FieldOffset(2)]
public byte Red;
/// <summary>
/// Holds the alpha component of the colour
/// </summary>
[FieldOffset(3)]
public byte Alpha;
/// <summary>
/// Permits the color32 to be treated as an int32
/// </summary>
[FieldOffset(0)]
public int ARGB;
/// <summary>
/// Return the color for this Color32 object
/// </summary>
public Color Color
{
get { return Color.FromArgb(Alpha, Red, Green, Blue); }
}
}发布于 2009-07-08 18:03:48
Marshal.PtrToStructure方法(IntPtr,Type)无论如何都不能工作,因为它需要
SecurityPermissionAttribute( SecurityAction.LinkDemand,Flags = SecurityPermissionFlag.UnmanagedCode)
你可以安全地处理补偿,但你必须不用元帅。
为了简单起见,我还建议使用uint。
public struct Color32
{
const uint BlueMask = 0xFF000000;
const uint GreenMask = 0x00FF0000;
const uint RedMask = 0x0000FF00;
const uint AlphaMask = 0x000000FF;
const int BlueShift = 24;
const int GreenShift = 16;
const int RedShift = 8;
const int AlphaShift = 0;
private byte GetComponent(uint mask, int shift)
{
var b = (this.ARGB & mask);
return (byte) (b >> shift);
}
private void SetComponent(int shift, byte value)
{
var b = ((uint)value) << shift
this.ARGB |= b;
}
public byte Blue
{
get { return GetComponent(BlueMask, BlueShift); }
set { SetComponent(BlueShift, value); }
}
public byte Green
{
get { return GetComponent(GreenMask, GreenShift); }
set { SetComponent(GreenShift, value); }
}
public byte Red
{
get { return GetComponent(RedMask, RedShift); }
set { SetComponent(RedShift, value); }
}
public byte Alpha
{
get { return GetComponent(AlphaMask, AlphaShift); }
set { SetComponent(AlphaShift, value); }
}
/// <summary>
/// Permits the color32 to be treated as an UInt32
/// </summary>
public uint ARGB;
/// <summary>
/// Return the color for this Color32 object
/// </summary>
public Color Color
{
get { return Color.FromArgb(Alpha, Red, Green, Blue); }
}
}但是,您想要做的似乎是以一个字节顺序将int32转换为相反的。
对于这一点,您有System.Net.IPAddress.HostToNetworkOrder (在这方面的恶劣用法,您只是使用它来逆转恩典,而不是具体说明您想要的顺序)。
因此,如果您可以将输入数据读取为简单的int32值,那么请执行以下操作:
static Color FromBgra(int bgra)
{
return Color.FromArgb(System.Net.IPAddress.HostToNetworkOrder(bgra));
}这可能要简单得多。
发布于 2009-07-08 17:47:39
与本机代码和内存进行互操作本质上是不安全的操作。任何打给元帅级的电话都会失败。除非您具有完全信任级别,否则无法访问内存,也不能执行任何互操作。
发布于 2009-07-08 17:46:59
您只需存储int,并使用BitConverter将其转换为颜色返回的4个字节。
更好的是,只需存储int,并使用Color.FromArgb(Int32)。
这两种方法都消除了存储单个字节的需要,因此您可以完全消除StructLayout属性。
https://stackoverflow.com/questions/1099477
复制相似问题