110,500
社区成员
发帖
与我相关
我的任务
分享
internal class UserControl:Control
{
public UserControl()
{
System.Windows.Forms.Label label1;
label1=new Label();
label1.AutoSize = true;
label1.Location = new System.Drawing.Point(22, 22);
label1.Name = "label1";
label1.Size = new System.Drawing.Size(65, 12);
label1.TabIndex = 0;
label1.Text = "我是猪头哦";
this.ClientSize = new System.Drawing.Size(300, 300);
this.Controls.Add(label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
}
var bitmap = new Bitmap(300, 300);
var userControl = new UserControl();
userControl.DrawToBitmap(bitmap, new Rectangle(0, 0, 300, 300));
bitmap.Save("d:\\temp.bmp", ImageFormat.Bmp);
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Form2 frm2;
Bitmap bmp;
//i,j分别是标题和边框导致的控件Location的偏移
int i;
int j;
private void Form1_Load(object sender, EventArgs e)
{
frm2 = new Form2();
bmp =new Bitmap(frm2.Width, frm2.Height);
i = frm2.Width - frm2.ClientSize.Width;
j = frm2.Height - frm2.ClientSize.Height;
frm2.DrawToBitmap(bmp, new Rectangle(0, 0, frm2.Width, frm2.Height));
Draw(frm2,frm2);
bmp.Save("e:\\123.bmp");
bmp.Dispose();
}
void Draw(Control ctrl,Form frm)
{
foreach (Control c in ctrl.Controls)
{
if (ctrl.Parent != frm)
{
c.DrawToBitmap(bmp, new Rectangle(c.Location.X + i, c.Location.Y + j, c.Width, c.Height));
}
else
{
c.DrawToBitmap(bmp, new Rectangle(c.Location.X+ctrl.Location.X+i , c.Location.Y+ctrl.Location.Y +j, c.Width, c.Height));
}
Draw(c,frm);
}
}
}
}
AForm f = new AForm(property);
Bitmap bm = new Bitmap(1024, 768);
f.DrawToBitmap(bm, new Rectangle(0, 0, 1024, 768));
bm.Save(@"d:\\test.png");