110,539
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace test1
{
public partial class Form1 : Form
{
public class control_Info
{
public Bitmap control_Img;
public Point control_Location;
public Size control_Size;
}
private List<control_Info> _controlList = new List<control_Info>();
private void getControl_Info(Bitmap control_Img, Point control_Location, Size control_Size)
{
_controlList.Add(new control_Info
{
control_Img = control_Img,
control_Location = control_Location,
control_Size = control_Size
});
}
public Form1()
{
InitializeComponent();
}
public static Bitmap RtbToBitmap(int x, int y, RichTextBox rtb)
{
rtb.Update();
Bitmap bmp = new Bitmap(rtb.Width, rtb.Height);
using (Graphics gr = Graphics.FromImage(bmp))
{
gr.CopyFromScreen( new Point (x,y), Point.Empty, rtb.Size);
}
return bmp;
}
private void Form1_Shown(object sender, EventArgs e)
{
foreach (System.Windows.Forms.Control control in this.Controls)
{
if (control is RichTextBox)
{
getControl_Info(RtbToBitmap(Location.X +control.Location.X,Location.Y + control.Location.Y, (RichTextBox)control), control.Location,
control.Size);
}
else
{
Bitmap controlBmp = new Bitmap(control.Width, control.Height);
control.DrawToBitmap(controlBmp, control.ClientRectangle);
if (control is Label)
{
controlBmp.MakeTransparent();
}
getControl_Info(controlBmp, control.Location, control.Size);
}
}
Bitmap block = new Bitmap(400, 400);
Graphics bmp_block = Graphics.FromImage(block);
//全部图片放到一个PictureBox里
for (int i = 0; i < _controlList.Count; i++)
{
bmp_block.DrawImage(_controlList[i].control_Img, _controlList[i].control_Location);
}
PictureBox bp = new PictureBox();
//bp.BackColor = Color.SteelBlue;
bp.BackgroundImage = Image.FromFile("2.jpg");
bp.Size = new Size(300, 300);
bp.Location = new Point(200, 10);
bp.Image = block;
this.Controls.Add(bp);
}
}
}