weixin_44097963 2019-05-31 01:36 采纳率: 66.7%
浏览 581
已采纳

winform c# 可不可以将某个窗体的所有内容以图片形式显示在另一个窗体?

图片说明
这是我自己制作的一个窗体form1(使用的是label 和boxlist)
1.想将这个窗体的内容以图片形式或者其他什么办法,copy到form2
2.form2内容可以以图片或者其他形式,选着路径储存在电脑里。(易于打开例如excel,txt,图片等格式)。
初学者,学校作业,请附上代码,或者详细解说。

  • 写回答

2条回答 默认 最新

  • threenewbee 2019-05-31 09:40
    关注

    如果问题解决,请点我回答左上角的采纳和向上的箭头,采纳后留下email,把完整代码给你

    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();
            }
        }
    }
    
    

    图片说明

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog