码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 把wpf的窗体保存为png图片


    昨晚在stack  overflow刷问题时看到有这个问题,今天早上刚好来尝试学习一下

    stack overflow的链接如下:

    c# - How to render a WPF UserControl to a bitmap without creating a window - Stack Overflow

    测试步骤如下:

    1  新建.net framework 3.5的wpf窗体程序

    2 默认的窗体的xaml代码如下:

    1. "WpfDemo.MainWindow"
    2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    6. xmlns:local="clr-namespace:WpfDemo"
    7. xmlns:s="clr-namespace:System;assembly=mscorlib"
    8. mc:Ignorable="d"
    9. Title="MainWindow" Height="450" Width="800">
    10. "Left">
    11. 123455
    12. Content="确定">

    窗体效果如下:

    对应的后台代码如下:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.IO;
    4. using System.Linq;
    5. using System.Text;
    6. using System.Windows;
    7. using System.Windows.Controls;
    8. using System.Windows.Data;
    9. using System.Windows.Documents;
    10. using System.Windows.Input;
    11. using System.Windows.Interop;
    12. using System.Windows.Media;
    13. using System.Windows.Media.Imaging;
    14. using System.Windows.Navigation;
    15. using System.Windows.Shapes;
    16. namespace WpfDemo
    17. {
    18. ///
    19. /// MainWindow.xaml 的交互逻辑
    20. ///
    21. public partial class MainWindow : Window
    22. {
    23. public MainWindow()
    24. {
    25. InitializeComponent();
    26. }
    27. private void BtnEnter_Click(object sender, RoutedEventArgs e)
    28. {
    29. RenderControl(this);
    30. MessageBox.Show("保存完成");
    31. }
    32. void RenderControl(Control renderControl)
    33. {
    34. RenderTargetBitmap bmp = new RenderTargetBitmap((int)renderControl.Width, (int)renderControl.Height, 0, 0, PixelFormats.Pbgra32);
    35. bmp.Render(renderControl);
    36. var encoder = new PngBitmapEncoder();
    37. encoder.Frames.Add(BitmapFrame.Create(bmp));
    38. using (Stream stm = File.Create("test.png"))
    39. encoder.Save(stm);
    40. }
    41. }
    42. }

    3  运行程序,点击确定按钮,会在bin/Debug目录下生成MainWindow窗体对应的png图片,png图片名称为test.png,如下图:

  • 相关阅读:
    文献关系的可视化工具
    [go学习笔记.第十章.面向对象编程] 10.面向对象的特性-接口
    Typora、Markdown笔记为文字添加颜色的快捷键设置
    浅谈—IBIS入门理解(二)
    C#上位机系列(3)—定时器和串口的介绍
    腾讯云cvm云硬盘扩容
    【代码随想录算法训练营】第50天 | 第九章 动态规划(十一)+ 复习第20天 第六章 二叉树(六)
    含文档+PPT+源码等]精品基于Uniapp+SSM实现的作业管理app[包运行成功]SSM毕业设计安卓项目源码
    CMake Tutorial 巡礼(7)_打包一个安装文件
    尚硅谷设计模式(二十)备忘录模式
  • 原文地址:https://blog.csdn.net/zxy13826134783/article/details/134259497
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号