zoukankan      html  css  js  c++  java
  • C#将Color32[]转到byte[]及其反转

    
        public static byte[] Color32ArrayToByteArray(Color32[] colors)
        {
            if (colors == null || colors.Length == 0)
                return null;
    
            int lengthOfColor32 = Marshal.SizeOf(typeof(Color32));
            int length = lengthOfColor32 * colors.Length;
            byte[] bytes = new byte[length];
    
            GCHandle handle = default(GCHandle);
            try
            {
                handle = GCHandle.Alloc(colors, GCHandleType.Pinned);
                IntPtr ptr = handle.AddrOfPinnedObject();
                Marshal.Copy(ptr, bytes, 0, length);
            }
            finally
            {
                if (handle != default(GCHandle))
                    handle.Free();
            }
    
            return bytes;
        }
    
    
    
    
    
        public static byte[] ConvertColor32ArrayToByteArray(Color32[] colors)
        {
            if (colors == null || colors.Length == 0)
                return null;
    
            int lengthOfColor32 = Marshal.SizeOf(typeof(Color32));
            int length = lengthOfColor32 * colors.Length;
            byte[] bytes = new byte[length];
    
            GCHandle handle = default(GCHandle);
            try
            {
                handle = GCHandle.Alloc(colors, GCHandleType.Pinned);
                IntPtr ptr = handle.AddrOfPinnedObject();
                Marshal.Copy(ptr, bytes, 0, length);
            }
            finally
            {
                if (handle != default(GCHandle))
                    handle.Free();
            }
    
            return bytes;
        }
    
    
        public static Color32[] ConvertByteArrayToColor32Array(byte[] bytes)
        {
            if (bytes == null || bytes.Length == 0)
                return null;
    
            int length = bytes.Length;
            int lengthOfColor32 = Marshal.SizeOf(typeof(Color32));
            Color32[] colors = new Color32[length / lengthOfColor32];
    
            GCHandle handle = default(GCHandle);
            try
            {
                handle = GCHandle.Alloc(colors, GCHandleType.Pinned);
                IntPtr ptr = handle.AddrOfPinnedObject();
                Marshal.Copy(bytes, 0, ptr, length);
            }
            finally
            {
                if (handle != default(GCHandle))
                    handle.Free();
            }
    
            return colors;
        }
    
    
  • 相关阅读:
    Unity --- sharedMaterial 、material
    lua --- Module
    lua --- 点号 和 冒号
    lua --- __newindex 的使用规则
    DirectX之顶点法线的计算
    DirectX学习之第一个可运行的工程
    java--select*
    java--Servlet做控制器实现代码和UI分离
    java--JSTL取代%
    java--entity层的引入
  • 原文地址:https://www.cnblogs.com/ezhar/p/13651274.html
Copyright © 2011-2022 走看看