当前位置: 代码迷 >> .NET报表 >> 关于windows forms保存图像的有关问题
  详细解决方案

关于windows forms保存图像的有关问题

热度:280   发布时间:2016-05-05 01:37:18.0
关于windows forms保存图像的问题。
有没有人知道怎么将窗体保存成图片?整个窗体或部分窗体都行。
------解决思路----------------------
用windows的api
http://blog.csdn.net/zicheng_lin/article/details/7009010
------解决思路----------------------
http://www.cnblogs.com/liuxianan/archive/2012/07/29/2613755.html
------解决思路----------------------


 public static Bitmap GetHandler(IntPtr hand)
        {
            var handle = hand;
            if (handle == IntPtr.Zero)
            {
                throw new Exception("进程没有主窗体Handle");
            }

            //判断Handle是否是Window,不是的话,尝试获取Window Handle。
            if (!NativeMethods.IsWindow(handle))
            {
                handle = NativeMethods.GetWindowDC(handle);
            }

            if (!NativeMethods.IsWindowVisible(handle))
            {
                throw new Exception("窗体没有被显示");
            }

            if (NativeMethods.IsIconic(handle))
            {
                return null;
                throw new Exception("窗体不能被最小化");
            }

            //获取窗体在屏幕中的区域
            NativeMethods.RECT rect;
            if (!NativeMethods.GetWindowRect(handle, out rect))
            {
                throw new Exception("GetWindowRect调用失败");
            }

            //设置大小和坐标
            var width = rect.Right - rect.Left;
            var height = rect.Bottom - rect.Top;

            var x = 0;
            var y = 0;

            //修改自:http://stackoverflow.com/questions/3072349/capture-screenshot-including-semitransparent-windows-in-net
            var hDesk = handle;
            var hSrce = NativeMethods.GetWindowDC(hDesk);//NativeMethods.GetDC(hDesk)
            var hDest = NativeMethods.CreateCompatibleDC(hSrce);
            var hBmp = NativeMethods.CreateCompatibleBitmap(hSrce, width, height);
            var hOldBmp = NativeMethods.SelectObject(hDest, hBmp);
            var b = NativeMethods.BitBlt(hDest, 0, 0, width, height, hSrce, x, y, System.Drawing.CopyPixelOperation.SourceCopy);
            var bmp = System.Drawing.Bitmap.FromHbitmap(hBmp);
            NativeMethods.SelectObject(hDest, hOldBmp);
            NativeMethods.DeleteObject(hBmp);
            NativeMethods.DeleteDC(hDest);
            NativeMethods.ReleaseDC(hDesk, hSrce);

            return bmp;
        }



class NativeMethods
    {
        //http://stackoverflow.com/questions/3072349/capture-screenshot-including-semitransparent-windows-in-net
        [DllImport("gdi32.dll")]
  相关解决方案