Example #1
0
        public static byte[] CreatePacket_StartSizeStopCompressSum(byte[] data)
        {
            if (data.IsNullOrEmpty())
            {
                return(null);
            }

            byte[] data_compressed = data.GZip();
            byte   compression     = 0;

            if (data_compressed.Length < data.Length - 1)
            {
                compression = 1;
                data        = data_compressed;
            }

            byte[] length   = Int32Ex.ToBytes(data.Length);
            Int32  checksum = data.ChecksumInt32();

            byte[] check = Int32Ex.ToBytes(checksum);

            List <byte> result = new List <byte>();

            #region packet
            result.Add(TcpAsyncCommon.StartByte);
            result.Add(compression);
            result.AddRange(length);
            result.AddRange(check);
            result.AddRange(data);
            result.Add(TcpAsyncCommon.EndByte);
            #endregion

            return(result.ToArray());
        }
Example #2
0
        /*public static int ToIndex2D(int x, int y, int width, int height)
         * {
         *  if (x < 0 || y < 0 || width <= 0 || height <= 0 || x >= width || y >= height)
         *      return -1;
         *
         *  return x + (width * y);
         * }
         *
         * public static Point FromIndex2D(int index, int width, int height)
         * {
         *  if (index < 0 || width <= 0 || height <= 0 || index >= (width * height))
         *      return new Point(-1, -1);
         *
         *
         *  int y = index / width;
         *  int x = index + (width * y);
         *
         *
         *  return new Point(x, y);
         * }*/

        /// <summary>
        /// maximum allowed matrix dimentions are 32767x32767
        /// </summary>
        /// <param name="bitmaps"></param>
        /// <returns></returns>
        public static byte[] ToByteArray_16(this Bitmap[,] bitmaps, ImageFormat format = null)
        {
            if (bitmaps.IsNullOrEmpty())
            {
                return(null);
            }

            int xParts = bitmaps.Width();
            int yParts = bitmaps.Height();

            if (!xParts.InClosedInterval(1, Int16.MaxValue))
            {
                return(null);
            }
            if (!yParts.InClosedInterval(1, Int16.MaxValue))
            {
                return(null);
            }


            List <byte> result = new List <byte>();

            result.AddRange(Int16Ex.ToBytes((Int16)xParts));
            result.AddRange(Int16Ex.ToBytes((Int16)yParts));

            Int16 x = 0, y;

            byte[] dd;
            for (; x < xParts; x++)
            {
                for (y = 0; y < yParts; y++)
                {
                    dd = bitmaps[x, y].ToByteArray(format);

                    if (dd.IsNullOrEmpty())
                    {
                        continue;
                    }

                    result.AddRange(Int16Ex.ToBytes(x));
                    result.AddRange(Int16Ex.ToBytes(y));
                    result.AddRange(Int32Ex.ToBytes(dd.Length));
                    result.AddRange(dd);
                }
            }

            if (result.IsCountLessOrEqual(24))
            {
                return(null);
            }

            return(result.ToArray());
        }