我如何通过新窗体Form1中的pictureBox1实时传递和更新?

西蒙·加利利(Simon Gamlieli)

我向项目添加了一个新表单,该新表单包含在设计器的webBrowser控件中。我正在做的是从html解析一些链接,然后导航到每个链接,然后截取屏幕截图,并将每个我在webBrowser中导航到的图像保存到硬盘上。

最后,当所有链接都经过导航并且我将图像存储在硬盘上时,我使用hScrollBar将它们显示在Form1 pictureBox上。但是现在,要等待它以新形式完成,然后显示所有图像,我想在form1的pictureBox中的硬盘上显示每个保存的图像。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;

namespace WebBrowserScreenshots.cs
{
    public partial class WebBrowserScreenshots : Form
    {

        private class MyComparer : IComparer<string>
        {
            [DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
            private static extern int StrCmpLogicalW(string x, string y);
            public int Compare(string x, string y)
            {
                return StrCmpLogicalW(x, y);
            }
        }

        List<string> newHtmls = new List<string>();
        string uri = "";
        public bool htmlloaded = false;
        int countlinks = 0;
        public int numberoflinks = 0;
        public int numberofimages = 0;
        public List<string> imageList = new List<string>();

        public WebBrowserScreenshots()
        {
            InitializeComponent();

            webBrowser1.ScrollBarsEnabled = false;
            webBrowser1.ScriptErrorsSuppressed = true;
            NavigateToSites();
        }

        string test;
        List<string> htmls;
        private void GetLinks()
        {
            htmlloaded = true;
            for (int i = 1; i < 304; i++)
            {
                if (newHtmls.Count == 1)
                    break;
                backgroundWorker1.ReportProgress(i);
                HtmlAgilityPack.HtmlWeb hw = new HtmlAgilityPack.HtmlWeb();
                HtmlAgilityPack.HtmlDocument doc = hw.Load("http://test/page" + i);
                htmls = new List<string>();
                foreach (HtmlAgilityPack.HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
                {
                    string hrefValue = link.GetAttributeValue("href", string.Empty);
                    if (hrefValue.Contains("http") && hrefValue.Contains("attachment"))
                        htmls.Add(hrefValue);
                }
                if (htmls.Count > 0 && abovezero == false)
                RealTimeHtmlList();
            }
        }

        bool abovezero = false;
        private void RealTimeHtmlList()
        {
            abovezero = true;
            for (int x = 0; x < htmls.Count; x++)
            {
                test = htmls[x];
                int index = test.IndexOf("amp");
                string test1 = test.Substring(39, index - 25);
                test = test.Remove(39, index - 35);
                int index1 = test.IndexOf("amp");
                if (index1 > 0)
                    test = test.Remove(index1, 4);
                if (!newHtmls.Contains(test))
                {
                    while (true)
                    {
                        if (htmlloaded == true)
                        {
                            newHtmls.Add(test);
                            RealTimeNavigate(test);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
        }

        private void RealTimeNavigate(string tests)
        {

                    uri = test;
                    webBrowser1.Navigate(test);
                    htmlloaded = false;


        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            GetLinks();
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            numberoflinks = e.ProgressPercentage;
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            numberofimages = newHtmls.Count;
            htmlloaded = true;
        }

        private void NavigateToLinks()
        {
            if (countlinks != newHtmls.Count)
            {
                while (true)
                {
                    if (htmlloaded == true)
                    {
                        uri = newHtmls[countlinks];
                        webBrowser1.Navigate(newHtmls[countlinks]);
                        countlinks++;
                        htmlloaded = false;
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }

        int imagescount = 0;
        public FileInfo[] filesinfo;
        public bool savedall = false;
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (e.Url.ToString() == uri)
            {

                Bitmap bmp = new Bitmap(SaveImageFromWebBrowser(webBrowser1));
                bmp = ImageTrim.ImagesTrim(bmp);
                bmp.Save(@"e:\webbrowserimages\Image" + imagescount.ToString() + ".bmp",
                              System.Drawing.Imaging.ImageFormat.Bmp);
                bmp.Dispose();
                imagescount++;
                htmlloaded = true;
                RealTimeHtmlList();
            }
        }


        private void NavigateToSites()
        {
            backgroundWorker1.RunWorkerAsync();
        }

        [DllImport("user32.dll")]
        public static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, uint nFlags);

        private Bitmap SaveImageFromWebBrowser(Control ctl)
        {
            Bitmap bmp = new Bitmap(ctl.ClientRectangle.Width, ctl.ClientRectangle.Height);
            using (Graphics graphics = Graphics.FromImage(bmp))
            {
                IntPtr hDC = graphics.GetHdc();
                try { PrintWindow(ctl.Handle, hDC, (uint)0); }
                finally { graphics.ReleaseHdc(hDC); }
            }
            return bmp;
        }


    }
}

在form1中,我做到了:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;

namespace WebBrowserScreenshots.cs
{
    public partial class Form1 : Form
    {

        private class MyComparer : IComparer<string>
        {
            [DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
            private static extern int StrCmpLogicalW(string x, string y);
            public int Compare(string x, string y)
            {
                return StrCmpLogicalW(x, y);
            }
        }


        public List<string> imageList = new List<string>();
        List<string> numbers = new List<string>();
        WebBrowserScreenshots wbss;

        public Form1()
        {
            InitializeComponent();

            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            this.imageList = Directory.GetFiles(@"e:\webbrowserimages\", "*.bmp").ToList();
            this.imageList.Sort(new MyComparer());

            if (this.imageList.Count > 0)
            {
                hScrollBar1.Minimum = 0;
                hScrollBar1.Value = 0;
                hScrollBar1.Maximum = this.imageList.Count - 1;
                hScrollBar1.SmallChange = 1;
                hScrollBar1.LargeChange = 1;
                pictureBox1.Image = Image.FromFile(this.imageList[0]);
            }
            else
            {
                timer1.Start();
                wbss = new WebBrowserScreenshots();
                wbss.Show();
            }

        }

        FileInfo[] myFile;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (wbss.savedall == true)
            {
                timer1.Stop();
                hScrollBar1.Enabled = true;
                hScrollBar1.Minimum = 0;
                hScrollBar1.Value = 0;
                hScrollBar1.Maximum = wbss.imageList.Count - 1;
                hScrollBar1.SmallChange = 1;
                hScrollBar1.LargeChange = 1;
                pictureBox1.Image = Image.FromFile(wbss.imageList[0]);
            }
            else
            {
                if (wbss.htmlloaded == true)
                {
                    var directory = new DirectoryInfo(@"e:\webbrowserimages\");
                    myFile = directory.GetFiles("*.bmp");
                    if (myFile.Length > 0)
                    {
                        var myFiles = (from f in directory.GetFiles("*.bmp")
                                       orderby f.LastWriteTime descending
                                       select f).First();
                        hScrollBar1.Enabled = true;
                        hScrollBar1.Minimum = 0;
                        hScrollBar1.Value = 0;
                        hScrollBar1.Maximum = 1;
                        hScrollBar1.SmallChange = 1;
                        hScrollBar1.LargeChange = 1;
                        pictureBox1.Image = Image.FromFile(myFiles.Name);
                    }
                }

            }
        }

        private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            int index = (int)hScrollBar1.Value;
            if (pictureBox1.Image != null) pictureBox1.Image.Dispose();
            if (this.imageList.Count > 0)
            {
                pictureBox1.Image = Image.FromFile(this.imageList[index]);
                label1.Text = "Displaying image " + index + " of " + (this.imageList.Count - 1);
            }
            else
            {
                pictureBox1.Image = Image.FromFile(wbss.imageList[index]);
                label1.Text = "Displaying image " + index + " of " + (wbss.imageList.Count - 1);
            }
        }
    }
}

在form1中,我可以选择两种情况。如果我以新形式使用的方式是等待后台工作人员完成然后等待的方式,则它将导航到List newHtmls中的所有链接,然后将所有图像保存在硬盘上,例如2453个图像然后我在form1 pictureBox和hScrollBar中浏览它们。

或者,我现在使用的第二个选项是,一旦将图像以新格式保存到硬盘,那么我将在form1 pictureBox1中显示该图像。然后保存了另一个图像,因此硬盘上现在有两个图像,因此现在显示最后保存的图像。以此类推,一旦保存了图像,就会将其显示在form1 pictureBox上。

只是为了展示它。因此,我将每隔X秒看到一次图像在form1 pictureBox中发生变化。

我现在面临的问题在这部分的Form1中:

if (wbss.htmlloaded == true)
                {
                    var directory = new DirectoryInfo(@"e:\webbrowserimages\");
                    myFile = directory.GetFiles("*.bmp");
                    if (myFile.Length > 0)
                    {
                        var myFiles = (from f in directory.GetFiles("*.bmp")
                                       orderby f.LastWriteTime descending
                                       select f).First();
                        hScrollBar1.Enabled = true;
                        hScrollBar1.Minimum = 0;
                        hScrollBar1.Value = 0;
                        hScrollBar1.Maximum = 1;
                        hScrollBar1.SmallChange = 1;
                        hScrollBar1.LargeChange = 1;
                        pictureBox1.Image = Image.FromFile(myFiles.Name);
                    }
                }

在线上:

pictureBox1.Image = Image.FromFile(myFiles.Name);

FileNotFoundException:Image3.bmp

但是在我的硬盘上,我看到了Image3.bmp

我将尝试缩小一些代码,但是它们都将新表单与form1连接在一起。

a安

您需要使用myFiles.FullNamemyFiles.Name仅具有相对于文件所在目录的路径,不足以找到该文件。FullName包括目录,因此它是文件的完整绝对路径。

对于gasake,请命名您的控件。Form1不是一个好名字。pictureBox1不是一个好名字。甚至变量名也具有误导性-myFile对于文件集合,然后myFiles对于单个文件?GetFiles当您已经有一个清单时,为什么还要再打一次电话myFile为什么还要检查文件长度?为什么不做directory.GetFiles("*.bmp").OrderByDescending(i => i.LastWriteTime).Select(i => i.FullName).FirstOrDefault()呢?

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

我如何获取Form1鼠标进入事件中的鼠标坐标X和Y?

来自分类Dev

如何在pictureBox1边框周围绘制矩形?

来自分类Dev

C#中form1和form2之间的数据传递

来自分类Dev

如何将值从form1中的datagridview1传递到form2中的datagridview2?

来自分类Dev

当PictureBox1命中PictureBox2时添加点

来自分类Dev

如何将图片从PictureBox1复制到Excel?

来自分类Dev

我该如何在鼠标进入事件时将新表单/用户控件移至Form1的中心?

来自分类Dev

我该如何在鼠标进入事件时将新表单/用户控件移至Form1的中心?

来自分类Dev

当backgroundworker在新类中完成其操作时,如何在form1中启用真正的ToolStripMenuItem?

来自分类Dev

在form1中搜索并使用vb.net Windows窗体将其显示到form2

来自分类Dev

Application.Run(新Form1()); 通过命令行参数

来自分类Dev

在form1的文本框中输入的值未传递到form2的标签

来自分类Dev

将新值从 form1 传递给 form2,该值位于数据库中的同一条记录上

来自分类Dev

通过按钮单击将值从 form1 传递到 form2 到 form2

来自分类Dev

关闭 form1 时如何将值从 form1.DataGridView1 传递给 form2.Label1

来自分类Dev

C#从Form1中的静态方法获取现有Form1实例

来自分类Dev

如何在不创建新Form()的情况下从Form2访问Form1函数;

来自分类Dev

我可以在Winforms C#中制作和使用Form1的多个副本吗?

来自分类Dev

如何从form1到form2取值并返回?

来自分类Dev

将数据从form2传递到form1并将其保存在字符串变量中

来自分类Dev

获取checkedCheckList(Form1)的GetItemCheckState(在Form2中)

来自分类Dev

如何在C#Winforms中以编程方式移动Form1中的所有标签

来自分类Dev

如何在Form1 C#中的webapp Directlt中显示输出

来自分类Dev

C#Timer和Form1隐藏或显示

来自分类Dev

通过在form2中的命令来锁定和解锁form1上的文本框

来自分类Dev

简单的PHP Form1

来自分类Dev

从form2中的按钮我想在form1的面板中添加动态按钮。此代码不起作用

来自分类Dev

如何从另一个类在Form1中创建对象?

来自分类Dev

如何在form1中使用Class.cs

Related 相关文章

  1. 1

    我如何获取Form1鼠标进入事件中的鼠标坐标X和Y?

  2. 2

    如何在pictureBox1边框周围绘制矩形?

  3. 3

    C#中form1和form2之间的数据传递

  4. 4

    如何将值从form1中的datagridview1传递到form2中的datagridview2?

  5. 5

    当PictureBox1命中PictureBox2时添加点

  6. 6

    如何将图片从PictureBox1复制到Excel?

  7. 7

    我该如何在鼠标进入事件时将新表单/用户控件移至Form1的中心?

  8. 8

    我该如何在鼠标进入事件时将新表单/用户控件移至Form1的中心?

  9. 9

    当backgroundworker在新类中完成其操作时,如何在form1中启用真正的ToolStripMenuItem?

  10. 10

    在form1中搜索并使用vb.net Windows窗体将其显示到form2

  11. 11

    Application.Run(新Form1()); 通过命令行参数

  12. 12

    在form1的文本框中输入的值未传递到form2的标签

  13. 13

    将新值从 form1 传递给 form2,该值位于数据库中的同一条记录上

  14. 14

    通过按钮单击将值从 form1 传递到 form2 到 form2

  15. 15

    关闭 form1 时如何将值从 form1.DataGridView1 传递给 form2.Label1

  16. 16

    C#从Form1中的静态方法获取现有Form1实例

  17. 17

    如何在不创建新Form()的情况下从Form2访问Form1函数;

  18. 18

    我可以在Winforms C#中制作和使用Form1的多个副本吗?

  19. 19

    如何从form1到form2取值并返回?

  20. 20

    将数据从form2传递到form1并将其保存在字符串变量中

  21. 21

    获取checkedCheckList(Form1)的GetItemCheckState(在Form2中)

  22. 22

    如何在C#Winforms中以编程方式移动Form1中的所有标签

  23. 23

    如何在Form1 C#中的webapp Directlt中显示输出

  24. 24

    C#Timer和Form1隐藏或显示

  25. 25

    通过在form2中的命令来锁定和解锁form1上的文本框

  26. 26

    简单的PHP Form1

  27. 27

    从form2中的按钮我想在form1的面板中添加动态按钮。此代码不起作用

  28. 28

    如何从另一个类在Form1中创建对象?

  29. 29

    如何在form1中使用Class.cs

热门标签

归档