zoukankan      html  css  js  c++  java
  • wpf winform 截图

     wpf 通过下面的截图,标题可能会丢失。

    public void CreateBitmapFromVisual(Window win, string fileName)
            {
                if (win == null || string.IsNullOrEmpty(fileName))
                {
                    return;
                }

                int index = fileName.LastIndexOf(System.IO.Path.DirectorySeparatorChar);
                if (index > 0)
                {
                    string tmpPath = fileName.Substring(0, index);
                    if (!Directory.Exists(tmpPath))
                    {
                        Directory.CreateDirectory(tmpPath);
                    }
                }
              
                RenderTargetBitmap renderTarget = new RenderTargetBitmap((Int32)win.Width, ((Int32)win.Height), 96, 96, PixelFormats.Pbgra32);
                renderTarget.Render(win);
                PngBitmapEncoder bitmapEncoder = new PngBitmapEncoder();
                bitmapEncoder.Frames.Add(BitmapFrame.Create(renderTarget));
                using (Stream stm = File.Create(fileName))
                {
                    bitmapEncoder.Save(stm);
                }
            }

    wpf 通过下面的截图,可能会截的多余或是少 的

     public void CreateBitmapFromVisual(Window win, string fileName)
            {
                if (win == null || string.IsNullOrEmpty(fileName))
                {
                    return;
                }

                int index = fileName.LastIndexOf(System.IO.Path.DirectorySeparatorChar);
                if (index > 0)
                {
                    string tmpPath = fileName.Substring(0, index);
                    if (!Directory.Exists(tmpPath))
                    {
                        Directory.CreateDirectory(tmpPath);
                    }
                }




                System.Drawing.Graphics graphics = System.Drawing.Graphics.FromHwnd(IntPtr.Zero);
                float systemdpi = graphics.DpiX / 96;

                var bitmap = new System.Drawing.Bitmap((int) (win.Width* systemdpi), (int)(win.Height* systemdpi), System.Drawing.Imaging.PixelFormat.Format32bppArgb);



               using (System.Drawing.Graphics memoryGrahics = System.Drawing.Graphics.FromImage(bitmap))
                {
                    memoryGrahics.CopyFromScreen((int)Math.Round(win.Left), (int)Math.Round(win.Top), 0, 0, new System.Drawing.Size((int)(win.Width* systemdpi), (int)(win.Height* systemdpi)), System.Drawing.CopyPixelOperation.SourceCopy);
                }


                bitmap.Save(fileName);            
            }

    原因是 double 转化成 int ,造成位置信息错误,出现截图错误。神奇的 windows

  • 相关阅读:
    Silverlight4 打印 生成文件过大解决
    清理 Visual Studio 工具箱 的冗杂控件(第三方控件卸载不完全)
    SQL2005SP4生成数据库脚本视图依赖顺序错误问题
    SQL Server 2005 企业版没有 Management Studio管理工具
    Excel Reader 轻量级
    开发托管ActiveX或第三方程序托管插件时调试问题解决方法
    Entity Framework 从数据库生成模型丢失数据库文档不完美解决方案
    今天写了个很蛋疼的sql语句
    WinForm 内嵌 Office 文档 解决方案测试(非DSOFRAME 纯C#代码,网上独一份)
    生产者、消费者问题之闹钟
  • 原文地址:https://www.cnblogs.com/tianya/p/wpf_winform.html
Copyright © 2011-2022 走看看