绘制到位图会返回类库的白框 [英] Draw to bitmap returns a white box for a class library

查看:57
本文介绍了绘制到位图会返回类库的白框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行一个调用win表单的类库来处理一些数据创建图形,然后我想使用表单的drawtobitmap创建表单的位图。



当我运行它时,应用程序运行正常并按预期创建位图。当我使用类库启动它时,只返回一个表格大小的白盒子。



我试过更改z顺序,可见属性,调用和运行表单的各种不同方法,甚至将所有内容升级到.net4.5,但没有似乎有任何影响。



我的代码如下。



来自我父程序的班级电话。

I am trying to run a class library that calls a win form processes some data create graphs and then I want to create a bitmap of the form using drawtobitmap of the form.

When I run it as an application is runs fine and creates the bitmap as expected. when I launch it using the class library is only returns a white box the size of the form.

I have tried changing the z-order, the visible properties, various different ways to call and run the form and even upgrading everything to .net4.5 , but nothing seems to have any affect.

My code is below.

class call from my parent program.

  ProcessBDData.Form1 form1 = new ProcessBDData.Form1();
  form1.Show();
  form1.openFile(args[0]);
  form1.Dispose(); 



public Form1()
       {
           InitializeComponent();

       }


 public bool openFile(string args)
        {
                   
            System.IO.DirectoryInfo DI = new DirectoryInfo(args);
            System.IO.DirectoryInfo[] DInfo = DI.GetDirectories();

            string Variable = "";
            try
            {
                string BDFile = null;

                foreach (System.IO.FileInfo FInfo in DInfo[0].GetFiles())
                {
                    if (FInfo.FullName.EndsWith(".txt")) BDFile = FInfo.FullName;
                }
                
                bool processed = startProcessing(BDFile);

                return true; 
            }
            catch (Exception E)
            {
                StreamWriter SW = new StreamWriter("BD3ToolsErrorlog.txt", true);
                SW.WriteLine("Open File FAILED ");
                SW.WriteLine(E.ToString());
                SW.WriteLine();
                SW.Close();
                return false; 
            }
} 
private bool startProcessing(string BDFile)
        {

            // Process data... 

           
            Bitmap sav = new Bitmap(this.Width, this.Height);
            this.Refresh();
            this.Show();

            //this.DrawToBitmap(sav, new Rectangle(0, 0, this.Width, this.Height));
            this.DrawToBitmap(sav, this.Bounds);
            sav.Save("Original.png");
            sav.Dispose();

            return true
            }
            catch (Exception E)
            {
                StreamWriter SW = new StreamWriter("BDToolsErrorlog.txt", true);
                SW.WriteLine("Error Processing BD Data ");
                SW.WriteLine(E.ToString());                
                SW.WriteLine();
                SW.Close();
                return false;
            }

推荐答案

您的问题不在DLL或EXE中(.NET不会在它们之间产生本质区别,它们只是程序集可执行模块的文件名模式),你的问题只是运行时上下文。



如果你将表单绘制到位图在应用程序中,您的表单可能包含在从 Application.Run(someForm) someForm )开始的应用程序中必须与您的形式相同,但如果您在同一个线程中显示一些新表单,它将包含在相同的UI实例中)。您没有显示代码在显示的代码的第一行中显示的位置;它不包含任何 Application.Run 调用,但我会得出一个结论:这是在你在同一个线程中显示表单之前完成的,因为你报告了这段代码工作。



如果将此代码移动到类库中,我无法确定相同的功能是否有效。其余部分取决于您在引用它的应用程序中如何使用此库。实现它的方法之一:从您的库中公开类 ProcessBDData.Form1 的代码。 (并且永远不要使用自动生成的名称,总是使用VS的重构引擎使用一些语义敏感的名称重命名成员中的所有类型。)使用该库的应用程序程序集也应该使用 System.Windows.Forms 汇编并以 Application.Run(someForm)开头,稍后,在UI内部,从库中显示您的表单。如果您将此表单用作主申请表,它也可以使用。



或者,您可以公开整个 Main 以前是入口点的方法(公开)并从应用程序集引用的 Main 表单中调用此方法你的图书馆。然后它的工作方式与你的库是应用程序时的工作方式相同。



最后,我不知道你为什么这么奇怪地做这件事。您可以创建并显示表单,并立即使用该文件。相反,您应该让应用程序处理所有事件。您可以使用您的文件来响应某些事件,例如按钮点击(我不知道您的目的,所以我不能告诉您该怎么做,我只是发现您想做的事情很奇怪)。此外,在 startProcessing 中不使用 BDFile 参数;为什么称它为开始?等等...



-SA
Your problem is not in the DLL or EXE (.NET doesn't make essential difference between them, they are just the patterns for the file names for the executable modules of the assemblies), your problem is just the runtime context.

If you draw the form to the bitmap in the application, your form is probably included in the application started from Application.Run(someForm) (someForm does not have to be the same form as yours, but if you show some new form in the same thread, it gets included in the same instance of UI). You don't show where the code showed in first lines of your code shown; it does not contain any Application.Run calls, but I would make a conclusion: this is done before you show the form in the same thread, because you reported that this code "works".

I cannot be sure that the same functionality works if you move this code to a class library. The rest of it depends on how you use this library in the application it references it. One of the way to achieve it this: expose the code of the class ProcessBDData.Form1 from you library. (And never use auto-generated names, always rename all types in members using some semantically sensitive names with refactoring engine of VS.) The application assembly using the library should also use System.Windows.Forms assembly and start with Application.Run(someForm), and, later on, insider the UI, show your form from the library. If you use this form as a main application form, it will work, too.

Alternatively, you can expose the whole Main method which used to be the entry point (make it public) and call this method from the Main form of your application assembly referencing your library. Then it will work the same way as when your library was the application.

Finally, I have no idea why are you doing it in such a weird way. You create and show the form and immediately works with the file. Instead, you should let the application to handle all events. You can work with your file in response to some event, such as button click (I have no idea of your purpose, so I cannot tell you what to do, I just find what you are trying to do weird). Also, BDFile argument is not used in startProcessing; and why is it called "start"? And so on…

—SA


我不是代码I的原作者只是继承它,必须保持它的工作。我真的没有时间经历并将其清理到那个级别。



我相信这是导致错误的原因。现在我只需要弄清楚另一种方法来解决它。



https://social.msdn.microsoft.com/Forums/en-US/9d690398-1f91- 4fbd-82fa-4b663c3b558f / kb3057839-has-broken-windows-forms-controldrawtobitmap-when-from-application-initiated-from?forum = windowsgeneraldevelopmentissues [ ^ ]
I am not the original Author of the code I just inherited it and have to keep it working. I dont really have the time to go through and clean it up to that level.

I beleive this is the cause of the error. Now I just have to figure out another method to work around it.

https://social.msdn.microsoft.com/Forums/en-US/9d690398-1f91-4fbd-82fa-4b663c3b558f/kb3057839-has-broken-windows-forms-controldrawtobitmap-when-called-from-application-launched-from?forum=windowsgeneraldevelopmentissues[^]


这篇关于绘制到位图会返回类库的白框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆