Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys quick help with some code conversion if you please, developerfusion can't quite handle it.

[StructLayout(LayoutKind.Explicit)]
public struct Color32
{
    // Fields
    [FieldOffset(3)]
    public byte Alpha;
    [FieldOffset(0)]
    public int ARGB;
    [FieldOffset(0)]
    public byte Blue;
    [FieldOffset(1)]
    public byte Green;
    [FieldOffset(2)]
    public byte Red;

    // Methods
    public Color32(IntPtr pSourcePixel)
    {
       this = (Quantizer.Color32)Marshal.PtrToStructure(pSourcePixel, typeof(Quantizer.Color32));
    }

    // Properties
    public Color Color
    {
        get
        {
            return Color.FromArgb(this.Alpha, this.Red, this.Green, this.Blue);
        }
    }
}



It's the public Color32 bit I can't fathom.

Also

pSourcePixel = (IntPtr)(((long)pSourcePixel) + this._pixelSize);


Where localvariable pSourcePixel is type IntPtr is giving me grief, I'm getting the error:

Value of type 'Long' cannot be converted to 'System.IntPtr'

I'm messing about with a conversion of the OctreeQuantizer I'll understand what is going on inside it more if it's in VB (I know..... I'm trying to learn C# :sigh: )

Many thanks.

***********Update*******************************

Sorry, should have been more specific with the question.

' Methods
Public Sub New(ByVal pSourcePixel As IntPtr)
    Me = DirectCast(Marshal.PtrToStructure(pSourcePixel, GetType(Quantizer.Color32)), Quantizer.Color32)
End Sub


Cannot be used since, 'Me' cannot be the target of an assignment.
Posted
Updated 14-Jan-10 8:13am
v2

1 solution

Here you go.

<StructLayout(LayoutKind.Explicit)> _
Public Structure Color32
    ' Fields
    <FieldOffset(3)> _
    Public Alpha As Byte
    <FieldOffset(0)> _
    Public ARGB As Integer
    <FieldOffset(0)> _
    Public Blue As Byte
    <FieldOffset(1)> _
    Public Green As Byte
    <FieldOffset(2)> _
    Public Red As Byte
   
    ' Methods
    Public Sub New(ByVal pSourcePixel As IntPtr)
        Me = DirectCast(Marshal.PtrToStructure(pSourcePixel, GetType(Quantizer.Color32)), Quantizer.Color32)
    End Sub
   
    ' Properties
    Public ReadOnly Property Color() As Color
        Get
            Return Color.FromArgb(Me.Alpha, Me.Red, Me.Green, Me.Blue)
        End Get
    End Property
End Structure
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900