Ejemplo n.º 1
0
        private BitmapSource ImageFromClipboardDib()
        {
            MemoryStream ms = Clipboard.GetData("DeviceIndependentBitmap") as MemoryStream;

            if (ms != null)
            {
                byte[] dibBuffer = new byte[ms.Length];
                ms.Read(dibBuffer, 0, dibBuffer.Length);

                BITMAPINFOHEADER infoHeader =
                    BinaryStructConverter.FromByteArray <BITMAPINFOHEADER>(dibBuffer);

                int fileHeaderSize = Marshal.SizeOf(typeof(BITMAPFILEHEADER));
                int infoHeaderSize = infoHeader.biSize;
                int fileSize       = fileHeaderSize + infoHeader.biSize + infoHeader.biSizeImage;

                BITMAPFILEHEADER fileHeader = new BITMAPFILEHEADER();
                fileHeader.bfType      = BITMAPFILEHEADER.BM;
                fileHeader.bfSize      = fileSize;
                fileHeader.bfReserved1 = 0;
                fileHeader.bfReserved2 = 0;
                fileHeader.bfOffBits   = fileHeaderSize + infoHeaderSize + infoHeader.biClrUsed * 4;

                byte[] fileHeaderBytes =
                    BinaryStructConverter.ToByteArray <BITMAPFILEHEADER>(fileHeader);

                MemoryStream msBitmap = new MemoryStream();
                msBitmap.Write(fileHeaderBytes, 0, fileHeaderSize);
                msBitmap.Write(dibBuffer, 0, dibBuffer.Length);
                msBitmap.Seek(0, SeekOrigin.Begin);

                return(BitmapFrame.Create(msBitmap));
            }
            return(null);
        }
Ejemplo n.º 2
0
        public void LoadClipObject(ClipObject clipObject)
        {
            var ms = (MemoryStream) new DataObjectConverter().GetClipObjectData(clipObject);

            byte[] dibBuffer = new byte[ms.Length];
            ms.Read(dibBuffer, 0, dibBuffer.Length);

            BITMAPINFOHEADER infoHeader =
                BinaryStructConverter.FromByteArray <BITMAPINFOHEADER>(dibBuffer);

            int fileHeaderSize = Marshal.SizeOf(typeof(BITMAPFILEHEADER));
            int infoHeaderSize = infoHeader.biSize;
            int fileSize       = fileHeaderSize + infoHeader.biSize + infoHeader.biSizeImage;

            BITMAPFILEHEADER fileHeader = new BITMAPFILEHEADER();

            fileHeader.bfType      = BITMAPFILEHEADER.BM;
            fileHeader.bfSize      = fileSize;
            fileHeader.bfReserved1 = 0;
            fileHeader.bfReserved2 = 0;
            fileHeader.bfOffBits   = fileHeaderSize + infoHeaderSize + infoHeader.biClrUsed * 4;

            byte[] fileHeaderBytes =
                BinaryStructConverter.ToByteArray <BITMAPFILEHEADER>(fileHeader);

            MemoryStream msBitmap = new MemoryStream();

            msBitmap.Write(fileHeaderBytes, 0, fileHeaderSize);
            msBitmap.Write(dibBuffer, 0, dibBuffer.Length);
            msBitmap.Seek(0, SeekOrigin.Begin);

            _image.Source = BitmapFrame.Create(msBitmap);
        }
Ejemplo n.º 3
0
        public static BitmapSource ImageFromClipboardDib()
        {
            var ms = Clipboard.GetData("DeviceIndependentBitmap") as MemoryStream;

            if (ms == null)
            {
                return(null);
            }

            var dibBuffer = new byte[ms.Length];

            ms.Read(dibBuffer, 0, dibBuffer.Length);

            var infoHeader     = BinaryStructConverter.FromByteArray <BITMAPINFOHEADER>(dibBuffer);
            var fileHeaderSize = Marshal.SizeOf(typeof(BITMAPFILEHEADER));
            var infoHeaderSize = infoHeader.biSize;
            var fileSize       = fileHeaderSize + infoHeader.biSize + infoHeader.biSizeImage;

            var fileHeader = new BITMAPFILEHEADER
            {
                bfType      = BITMAPFILEHEADER.BM,
                bfSize      = fileSize,
                bfReserved1 = 0,
                bfReserved2 = 0,
                bfOffBits   = fileHeaderSize + infoHeaderSize + infoHeader.biClrUsed * 4
            };

            var fileHeaderBytes = BinaryStructConverter.ToByteArray(fileHeader);
            var msBitmap        = new MemoryStream();

            msBitmap.Write(fileHeaderBytes, 0, fileHeaderSize);
            msBitmap.Write(dibBuffer, 0, dibBuffer.Length);
            msBitmap.Seek(0, SeekOrigin.Begin);
            return(BitmapFrame.Create(msBitmap));
        }
Ejemplo n.º 4
0
        void WorldListResult(byte[] pMsg)
        {
            _world_list_result_locl worldListResultLocl = BinaryStructConverter.FromByteArray <_world_list_result_locl>(pMsg);

            if (worldListResultLocl.byRetCode == 0)
            {
                int nIndx       = 0;
                int nCountWorld = worldListResultLocl.sListData[nIndx++];
                for (int i = 0; i < nCountWorld; ++i)
                {
                    byte bWorldState    = worldListResultLocl.sListData[nIndx++];
                    byte byWorldNameLen = worldListResultLocl.sListData[nIndx++];

                    byte[] bName = new byte[byWorldNameLen];
                    for (int j = 0; j < byWorldNameLen; j++)
                    {
                        bName[j] = worldListResultLocl.sListData[j + nIndx];
                    }
                    _serverList.Add(new ServerState
                    {
                        s_ServerName  = Encoding.ASCII.GetString(bName).Substring(0, byWorldNameLen - 1),
                        b_ServerState = bWorldState
                    });
                    nIndx += byWorldNameLen + 1;
                }
            }
        }
        /// <summary>
        /// Gets the bitmap source from clipboard asynchronous.
        /// </summary>
        /// <returns>Task&lt;System.Object&gt;.</returns>
        public Task <object> GetBitmapSourceFromClipboardAsync()
        {
            if (System.Windows.Clipboard.GetData("DeviceIndependentBitmap") is MemoryStream iMemoryStream)
            {
                byte[] dibBuffer = new byte[Convert.ToInt32(iMemoryStream.Length - 1) + 1];
                iMemoryStream.Read(dibBuffer, 0, dibBuffer.Length);

                BITMAPINFOHEADER iInfoHeader = BinaryStructConverter.FromByteArray <BITMAPINFOHEADER>(dibBuffer);

                int fileHeaderSize = Marshal.SizeOf(typeof(BITMAPFILEHEADER));
                int infoHeaderSize = iInfoHeader.biSize;
                int fileSize       = fileHeaderSize + iInfoHeader.biSize + iInfoHeader.biSizeImage;

                BITMAPFILEHEADER iFileHeader = new BITMAPFILEHEADER
                {
                    bfType      = BITMAPFILEHEADER.BM,
                    bfSize      = fileSize,
                    bfReserved1 = 0,
                    bfReserved2 = 0,
                    bfOffBits   = fileHeaderSize + infoHeaderSize + iInfoHeader.biClrUsed * 4
                };

                byte[] fileHeaderBytes = BinaryStructConverter.ToByteArray <BITMAPFILEHEADER>(iFileHeader);

                MemoryStream iBitmapStream = new MemoryStream();
                iBitmapStream.Write(fileHeaderBytes, 0, fileHeaderSize);
                iBitmapStream.Write(dibBuffer, 0, dibBuffer.Length);
                iBitmapStream.Seek(0, SeekOrigin.Begin);

                return(Task.FromResult <object>(BitmapFrame.Create(iBitmapStream)));
            }
            return(Task.FromResult <object>(null));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Creates a BitmapFrame from a buffer containing a DIB.
        /// </summary>
        public static BitmapFrame CreateBitmapFrameFromDibBytes(byte[] dibBuffer)
        {
            BITMAPINFOHEADER infoHeader = BinaryStructConverter.FromByteArray <BITMAPINFOHEADER>(dibBuffer);

            int fileHeaderSize = Marshal.SizeOf(typeof(BITMAPFILEHEADER));
            int infoHeaderSize = infoHeader.biSize;
            int fileSize       = fileHeaderSize + infoHeader.biSize + infoHeader.biSizeImage;

            BITMAPFILEHEADER fileHeader = new BITMAPFILEHEADER
            {
                bfType      = BITMAPFILEHEADER.BM,
                bfSize      = fileSize,
                bfReserved1 = 0,
                bfReserved2 = 0,
                bfOffBits   = fileHeaderSize + infoHeaderSize + infoHeader.biClrUsed * 4
            };

            byte[] fileHeaderBytes = BinaryStructConverter.ToByteArray(fileHeader);

            MemoryStream msBitmap = new MemoryStream();

            msBitmap.Write(fileHeaderBytes, 0, fileHeaderSize);
            msBitmap.Write(dibBuffer, 0, dibBuffer.Length);
            msBitmap.Seek(0, SeekOrigin.Begin);

            return(BitmapFrame.Create(msBitmap));
        }
        public static MemoryStream GetConvertedBytes(ref MemoryStream ms)
        {
            if (ms == null)
            {
                return(null);
            }
            byte[] dibBuffer = new byte[ms.Length];
            ms.Read(dibBuffer, 0, dibBuffer.Length);
            BITMAPV5HEADER   infoHeader     = BinaryStructConverter.FromByteArray <BITMAPV5HEADER>(dibBuffer);
            int              fileHeaderSize = Marshal.SizeOf(typeof(BITMAPFILEHEADER));
            int              infoHeaderSize = infoHeader.bV5Size;
            int              fileSize       = fileHeaderSize + infoHeader.bV5Size + infoHeader.bV5SizeImage;
            BITMAPFILEHEADER fileHeader     = new BITMAPFILEHEADER();

            fileHeader.bfType      = BITMAPFILEHEADER.BM;
            fileHeader.bfSize      = fileSize;
            fileHeader.bfReserved1 = 0;
            fileHeader.bfReserved2 = 0;
            fileHeader.bfOffBits   = fileHeaderSize + infoHeaderSize + infoHeader.bV5ClrUsed * 4;
            byte[]       fileHeaderBytes = BinaryStructConverter.ToByteArray <BITMAPFILEHEADER>(fileHeader);
            MemoryStream msBitmap        = new MemoryStream();

            msBitmap.Write(fileHeaderBytes, 0, fileHeaderSize);
            msBitmap.Write(dibBuffer, 0, dibBuffer.Length);
            msBitmap.Seek(0, SeekOrigin.Begin);
            return(msBitmap);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Parses the ClipboardItem as a FileDrop (string[]).
        /// </summary>
        public static string[] ParseFileDrop(ClipboardItem item)
        {
            if (!CanParse(item, AbstractDataFormat.FileDrop))
            {
                throw new ArgumentException("The ClipboardItem does not contain data in the requested format.");
            }

            switch (item.Format)
            {
            case DataFormat.HDrop:
            {
                byte[] bytes = item.GetDataBuffer();
                // The first 18 bytes are object header and size information.
                // The rest is a block of zero-terminated unicode strings.
                string   fileNamesZero = Encoding.Unicode.GetString(bytes, 18, bytes.Length - 18);
                string[] fileNames     = fileNamesZero
                                         .Split('\0')
                                         .Where(x => x.Length > 0)
                                         .ToArray();

                return(fileNames);
            }

            case DataFormat.ShellIdList:
            {
                byte[]             bytes = item.GetDataBuffer();
                NativeMethods.CIDA cida  = BinaryStructConverter.FromByteArray <NativeMethods.CIDA>(bytes);

                return(new string[] { });
            }

            default:
                throw new NotImplementedException();
            }
        }
Ejemplo n.º 9
0
        public void PushCloseAccountResult(Socket client, byte[] pMsg)
        {
            _push_close_result_locl pushCloseResult = BinaryStructConverter.FromByteArray <_push_close_result_locl>(pMsg);

            if (pushCloseResult.byRetCode == 0)
            {
                CryptoKeyRequest(client);
            }
        }
Ejemplo n.º 10
0
        static int PaletteSize(byte[] dibBuffer)
        {
            BITMAPINFOHEADER infoHeader = BinaryStructConverter.FromByteArray <BITMAPINFOHEADER>(dibBuffer);
            BITMAPCOREHEADER coreHeader = BinaryStructConverter.FromByteArray <BITMAPCOREHEADER>(dibBuffer);

            if (infoHeader.biSize == System.Runtime.InteropServices.Marshal.SizeOf(infoHeader))
            {
                return(DIBNumColors(dibBuffer) * System.Runtime.InteropServices.Marshal.SizeOf(typeof(RGBQUAD)));
            }
            return(DIBNumColors(dibBuffer) * System.Runtime.InteropServices.Marshal.SizeOf(typeof(RGBTRIPLE)));
        }
Ejemplo n.º 11
0
        void CryptoKeyResult(Socket client, byte[] pMsg)
        {
            _cryptyKeyInformLocl = BinaryStructConverter.FromByteArray <_crypty_key_inform_locl>(pMsg);
            NetworkClientEventArgs args = new NetworkClientEventArgs();

            args.AccountSerial = 0;
            args.Servers       = null;
            args.DefaultSet    = new Default_Set();
            args.SConnection   = client;
            args.CState        = NetworkClientEventArgs.Callback.CRYPTO_KEY_INFORM;
            OnEvent(args);
        }
Ejemplo n.º 12
0
        void AfterLogin(Socket client, byte[] pMsg)
        {
            _login_account_result_locl loginAccountResultLocl = BinaryStructConverter.FromByteArray <_login_account_result_locl>(pMsg);

            // Fill in default set //
            _defaulSet.account_serial = loginAccountResultLocl.dwAccountSerial;
            _defaulSet.is_premium     = loginAccountResultLocl.dwBillingType > 1 ? 0 : (uint)1;

            // EventArgs for callback
            NetworkClientEventArgs args = new NetworkClientEventArgs();

            args.AccountSerial = loginAccountResultLocl.dwAccountSerial;
            args.Servers       = null;
            args.DefaultSet    = _defaulSet;
            args.SConnection   = client;

            switch (loginAccountResultLocl.byRetCode)
            {
            case 0x00:     // OK
                args.CState = NetworkClientEventArgs.Callback.NULL;
                WorldListRequest(client);
                break;

            case 48:       // Server CLOSE
                args.CState = NetworkClientEventArgs.Callback.LOGIN_ACCOUNT_SERVER_CLOSED;
                break;

            case 0x05:     // Account allready in game
                args.CState = NetworkClientEventArgs.Callback.LOGIN_ACCOUNT_ALREADY_IN_GAME;
                break;

            case 0x06:     // Login Wrong
                args.CState = NetworkClientEventArgs.Callback.LOGIN_ACCOUNT_WRONG_LOGIN;
                break;

            case 0x07:     // PW Wrong
                args.CState = NetworkClientEventArgs.Callback.LOGIN_ACCOUNT_WRONG_PW;
                break;

            case 0x28:     // Account BLOCK
                args.CState       = NetworkClientEventArgs.Callback.LOGIN_ACCOUNT_BANNED;
                args.ZBlockReason = loginAccountResultLocl.uszBlockReason;
                StopListen(true);
                break;

            default:
                args.CState = NetworkClientEventArgs.Callback.NULL;
                break;
            }

            OnEvent(args);
        }
Ejemplo n.º 13
0
        static int DIBNumColors(byte[] dibBuffer)
        {
            BITMAPINFOHEADER infoHeader = BinaryStructConverter.FromByteArray <BITMAPINFOHEADER>(dibBuffer);

            /*  If this is a Windows-style DIB, the number of colors in the
             *  color table can be less than the number of bits per pixel
             *  allows for (i.e. lpbi->biClrUsed can be set to some value).
             *  If this is the case, return the appropriate value.
             */

            if (infoHeader.biSize == System.Runtime.InteropServices.Marshal.SizeOf(infoHeader))
            {
                if (infoHeader.biClrUsed > 0)
                {
                    return(infoHeader.biClrUsed);
                }
            }

            /*  Calculate the number of colors in the color table based on
             *  the number of bits per pixel for the DIB.
             */
            int bitCount = 0;

            if (infoHeader.biSize == System.Runtime.InteropServices.Marshal.SizeOf(infoHeader))
            {
                bitCount = infoHeader.biBitCount;
            }
            else
            {
                BITMAPCOREHEADER coreHeader = BinaryStructConverter.FromByteArray <BITMAPCOREHEADER>(dibBuffer);

                bitCount = coreHeader.bcBitCount;
            }

            /* return number of colors based on bits per pixel */
            switch (bitCount)
            {
            case 1:
                return(2);

            case 4:
                return(16);

            case 8:
                return(256);
            }
            return(0);
        }
Ejemplo n.º 14
0
        private void SelectWorldResult(Socket client, byte[] pMsg)
        {
            _select_world_result_locl selectWorldResult = BinaryStructConverter.FromByteArray <_select_world_result_locl>(pMsg);

            if (selectWorldResult.byRetCode == 0)
            {
                // Fill in last data to defaultSet
                var serverCfg = LauncherConfig.GetInstance.ServerConfig;
                if (serverCfg.OverrideServerAddress)
                {
                    _defaulSet.world_ipv4 = (uint)IPAddress.Parse(serverCfg.ServerAddress.Split(':')[0]).Address;
                    _defaulSet.world_port = ushort.Parse(serverCfg.ServerAddress.Split(':')[1]);
                }
                else
                {
                    _defaulSet.world_ipv4 = selectWorldResult.dwWorldGateIP;
                    _defaulSet.world_port = selectWorldResult.wWorldGatePort;
                }

                _defaulSet.world_master_key = new uint[4];
                for (int i = 0; i < 4; i++)
                {
                    _defaulSet.world_master_key[i] = selectWorldResult.dwWorldMasterKey[i];
                }
                _defaulSet.allow_alt_tab = selectWorldResult.bAllowAltTab;
                _defaulSet.reserved      = 0;
                var nationCfg = LauncherConfig.GetInstance.NationalConfig;
                if (nationCfg.OverrideNationalCode)
                {
                    _defaulSet.nation_code = (ushort)nationCfg.NationCode;
                }
                else
                {
                    _defaulSet.nation_code = (ushort)e_nation_code.ru_ru;
                }
                StopListen(true);

                NetworkClientEventArgs args = new NetworkClientEventArgs();
                args.Servers     = _serverList;
                args.DefaultSet  = _defaulSet;
                args.CState      = NetworkClientEventArgs.Callback.SERVER_SESSION_RESULT;
                args.SConnection = client;
                OnEvent(args);
            }
        }
Ejemplo n.º 15
0
        private void WorldListInform(Socket client, byte[] pMsg)
        {
            _world_user_inform_locl worldUserInform = BinaryStructConverter.FromByteArray <_world_user_inform_locl>(pMsg);

            for (int i = 0; i < worldUserInform.byServiceWorldNum; i++)
            {
                ServerState state = _serverList[i];
                state.i_ServerLoad = worldUserInform.wUserNum[i];
            }

            NetworkClientEventArgs args = new NetworkClientEventArgs();

            args.Servers     = _serverList;
            args.DefaultSet  = _defaulSet;
            args.CState      = NetworkClientEventArgs.Callback.SERVER_LIST_INFORM;
            args.SConnection = client;
            OnEvent(args);
        }
Ejemplo n.º 16
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            StateObject state  = (StateObject)ar.AsyncState;
            Socket      client = state.WorkSocket;

            int bytesRead = client.EndReceive(ar);

            try
            {
                if (_acceptRecive)
                {
                    for (int i = 0; i < bytesRead;)
                    {
                        _MSG_HEADER msgHeader = BinaryStructConverter.FromByteArray <_MSG_HEADER>(state.Buffer, i, 4);

                        byte[] data = new byte[msgHeader.m_wSize - 4];
                        for (int j = 0; j < msgHeader.m_wSize - 4; ++j)
                        {
                            data[j] = state.Buffer[i + j + 4];
                        }

                        i += msgHeader.m_wSize;


                        DataAnalyze(client, msgHeader, data);
                    }

                    client.BeginReceive(state.Buffer, 0, StateObject.BufferSize, 0,
                                        ReceiveCallback, state);
                }
                else
                {
                    _receiveDone.Set();
                }
            }
            catch (Exception)
            {
                OnClientError();
            }
        }