private void ImageDropped(object sender, DragEventArgs e)
 {
     string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
     try
     {
         if (Path.GetExtension(files[0]) == ".jpg" || Path.GetExtension(files[0]) == ".jpeg" || Path.GetExtension(files[0]) == ".png")
         {
             BitmapImage       bitmap       = new BitmapImage(new Uri(files[0]));
             TransformedBitmap targetBitmap = new TransformedBitmap(bitmap, new ScaleTransform(1, 1));
             Image             img          = new Image();
             img.Source  = targetBitmap;
             img.Stretch = Stretch.None;
             img.Height  = 75;
             img.Width   = 150;
             Dispatcher.BeginInvoke(new ThreadStart(delegate
             {
                 MyMes.Margin  = new Thickness(0, 71, 150, 10);
                 Images.Margin = new Thickness(5, -5, 10, 10);
             }));
             Dispatcher.BeginInvoke(new ThreadStart(delegate
             {
                 Images.Document.Blocks.Add(new BlockUIContainer(img));
                 Images.Focus();
                 Images.CaretPosition = Images.Document.ContentEnd;
                 Images.ScrollToEnd();
                 MyMes.Focus();
             }));
         }
     }
     catch { }
 }
 private void ResizeRtbImages(RichTextBox rtb)
 {
     foreach (Block block in rtb.Document.Blocks)
     {
         if (block is Paragraph)
         {
             Paragraph paragraph = (Paragraph)block;
             foreach (Inline inline in paragraph.Inlines)
             {
                 if (inline is InlineUIContainer)
                 {
                     InlineUIContainer uiContainer = (InlineUIContainer)inline;
                     if (uiContainer.Child is Image)
                     {
                         Image img = (Image)uiContainer.Child;
                         img.Height = 75;
                         img.Width  = 150;
                         if (Images.Margin == new Thickness(160, -8, 10, 10))
                         {
                             Dispatcher.BeginInvoke(new ThreadStart(delegate
                             {
                                 MyMes.Margin  = new Thickness(0, 71, 150, 10);
                                 Images.Margin = new Thickness(5, -5, 10, 10);
                             }));
                         }
                         Dispatcher.BeginInvoke(new ThreadStart(delegate
                         {
                             uiContainer.Child = null;
                             Images.Document.Blocks.Add(new BlockUIContainer(img));
                             Images.Focus();
                             Images.CaretPosition = Images.Document.ContentEnd;
                             Images.ScrollToEnd();
                             MyMes.Focus();
                         }));
                     }
                 }
             }
         }
         if (block is BlockUIContainer)
         {
             BlockUIContainer blockui = (BlockUIContainer)block;
             if (blockui.Child is Image)
             {
                 Image img = (Image)blockui.Child;
                 img.Height = 75;
                 img.Width  = 150;
                 if (Images.Margin == new Thickness(160, -8, 10, 10))
                 {
                     Dispatcher.BeginInvoke(new ThreadStart(delegate
                     {
                         MyMes.Margin  = new Thickness(0, 71, 150, 10);
                         Images.Margin = new Thickness(5, -5, 10, 10);
                     }));
                 }
                 Dispatcher.BeginInvoke(new ThreadStart(delegate
                 {
                     blockui.Child = null;
                     Images.Document.Blocks.Add(new BlockUIContainer(img));
                     Images.Focus();
                     Images.CaretPosition = Images.Document.ContentEnd;
                     Images.ScrollToEnd();
                     MyMes.Focus();
                 }));
             }
         }
     }
 }
        //[STAThread]
        private void Get_Mes(object mesargums)
        {
            string sobname     = MainWindow.api.Users.Get(new long[] { ActiveUserId }).FirstOrDefault().FirstName;
            string predmessage = "zhзущшепгтзкищшекгвьезипщывьгпизшщкеигекзипщцнзуищкшецкещицшугеихцущзpweouetvpowiertupmesotrmuser[topetr[vpeto,ivwe[opybiemr[po";
            Array  mesargar    = new object[3];

            mesargar = (Array)mesargums;
            VkApi get    = (VkApi)mesargar.GetValue(0);
            int   userid = (int)mesargar.GetValue(1);

            bool messtate = false;

            while (true)
            {
                string            curmessage = "";
                MessagesGetObject getDialogs;
                try
                {
#pragma warning disable CS0618 // Тип или член устарел
                    getDialogs = get.Messages.GetDialogs(new MessagesDialogsGetParams
                    {
                        Count = 200
                    });
#pragma warning restore CS0618
                }
                catch
                {
#pragma warning disable CS0618
                    getDialogs = get.Messages.GetDialogs(new MessagesDialogsGetParams
                    {
                        Count = 200
                    });
#pragma warning restore CS0618
                }

                int pos;
                for (pos = 0; pos < 200; pos++)
                {
                    if (getDialogs.Messages[pos].UserId == userid)
                    {
                        curmessage = getDialogs.Messages[pos].Body;
                        messtate   = (bool)getDialogs.Messages[pos].Out;
                        break;
                    }
                }

                string decmessage = curmessage;
                try
                {
                    decmessage = Utils.SimCrypto.Decryption(curmessage);
                }
                catch
                {
                    decmessage = curmessage;
                }

                if (predmessage != decmessage && !messtate)
                {
                    _ = Dispatcher.BeginInvoke(new ThreadStart(delegate
                    {
                        string dectext = "";
                        Regex regnt    = new Regex("<Image>");
                        Regex regkt    = new Regex("</Image>");
                        try
                        {
                            Attachment documentAttachment = getDialogs.Messages[pos].Attachments.First(x => x.Type == typeof(Document));
                            string uri = ((Document)documentAttachment.Instance).Uri;
                            using (WebClient webClient = new WebClient())
                            {
                                webClient.DownloadFile(uri, (Path.Combine(Environment.CurrentDirectory, "Dec_Images.txt")));
                            }

                            string enctext = DecompressString(File.ReadAllText(Path.Combine(Environment.CurrentDirectory, "Dec_Images.txt")));
                            dectext        = Utils.SimCrypto.Decryption(enctext);
                            //File.WriteAllText(Path.Combine(Environment.CurrentDirectory, "dectestImages.txt"), dectext);
                        }
                        catch (Exception e)
                        {
                            //MessageBox.Show(e.ToString());
                        }

                        Paragraph fullmessage = new Paragraph();
                        Bold name             = new Bold(new Run(MainWindow.myname + '\n'))
                        {
                            Foreground = Brushes.Red
                        };
                        fullmessage.Inlines.Add(name);
                        fullmessage.Inlines.Add(Regex.Split(decmessage, "<VkMKDateMes>")[0]);
                        Chat.Document.Blocks.Add(fullmessage);
                        Chat.Focus();
                        Chat.CaretPosition = Chat.Document.ContentEnd;
                        Chat.ScrollToEnd();
                        MyMes.Focus();

                        try
                        {
                            while (dectext.Length > 0)
                            {
                                string imagetext = dectext.Substring(dectext.IndexOf("<Image>") + 7, dectext.IndexOf("</Image>") - dectext.IndexOf("<Image>") - 7);
                                try
                                {
                                    dectext = dectext.Substring(dectext.IndexOf("</Image>") + 8);
                                }
                                catch
                                {
                                    dectext = "";
                                }
                                File.WriteAllText(Path.Combine(Environment.CurrentDirectory, "dectestImages.txt"), dectext);
                                BitmapImage bitmap            = ToImage(StringToByte(imagetext));
                                Image img                     = new Image();
                                img.Source                    = bitmap;
                                TransformedBitmap transformed = new TransformedBitmap();
                                if (bitmap.Height <= 1000 && bitmap.Height >= 250 && bitmap.Width <= 1000 && bitmap.Width >= 250)
                                {
                                    transformed = new TransformedBitmap(bitmap, new ScaleTransform(0.6, 0.6));
                                }
                                else
                                {
                                    if (bitmap.Height <= 2000 && bitmap.Width <= 2000)
                                    {
                                        transformed = new TransformedBitmap(bitmap, new ScaleTransform(0.3, 0.3));
                                    }
                                }
                                img.Source  = transformed;
                                img.Stretch = Stretch.None;
                                img.Height  = transformed.Height;
                                img.Width   = transformed.Width;
                                if (dectext != "")
                                {
                                    fullmessage.Inlines.Add(Environment.NewLine);
                                    fullmessage.Inlines.Add(Environment.NewLine);
                                    fullmessage.Inlines.Add(img);
                                    fullmessage.Inlines.Add(Environment.NewLine);
                                }
                                Chat.Focus();
                                Chat.CaretPosition = Chat.Document.ContentEnd;
                                Chat.ScrollToEnd();
                                MyMes.Focus();
                            }
                        }
                        catch (Exception e)
                        {
                            //MessageBox.Show(e.ToString());
                        }
                    }));
                }
                predmessage = decmessage;
                Thread.Sleep(50);
            }
        }
        private void SendMes(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                string    message     = new TextRange(MyMes.Document.ContentStart, MyMes.Document.ContentEnd).Text;
                Paragraph fullmessage = new Paragraph();
                Bold      name        = new Bold(new Run(MainWindow.myname + '\n'))
                {
                    Foreground = Brushes.Blue
                };
                fullmessage.Inlines.Add(name);
                fullmessage.Inlines.Add(message);

                List <Image> images = GetImages(Images);
                foreach (Image image in images)
                {
                    try
                    {
                        message += "<Image>" + Convert.ToBase64String(ImageToByte(image.Source as BitmapImage)) + "</Image>";
                    }
                    catch
                    {
                        message += "<Image>" + Convert.ToBase64String(ImageToByte(image.Source as TransformedBitmap)) + "</Image>";
                    }
                }
                message = Regex.Replace(message, @"\t|\n|\r", "");
                string messtr = message;
                Dispatcher.BeginInvoke(new ThreadStart(delegate
                {
                    MyMes.Document.Blocks.Clear();
                    MyMes.Margin = new Thickness(0, 71, 10, 10);
                    Images.Document.Blocks.Clear();
                    Images.Margin = new Thickness(160, -8, 10, 10);
                }));
                foreach (Image image in images)
                {
                    Dispatcher.BeginInvoke(new ThreadStart(delegate
                    {
                        BitmapImage bitmap            = image.Source as BitmapImage;
                        TransformedBitmap transformed = new TransformedBitmap();
                        if (bitmap == null)
                        {
                            transformed = new TransformedBitmap(image.Source as TransformedBitmap, new ScaleTransform(1, 1));
                            if (transformed.Height <= 1000 && transformed.Height >= 250 && transformed.Width <= 1000 && transformed.Width >= 250)
                            {
                                transformed = new TransformedBitmap(image.Source as TransformedBitmap, new ScaleTransform(0.6, 0.6));
                            }
                            else
                            {
                                if (transformed.Height <= 2000 && transformed.Width <= 2000)
                                {
                                    transformed = new TransformedBitmap(image.Source as TransformedBitmap, new ScaleTransform(0.3, 0.3));
                                }
                            }
                            image.Source = transformed;
                            image.Height = transformed.Height;
                            image.Width  = transformed.Width;
                            fullmessage.Inlines.Add(image);
                            fullmessage.Inlines.Add("\n");
                            fullmessage.Inlines.Add("\n");
                            Chat.CaretPosition = Chat.Document.ContentEnd;
                            Chat.ScrollToEnd();
                        }
                        else
                        {
                            image.Height = 360.0 / 1.5;
                            image.Width  = 720.0 / 1.5;
                            fullmessage.Inlines.Add(image);
                            fullmessage.Inlines.Add("\n");
                            fullmessage.Inlines.Add("\n");
                            Chat.CaretPosition = Chat.Document.ContentEnd;
                            Chat.ScrollToEnd();
                        }
                    }));
                }
                Send_Message(MainWindow.api, ActiveUserId, message);
                Chat.Document.Blocks.Add(fullmessage);
                Chat.Focus();
                Chat.CaretPosition = Chat.Document.ContentEnd;
                Chat.ScrollToEnd();
                MyMes.Focus();
                MyMes.Document.Blocks.Clear();
                MyMes.ScrollToHome();
            }
        }