29 Aralık 2015 Salı

c# get capta

private async Task GetImage()
        {
            try
            {
                mshtml.HTMLDocumentClass dom = (mshtml.HTMLDocumentClass)wborwser.Document;
                foreach (var item in dom.images)
                {
                    if ((item as mshtml.HTMLImgClass).alt == "CAPTCHA")
                    {
                        mshtml.IHTMLElement2 body2 = (mshtml.IHTMLElement2)dom.body;
                        mshtml.IHTMLControlRange controlRange = (mshtml.IHTMLControlRange)body2.createControlRange();
                        //  (item as mshtml.HTMLImgClass).

                        string strElName;
                        controlRange.add((mshtml.IHTMLControlElement)item);
                        controlRange.execCommand("Copy", false, System.Reflection.Missing.Value);
                        controlRange.remove(0);
                        strElName = (item as mshtml.HTMLImgClass).nameProp;

                        if (Clipboard.GetDataObject() != null)
                        {
                            IDataObject data = Clipboard.GetDataObject();

                            if (data.GetDataPresent(DataFormats.Bitmap))
                            {
                                System.Windows.Interop.InteropBitmap image = (System.Windows.Interop.InteropBitmap)data.GetData(DataFormats.Bitmap, true);

                                Cap_Img.Source = (ImageSource)data.GetData(DataFormats.Bitmap, true);

                                JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                                encoder.Frames.Add(BitmapFrame.Create(image));

                                string ThumbnailPath = Guid.NewGuid().ToString() + ".jpg";
                                FileStream stream = new FileStream(ThumbnailPath, FileMode.Create);
                                encoder.Save(stream);
                                stream.Close();

                                ImageSourceConverter conv = new ImageSourceConverter();
                                ImageSource imageSource = (ImageSource)conv.ConvertFromString(ThumbnailPath);

                                Cap_Img.Source = imageSource;
                            }
                        }
                    }
                }
            }
            catch
            {
                Status.Content = "Ошибка загруки";
                BusyIndicator.IsBusy = false;
            }
        }