Autor Beitrag
ThomasB
Hält's aus hier
Beiträge: 2



BeitragVerfasst: Do 25.11.10 08:52 
Seid Gegrüßt!

Ich habe folgendes Problem:
Ich möchte ein BitMap eines UserControls, das vierschiedene Controls enthält, erzeugen. Mit Freude fand ich die DrawToBitMap-Methode. Diese erzeugt auch ein BitMap. Doch leider gibt es bei der Darstellung der Textbox-Controls Probleme. Die werden nicht angezeigt, wenn ich die UserControl-Klasse als Container benutze.
Eine Lösung des Problems ist, anstatt des UserControl ein Form-Control zu benutzen, dieses sichtbar zu machen und die DrawToBitMap Methode aufzurufen. Die BitMap enthält dann alle Controls korrekt dargestellt. Das Problem ist, dass die Form kurz angezeigt werden muss.
Kennt jemand das Problem? Hat jemand dafür eine Lösung? Gehe ich vollkommen falsch an die Sache ran?

Zum Verständnis kurzer PseudoCode:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
public void CreateBitMap()
{
      TextBox textBox = new TextBox();
      textBox.Location = new System.Drawing.Point(00);
      textBox.Name = "textBox1";
      textBox.Size = new System.Drawing.Size(10020);      
      textBox.Text = "ABC";

      Form toDraw = new Form(); // malt die TextBox
      //UserControl toDraw = new UserControl(); // malt die TextBox nicht
      toDraw.Size = new Size(200200);
      toDraw.Controls.Add(textBox);      
      toDraw.Visible = true;

      Graphics myGraphics = toDraw.CreateGraphics();
      Bitmap bitMap = new Bitmap(toDraw.Size.Width, toDraw.Size.Height, myGraphics);      
      toDraw.DrawToBitmap(bitMap, new Rectangle(00, testControl.Width, testControl.Height));

      //...
}

MfG
Thomas.

Moderiert von user profile iconChristian S.: C#-Tags hinzugefügt
ThomasB Threadstarter
Hält's aus hier
Beiträge: 2



BeitragVerfasst: Do 25.11.10 11:34 
Ich hab einen Workaraound gefunden. Ich benutze die Form und das UserControl. Die Form lasse ich minimiert, ohne Icon und ohne Taskeintrag. Das UserControl malt dann alles so wie's sein soll in die BitMap:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
public void CreateBitMap()
{
      TextBox textBox = new TextBox();
      textBox.Location = new System.Drawing.Point(00);
      textBox.Name = "textBox1";
      textBox.Size = new System.Drawing.Size(10020);      
      textBox.Text = "ABC";
      
      UserControl toDraw = new UserControl(); 
      toDraw.Size = new Size(200200);
      toDraw.Controls.Add(textBox);      
      toDraw.Visible = true;

      Form form = new Form(); 
      form.Controls.Add(toDraw);
      form.WindowState = FormWindowState.Minimized;
      form.ShowInTaskbar = false;
      form.ShowIcon = false;
      form.TopMost = false;

      form.Visble = true;

      Graphics myGraphics = toDraw.CreateGraphics();
      Bitmap bitMap = new Bitmap(toDraw.Size.Width, toDraw.Size.Height, myGraphics);      
      toDraw.DrawToBitmap(bitMap, new Rectangle(00, toDraw.Width, toDraw.Height));

      form.Visible = false;

      //...
}


MfG
Thomas.