Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从新表单中实时传递和更新表单1中的pictureBox1?_C#_.net_Winforms - Fatal编程技术网

C# 如何从新表单中实时传递和更新表单1中的pictureBox1?

C# 如何从新表单中实时传递和更新表单1中的pictureBox1?,c#,.net,winforms,C#,.net,Winforms,我向项目中添加了一个新表单,该表单包含在designer webBrowser控件中。我所做的是从html解析一些链接,然后导航到每个链接,然后截图并将我在webBrowser中导航到的每个图像保存到硬盘 最后,当所有的链接都被导航,并且我在硬盘上有了这些图像,我就用一个hScrollBar把它们显示在Form1 pictureBox上。 但是现在,我要等待它以新的形式完成,然后显示所有图像,我想在form1的pictureBox中显示硬盘上保存的每个图像 using System; using

我向项目中添加了一个新表单,该表单包含在designer 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;
        }


    }
}
在线:

pictureBox1.Image = Image.FromFile(myFiles.Name);
FileNotFoundException:Image3.bmp

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


我将尝试缩小一些代码的范围,但所有代码都与新表单与form1连接。

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

对于gasake,请命名您的控件
Form1
不是一个好名字
pictureBox1
不是个好名字。甚至变量名也会产生误导-
myFile
用于文件集合,然后
myFiles
用于单个文件?既然在
myFile
中已有列表,为什么还要再次调用
GetFiles
?为什么还要检查文件长度?为什么不直接执行
directory.GetFiles(“*.bmp”).OrderByDescending(i=>i.LastWriteTime)。选择(i=>i.FullName)。FirstOrDefault()

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);