C# 未在Winform.show c上显示的图像#

C# 未在Winform.show c上显示的图像#,c#,C#,我有一个程序,当按下按钮时显示一个“加载”Winform,一旦需要运行的脚本完成,它就会消失 按下按钮时,新表单“出现”,但它不显示任何表单信息,如徽标和标签-仅显示一个空白/灰色框。我试图改变背景颜色和改变图像,但它仍然显示为空白形式 我发现最让人困惑的是,这个空白表单仅在特定的CS出现时才显示为空白。文件在按钮按下时被调用PDFMerge.CombineMultiblePDF。如果我尝试在程序的不同部分中显示加载表单,例如,当按下不同的按钮时,表单将按照计划正确加载所有内容 这里显示的空白表

我有一个程序,当按下按钮时显示一个“加载”Winform,一旦需要运行的脚本完成,它就会消失

按下按钮时,新表单“出现”,但它不显示任何表单信息,如徽标和标签-仅显示一个空白/灰色框。我试图改变背景颜色和改变图像,但它仍然显示为空白形式

我发现最让人困惑的是,这个空白表单仅在特定的CS出现时才显示为空白。文件在按钮按下时被调用PDFMerge.CombineMultiblePDF。如果我尝试在程序的不同部分中显示加载表单,例如,当按下不同的按钮时,表单将按照计划正确加载所有内容

这里显示的空白表单:

以下是显示在不同按钮或不同窗体上的正确窗体

下面是我正在调用的代码,它显示“空白”Winform

     loadingPDF.Show(); // Show the loading form 
     string fileDate = DateTime.Now.ToString("dd-MM-yy");
     string fileTime = DateTime.Now.ToString("HH.mm.ss");
     string outcomeFolder = outputFolder;
     string outputFile = "Combined Folder " + fileDate + " @ " + fileTime + ".pdf";
     string outputFileName = Path.Combine(outcomeFolder, outputFile);

     // combines the file name, output path selected and the yes / no for pagebreaks. 
     PDFMerge.CombineMultiplePDFs(sourceFiles, outputFileName);
     loadingPDF.Hide(); // Hide the loading form 
如果我将PDFMerge.combined替换为不同的intrance CS文件,加载表单将正确显示,这使我相信问题在于PDFMerge以及何时调用它。以下是PDFMerge中使用的代码

public class PDFMerge
{
    public static void CombineMultiplePDFs(String[] fileNames, string outFile)
    {
        try
        {
            int pageOffset = 0;
            int f = 0;

            Document document = null;
            PdfCopy writer = null;

            while (f < fileNames.Length)
            {
                // Create a reader for a certain document
                PdfReader reader = new PdfReader(fileNames[f]);
                reader.ConsolidateNamedDestinations();

                // Retrieve the total number of pages
                int n = reader.NumberOfPages;
                pageOffset += n;
                if (f == 0)
                {
                    // Creation of a document-object
                    document = new Document(reader.GetPageSizeWithRotation(1));

                    // Create a writer that listens to the document
                    writer = new PdfCopy(document, new FileStream(outFile, FileMode.Create));

                    // Open the document
                    document.Open();
                }

                // Add content
                for (int i = 0; i < n;)
                {
                    ++i;
                    if (writer != null)
                    {
                        PdfImportedPage page = writer.GetImportedPage(reader, i);
                        writer.AddPage(page);
                    }
                }
                PRAcroForm form = reader.AcroForm;
                if (form != null && writer != null)
                {
                    //writer.CopyAcroForm(reader);
                    writer.Close();
                }
                f++;
            }

            // Close the document
            if (document != null)
            {
                document.Close();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }
}
在文件选择表单中创建loadingPDF表单的实例

 // Declaring the 'loading' form when files are being combined.
    LoadingPDF loadingPDF = new LoadingPDF();

在这些注释的基础上,PDFMerge.CombineMultiplePDFs()是cpu锁定程序,导致线程在表单完成之前停止加载表单。您可以这样修改代码:

public void ShowLoading()
{
 loadingPDF.Shown += loadingPDF_Shown;
 loadingPDF.Show(); // Show the loading form
}
public void loadingPDF_Shown(object sender, eventargs e)
{
 string fileDate = DateTime.Now.ToString("dd-MM-yy");
 string fileTime = DateTime.Now.ToString("HH.mm.ss");
 string outcomeFolder = outputFolder;
 string outputFile = "Combined Folder " + fileDate + " @ " + fileTime + ".pdf";
 // combines the file name, output path selected and the yes / no for pagebreaks. 
 PDFMerge.CombineMultiplePDFs(sourceFiles, outputFileName);
 loadingPDF.Hide(); // Hide the loading form 
}
public void ShowLoading()
{
 loadingPDF.Show(); // Show the loading form 
 System.ComponentModel.BackgroundWorker worker = new BackgroundWorker();
 worker.DoWork += worker_DoWork;
 worker.RunWorkerCompleted += worker_RunWorkerCompleted;
 worker.RunWorkerAsync(); //Added missed line
}

void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
   //anything you want to do AFTER the cpu-intensive process is done
   loadingPDF.Hide(); // Hide the loading form 
}
public void worker_DoWork(object sender, DoWorkEventArgs e)
{
 string fileDate = DateTime.Now.ToString("dd-MM-yy");
 string fileTime = DateTime.Now.ToString("HH.mm.ss");
 string outcomeFolder = outputFolder;
 string outputFile = "Combined Folder " + fileDate + " @ " + fileTime + ".pdf";
 string outputFileName = Path.Combine(outcomeFolder, outputFile);
 // combines the file name, output path selected and the yes / no for pagebreaks. 
 PDFMerge.CombineMultiplePDFs(sourceFiles, outputFileName);

}
所示为加载表单时要触发的最后一个事件。这应该在启动cpu密集型进程之前加载映像。




另一种方法是将cpu密集型进程放在另一个线程上,以保持UI线程的清晰。您可以这样做:

public void ShowLoading()
{
 loadingPDF.Shown += loadingPDF_Shown;
 loadingPDF.Show(); // Show the loading form
}
public void loadingPDF_Shown(object sender, eventargs e)
{
 string fileDate = DateTime.Now.ToString("dd-MM-yy");
 string fileTime = DateTime.Now.ToString("HH.mm.ss");
 string outcomeFolder = outputFolder;
 string outputFile = "Combined Folder " + fileDate + " @ " + fileTime + ".pdf";
 // combines the file name, output path selected and the yes / no for pagebreaks. 
 PDFMerge.CombineMultiplePDFs(sourceFiles, outputFileName);
 loadingPDF.Hide(); // Hide the loading form 
}
public void ShowLoading()
{
 loadingPDF.Show(); // Show the loading form 
 System.ComponentModel.BackgroundWorker worker = new BackgroundWorker();
 worker.DoWork += worker_DoWork;
 worker.RunWorkerCompleted += worker_RunWorkerCompleted;
 worker.RunWorkerAsync(); //Added missed line
}

void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
   //anything you want to do AFTER the cpu-intensive process is done
   loadingPDF.Hide(); // Hide the loading form 
}
public void worker_DoWork(object sender, DoWorkEventArgs e)
{
 string fileDate = DateTime.Now.ToString("dd-MM-yy");
 string fileTime = DateTime.Now.ToString("HH.mm.ss");
 string outcomeFolder = outputFolder;
 string outputFile = "Combined Folder " + fileDate + " @ " + fileTime + ".pdf";
 string outputFileName = Path.Combine(outcomeFolder, outputFile);
 // combines the file name, output path selected and the yes / no for pagebreaks. 
 PDFMerge.CombineMultiplePDFs(sourceFiles, outputFileName);

}

使用后台工作人员执行此操作将保持UI可用/可点击,而不会使其冻结。除其他外,这还允许动画加载表单。

您需要显示创建和实例化加载PDF变量的代码,以及加载PDF表单的构造函数代码。@kateract-我已添加了有关加载PDF的其他信息。请尝试在合并后注释隐藏。查看联合收割机完成后是否显示图片。如果是,则是在加载图像之前cpu锁定了程序。如果您注释掉PDFMerge.Combine方法并放入Thread.Sleep(5000)中,表单是否无法显示图像?我已尝试注释掉PDFMerge.Combine,但显示的表单仍然不起作用。这包括添加线程。(睡眠)-将显示一个空白表单而不是装饰表单。我已经尝试了您建议的第一种方法。当按下按钮时,显示的表单仍然为空,当我再次按下按钮时,表单显示正确,但是文档无法合并,并且显示的表单不会在显示下一条关于合并文档总量的消息后消失。我将尝试您现在建议的第二个选项。尝试第二个建议的方法后,加载表单现在将正确显示,但仅此而已。显示正确的加载表单,但未能消失。该程序现在也无法合并文档并显示进行的合并总数。在尝试解决此问题时,application.doEvents是否值得继续?我在ShowLoading下的第二个建议中添加了一行我以前遗漏的内容,“worker.RunWorkerAsync();”。这就开始了工人的工作,这应该是所缺少的。谢谢!winform显示/工作正常。我现在将把这段代码应用到我程序的其他部分,因为分解调用的代码后,转换过程似乎更快了。再次感谢你