关于保存panal中多个控件成图片


截屏法;
    #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");




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chiasing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值