Click here to Skip to main content
15,885,904 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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.
C#
  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;
            }
Posted

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
 
Share this answer
 
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[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Jun-15 18:39pm    
Not an answer. Why did you accept it formally?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900