截屏法;
#region 抓屏截图法
//截屏
string strTmp = "";
string picName = "";
//宽,高,左上角X,左上角TOP
int iWidth, iHeight;
Image originalImg;
Graphics gc;
//屏幕宽
iWidth = Screen.PrimaryScreen.Bounds.Width;
//屏幕高
iHeight = Screen.PrimaryScreen.Bounds.Height;
//按照屏幕宽高创建位图
originalImg = new Bitmap(iWidth, iHeight);
//从一个继承自Image类的对象中创建Graphics对象
gc = Graphics.FromImage(originalImg);
//抓屏并拷贝到myimage里
gc.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(iWidth, iHeight));
//保存位图
//picName = System.Environment.CurrentDirectory + "\\TEMP\\" + DateTime.Now.ToString("yyyyMMddhhmmss") + Guid.NewGuid().ToString() + ".jpg";
picName = "C:\\" + Guid.NewGuid().ToString() + ".jpg";
originalImg.Save(picName);
//原图的起始坐标
int srcX, srcY;
for (int i = 0; i < MainPanel.Controls.Count; i++)
{
Image originalImg1 = Image.FromFile(picName);
iWidth = MainPanel.Controls[i].Width;
iHeight = MainPanel.Controls[i].Height;
srcX = MainPanel.Controls[i].PointToScreen(MainPanel.Controls[i].Location).X;
srcY = MainPanel.Controls[i].PointToScreen(MainPanel.Controls[i].Location).Y;
Bitmap partImg = new Bitmap(iWidth, iHeight);
Graphics graphics = Graphics.FromImage(partImg);
//目标位置
Rectangle destRect = new Rectangle(new Point(0, 0), new Size(iWidth, iHeight));
//原图位置(默认从原图中截取的图片大小等于目标图片的大小)
Rectangle origRect = new Rectangle(new Point(srcX, srcY), new Size(iWidth, iHeight));
graphics.DrawImage(originalImg1, destRect, origRect, GraphicsUnit.Pixel);
partImg.Save(ImagePath + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + Guid.NewGuid().ToString() + ".jpg");
//partImg.Save(ImagePath + "\\" + i.ToString()+ ".jpg");
strTmp += MainPanel.Controls[i].Name + ":" + srcX + "," + srcY + "," + iWidth + "," + iHeight + "\n";
}
MessageBox.Show(strTmp);
#endregion
第二种方法:创建panel图片
[System.Runtime.InteropServices.DllImport("gdi32.dll ")]
public static extern long BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
private Bitmap memoryImage;
private void CaptureScreen()
{
Graphics mygraphics = this.panel1.CreateGraphics();//创建的是整个panel
Size s = this.panel1.Size;//取panel大小
memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
IntPtr dc1 = mygraphics.GetHdc();
IntPtr dc2 = memoryGraphics.GetHdc();
BitBlt(dc2, 0, 0, this.panel1.ClientRectangle.Width, this.panel1.ClientRectangle.Height, dc1, 0, 0, 13369376);
mygraphics.ReleaseHdc(dc1);
memoryGraphics.ReleaseHdc(dc2);
}
然后调用
void pd_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 0, 0);
}
第三种:
int w, h;
if (this.panel1.VerticalScroll.Visible)
h = this.panel1.VerticalScroll.Maximum;
else
h = this.panel1.Height;
if (this.panel1.HorizontalScroll.Visible)
w = this.panel1.HorizontalScroll.Maximum;
else
w = this.panel1.Width;
Image m = new Bitmap(w, h);
Graphics g = Graphics.FromImage(m);
g.Clear(this.panel1.BackColor);
foreach (Control c in this.panel1.Controls)
{
Bitmap b=new Bitmap(c.Width,c.Height);
c.DrawToBitmap(b, new Rectangle(new Point(0, 0), c.Size));
g.DrawImage(b,c.Location);
}
m.Save("E:\\aa.jpg");