这是我自己制作的一个窗体form1(使用的是label 和boxlist)
1.想将这个窗体的内容以图片形式或者其他什么办法,copy到form2
2.form2内容可以以图片或者其他形式,选着路径储存在电脑里。(易于打开例如excel,txt,图片等格式)。
初学者,学校作业,请附上代码,或者详细解说。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Q763937
{
public partial class Form1 : Form
{
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool BitBlt
(
IntPtr hdcDest, //目标DC的句柄
int nXDest, //目标DC的矩形区域的左上角的x坐标
int nYDest, //目标DC的矩形区域的左上角的y坐标
int nWidth, //目标DC的句型区域的宽度值
int nHeight, //目标DC的句型区域的高度值
IntPtr hdcSrc, //源DC的句柄
int nXSrc, //源DC的矩形区域的左上角的x坐标
int nYSrc, //源DC的矩形区域的左上角的y坐标
System.Int32 dwRo //光栅的处理数值
);
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public static extern int GetWindowRect(IntPtr hWnd, out Rectangle lpRect);
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public extern static IntPtr GetDC(IntPtr hWnd);
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public extern static int ReleaseDC(IntPtr hWnd, IntPtr hDC);
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool GetClientRect(IntPtr hWnd, out Rectangle lpRect);
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
Rectangle rect;
IntPtr hwnd1 = this.Handle;
//GetWindowRect(hwnd1, out rect); //获得目标窗体的大小
GetClientRect(hwnd1, out rect);
Bitmap frmimg = new Bitmap(rect.Width, rect.Height);
Graphics g1 = Graphics.FromImage(frmimg);
IntPtr hdc1 = GetDC(hwnd1);
IntPtr hdc2 = g1.GetHdc(); //得到Bitmap的DC
BitBlt(hdc2, 0, 0, rect.Width, rect.Height, hdc1, 0, 0, 13369376);
g1.ReleaseHdc(hdc2); //释放掉Bitmap的DC
var f2 = Application.OpenForms["Form2"] as Form2;
if (f2 == null)
f2 = new Form2();
f2.pictureBox1.Image = frmimg;
SaveFileDialog sfd = new SaveFileDialog();
if (sfd.ShowDialog() == DialogResult.OK)
frmimg.Save(sfd.FileName, ImageFormat.Jpeg);
f2.Show();
}
}
}
其实没楼上那么复杂了,想获得本窗体的图片,每个Form都自带了绘制方法
this.DrawToBitmap(b, new Rectangle(new Point(0, 0), b.Size));