Rover.Packet.GetBytes C# (CSharp) Method

GetBytes() public method

public GetBytes ( ) : byte[]
return byte[]
        public byte[] GetBytes()
        {
            int size;
            unsafe
            {
                size = aFrame.Data.Length + sizeof(Header);
            }

            byte[] arr = new byte[size];
            IntPtr ptr = Marshal.AllocHGlobal(size);

            Marshal.StructureToPtr(aFrame, ptr, true);
            Marshal.Copy(ptr, arr, 0, size);
            Marshal.FreeHGlobal(ptr);

            return arr;
        }

Usage Example

Ejemplo n.º 1
0
        public void BitmapAcquiredCBHandler(Bitmap aNewBitmap)
        {
            stopwatch.Restart();

            //Console.WriteLine("BitmapAcquiredCBHandler:");
            //Console.WriteLine("From CameraId " + GetID());
            byte[] newBA = aCodecUtility.CompressBmpToJPEGArray(aNewBitmap);
            //byte[] newBA = (byte[])converter.ConvertTo(aNewBitmap, typeof(byte[]));

            //Total size of the packet to send including header  +  data.
            PacketPartitionner pp = new PacketPartitionner(GetID(), newBA.Length, Packet.GetHeaderSize() + Packet.DEFAULT_PACKET_SIZE, null);

            pp.PartitionFile(newBA, newBA.Length);

            //Console.WriteLine("Bitmap of size:" + newBA.Length + ", Splitted in:" + pp.GetPartitionnedPackets().Count + "Packets of " + (Packet.GetHeaderSize() + Packet.DEFAULT_PACKET_SIZE) + " Bytes");

            //Remove prints to the console
            //This is a critical section, we don't to have more than 1 camera sending data at the same time.
            for (int i = 0; i < pp.GetPartitionnedPackets().Count; i++)
            {
                //Console.WriteLine("Send packet number:" + i + "/" + (pp.GetPartitionnedPackets().Count - 1));

                //((Packet)pp.GetPartitionnedPackets()[i]).PrintInfo();

                Packet p = ((Packet)(pp.GetPartitionnedPackets()[i]));

                byte[] SerializedPacket = p.GetBytes();

                //aUDPSender.SendDataUDP(SerializedPacket, SerializedPacket.Length);
                aUDPSender.SendNow(SerializedPacket, SerializedPacket.Length);

                Thread.Sleep(10);
            }

            //Thread.Sleep(100);

            //clear the array list so you dont send alway the same image.
            //pp.GetPartitionnedPackets().Clear();

            stopwatch.Stop();
            // Write result
            //Console.WriteLine("Time elapsed ms: {0}", stopwatch.ElapsedMilliseconds);
        }