Ejemplo n.º 1
0
        void getPic()
        {
            MyWebClient WClient = new MyWebClient(10);

            try
            {
                int i = -1;
                foreach (JsonElement item in items.EnumerateArray())
                {
                    i++;
                    if (contentNo > i)
                    {
                        continue;
                    }

                    if (i - contentNo >= 5)
                    {
                        break;
                    }

                    if (item.TryGetProperty("title", out JsonElement titleElement))
                    {
                        String title = titleElement.GetString().Replace("<b>", "").Replace("</b>", "");;
                        imgPage_title[i - contentNo].Text = title;
                    }
                    if (item.TryGetProperty("url", out JsonElement linkElement))
                    {
                        String link = linkElement.GetString();

                        imgPage_img[i - contentNo].pageNo  = pageNoBox.Text.ToString();
                        imgPage_img[i - contentNo].imgLink = link;
                    }
                    if (item.TryGetProperty("height", out JsonElement heightElement) && item.TryGetProperty("width", out JsonElement widthElement))
                    {
                        String height = heightElement.GetString();
                        String width  = widthElement.GetString();

                        imgSize_label[i - contentNo].Text = width + " * " + height;
                    }
                    if (item.TryGetProperty("tbMedUrl", out JsonElement thumbnailLinkElement))
                    {
                        String thumbnailLink = thumbnailLinkElement.GetString();
                        byte[] myDataBuffer  = WClient.DownloadData(thumbnailLink);
                        Stream stream        = new MemoryStream();
                        stream.Write(myDataBuffer, 0, myDataBuffer.Length);
                        imgPage_img[i - contentNo].BackgroundImage = Image.FromStream(stream, true);
                    }
                    if (item.TryGetProperty("fileFormat", out JsonElement extElement))
                    {
                        String ext = extElement.GetString();
                        if (ext == "image/jpeg")
                        {
                            imgPage_img[i - contentNo].ext = ".jpg";
                        }
                        else if (ext == "image/png")
                        {
                            imgPage_img[i - contentNo].ext = ".png";
                        }
                        else if (ext == "image/gif")
                        {
                            imgPage_img[i - contentNo].ext = ".gif";
                        }
                        else
                        {
                            imgPage_img[i - contentNo].ext = ".jpg";
                        }
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }
Ejemplo n.º 2
0
        void downloadPic(String fileName, String ext)
        {
            try
            {
                string sDirPath;
                sDirPath = Application.StartupPath + "\\imgFile";
                DirectoryInfo di = new DirectoryInfo(sDirPath);
                if (di.Exists == false)
                {
                    di.Create();
                }

                string      path    = ".\\imgFile\\" + fileName + ext;
                MyWebClient WClient = new MyWebClient(10);

                if (proxyCheck.Checked)
                {
                    WebProxy proxy = new WebProxy(proxyText.Text, Convert.ToInt32(proxyPortText.Text));
                    proxy.UseDefaultCredentials = false;
                    proxy.BypassProxyOnLocal    = false;

                    WClient.Proxy = proxy;
                }

                WClient.Headers.Add("Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
                WClient.Headers.Add("User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2300.0 Safari/537.36");
                WClient.DownloadFile(fileSrcText.Text, path);

                if (!checkFile(path))
                {
                    if (retry)
                    {
                        retry = false;
                        proxyCheck.Checked = !proxyCheck.Checked;
                        downloadPic(fileName, ext);
                        retry = true;
                        proxyCheck.Checked = !proxyCheck.Checked;
                    }
                }

                WClient.Dispose();
                statusLabel.Text = "";
            }
            catch (Exception e)
            {
                statusLabel.Text = e.Message;

                if (p1PageText.Text != "")
                {
                    try
                    {
                        click = Convert.ToInt32(p1PageText.Text);
                    }
                    catch
                    {
                    }
                }
                click--;
                nameText.Text   = click.ToString();
                p1PageText.Text = click.ToString();
            }
        }