1. 功能

系统截图。

2. 实现

2.1 思路
  1. 控件继承自 System.Windows.Media.Visual,
  2. 通过 System.Windows.Media.Imaging.RenderVisualToBitmap 把 Visual 对象转换为位图 rtb
  3. 将位图转成编码器接受的一帧,类型为 BitmapFrame :BitmapFrame.Create(rtb)
  4. 将 BitmapFrame 添加到编码器 : encode.Frames.Add(BitmapFrame.Create(rtb))
  5. 将编码器的数据输出到文件 encode.Save(fs)
2.2 代码
class ImageHelper
{
public static RenderTargetBitmap RenderVisualToBitmap(Visual visual, int width, int height)
{
var rtb = new RenderTargetBitmap(width, height, 96d, 96d, PixelFormats.Default);
rtb.Render(visual); return rtb;
}
} class ControlImage
{
public void Get(Visual visual, int width, int height, string PicType)
{
// 1.获取 visual 的控件内容,保存为位图
RenderTargetBitmap rtb = Common.RenderVisualToBitmap(visual, width, height); try
{
string encodeClassName = "System.Windows.Media.Imaging." + PicType + "BitmapEncoder"; // 2.实例化一个图片编码器
BitmapEncoder encode =
(BitmapEncoder)Assembly.LoadFile(Environment.CurrentDirectory + "/PresentationCore.dll").CreateInstance(encodeClassName); //需要复制 dll 到本地 // 3. 把控件的位图转成编码器接受的图像数据。
encode.Frames.Add(BitmapFrame.Create(rtb)); //新建文件
using (Stream fs = File.Create(path))
{
//将位图编码为指定的流
encode.Save(fs);
}
}
catch (Exception e) { }
}
}

WPF 基础 - 图片之界面截图的更多相关文章

  1. WPF基础知识、界面布局及控件Binding(转)

    WPF是和WinForm对应的,而其核心是数据驱动事件,在开发中显示的是UI界面和逻辑关系相分离的一种开放语言.UI界面是在XAML语言环境下开发人员可以进行一些自主设计的前台界面,逻辑关系还是基于c ...

  2. WPF基础知识、界面布局及控件Binding

    WPF是和WinForm对应的,而其核心是数据驱动事件,在开发中显示的是UI界面和逻辑关系相分离的一种开放语言.UI界面是在XAML语言环境下开发人员可以进行一些自主设计的前台界面,逻辑关系还是基于c ...

  3. WPF 基础 - 图片与 base64

    1. base64 转图片 将 base64 转成 byte[] 将 byte[] 作为内存流保存到一个 BitmapImage 实例的流的源 把 BitmapImage 作为目标图片的 Source ...

  4. WPF 自定义图片剪切器 - 头像剪切(扩展与完善、实时截图)

    原文:WPF 自定义图片剪切器 - 头像剪切(扩展与完善.实时截图) 一.说明:上一次写的"WPF 自定义图片剪切器 - 头像剪切.你懂得"存在明显的缺陷,由于篇幅较长.重新写了一 ...

  5. 用WPF窗体打造个性化界面的图片浏览器

    原文:用WPF窗体打造个性化界面的图片浏览器 本文使用WPF窗体(XAML及C#)与Win Form控件(FolderBrowserDialog)结合的方式, 演示制作了一个简易漂亮的WPF图片浏览器 ...

  6. 使用WPF技术模拟手机界面

    原文:使用WPF技术模拟手机界面 1. 前言 WPF(Windows Presentation Foundation),即"Windows呈现基础",它的目的非常明确,就是用来把数 ...

  7. WPF:保存窗口当前状态截图方法

    在制作软件使用手册或者操作示范市,比较常用方式有截图和视频制作.如果软件内置当前状态的截图和操作视频的导出功能,则将极大简化这方面的工作.使用wpf编写的UI界面,截图的导出功能逻辑相对简单,通用的实 ...

  8. iOS开发基础-图片切换(4)之懒加载

    延续:iOS开发基础-图片切换(3),对(3)里面的代码用懒加载进行改善. 一.懒加载基本内容 懒加载(延迟加载):即在需要的时候才加载,修改属性的 getter 方法. 注意:懒加载时一定要先判断该 ...

  9. WPF基础到企业应用系列6——布局全接触

    本文转自:http://knightswarrior.blog.51cto.com/1792698/365351 一. 摘要 首先很高兴这个系列能得到大家的关注和支持,这段时间一直在研究Windows ...

随机推荐

  1. 信号量解决写者优先&读者优先&公平竞争(reader writer)

    先说问题: 这里的rand都是伪随机.解决也很简单,srand即可.内容懒得改了~~ 描述及思路:           代码:           运行结果:   读者优先:           效果 ...

  2. C# 类 (7) - 抽象 Abstract

    Abstract 抽象类,关键字Abstract ,最典型的应用就是在 继承机制里 作为base类,抽象类是不能被实例化的(前面说的static 类也不能被实例化)它必须作为 基类,被别人继承,然后必 ...

  3. uni-app 实战-打包 📦 App

    uni-app 实战-打包 App Android & iOS 证书 广告 refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问 ...

  4. 全球最好 css3 website

    http://www.awwwards.com/ http://www.revolution.pn/ http://www.bestcss.in/ http://www.csswinner.com/ ...

  5. VuePress plugins All In One

    VuePress plugins All In One VuePress & element-ui & docs $ yarn add -D vuepress-plugin-demo- ...

  6. vue 如何重绘父组件,当子组件的宽度变化时候

    vue 如何重绘父组件,当子组件的宽度变化时候 vue & dynamic el-popover position demo https://codepen.io/xgqfrms/pen/wv ...

  7. How to Use RSS on Your Website

    RSS feed 1 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  8. copy-webpack-plugin & ignore folder

    copy-webpack-plugin & ignore folder https://github.com/webpack-contrib/copy-webpack-plugin#ignor ...

  9. js class static property & public class fields & private class fields

    js class static property class static property (public class fields) const log = console.log; class ...

  10. ORM All In One

    ORM All In One ORM Object Relational Mapping https://en.wikipedia.org/wiki/Object-relational_mapping ...