惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
N
Netflix TechBlog - Medium
腾讯CDC
C
Check Point Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
S
SegmentFault 最新的问题
F
Fortinet All Blogs
美团技术团队
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
F
Full Disclosure
Recorded Future
Recorded Future
D
DataBreaches.Net
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
J
Java Code Geeks
I
InfoQ
Y
Y Combinator Blog
A
About on SuperTechFans
AI
AI
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
W
WeLiveSecurity
M
MIT News - Artificial intelligence
雷峰网
雷峰网
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Docker
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
V2EX - 技术
V2EX - 技术
Project Zero
Project Zero

博客园 - 天涯

Prism.Interactivity 和 Prism.Modularity 介绍 Prism BindableBase 和 Commands 的介绍 对 mvvm 架构的理解 wpf mvvm 架构 C#硬件开发业务流程调试技巧 wpf Cannot locate resource 'app.xaml' app.config ConfigSection 保护 程序开发注意4项,你知道吗? .net 变量缓存,你知道吗 wpf 多线程 更新UI 界面 wpf treeview 绑定不同的对象 .net 技术,你掌握了多少 aspx 中确认删除 脚本 - 天涯 .net 中对类的扩展 奇怪--ORA-12154:TNS:无法处理服务名: 建模十条原则 由软件加壳谈起 Hashtable 应注意的 2 点 vb 调用c#做的com 组件
wpf winform 截图
天涯 · 2020-05-03 · via 博客园 - 天涯

 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