在图形软件中,我们经常使用到“另存为图片”的功能,本文即介绍如何将WPF控件显示内容转换为图片。

例子

保存界面显示内容为图片:

代码:

var bitmapRender = new RenderTargetBitmap((int)MainCanvas.ActualWidth, (int)MainCanvas.ActualHeight, 96, 96, PixelFormats.Pbgra32);
bitmapRender.Render(MainCanvas);
var bmpEncoder = new BmpBitmapEncoder();
bmpEncoder.Frames.Add(BitmapFrame.Create(bitmapRender));
using (var file = File.Create("output.bmp"))
bmpEncoder.Save(file);

转换各种格式的图片

封装出如下函数:

private void GetPicFromControl(FrameworkElement element, String type, String outputPath)
{
//96为显示器DPI
var bitmapRender = new RenderTargetBitmap((int)element.ActualWidth, (int)element.ActualHeight, 96, 96, PixelFormats.Pbgra32);
//控件内容渲染RenderTargetBitmap
bitmapRender.Render(element);
BitmapEncoder encoder = null;
//选取编码器
switch (type.ToUpper())
{
case "BMP":
encoder = new BmpBitmapEncoder();
break;
case "GIF":
encoder = new GifBitmapEncoder();
break;
case "JPEG":
encoder = new JpegBitmapEncoder();
break;
case "PNG":
encoder = new PngBitmapEncoder();
break;
case "TIFF":
encoder = new TiffBitmapEncoder();
break;
default:
break;
}
//对于一般的图片,只有一帧,动态图片是有多帧的。
encoder.Frames.Add(BitmapFrame.Create(bitmapRender));
if (!Directory.Exists(System.IO.Path.GetDirectoryName(outputPath)))
Directory.CreateDirectory(System.IO.Path.GetDirectoryName(outputPath));
using (var file = File.Create(outputPath))
encoder.Save(file);
}

WPF中,控件基本都继承于FrameworkElement,所以,所有的控件都可以直接丢进来,并转换其内容为特定格式的图片。

测试代码XAML部分:

<Window x:Class="BMPGenerator.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<StackPanel>
<Canvas Name="MainCanvas" Background="White" Height="270"/>
<Button Click="Button_Click">PicGenerate</Button>
</StackPanel>
</Window>

测试代码后台部分:

private void Button_Click(object sender, RoutedEventArgs e)
{
GetPicFromControl(MainCanvas, "BMP", @"E:\Tmp\output.BMP");
GetPicFromControl(MainCanvas, "GIF", @"E:\Tmp\output.GIF");
GetPicFromControl(MainCanvas, "JPEG", @"E:\Tmp\output.JPEG");
GetPicFromControl(MainCanvas, "PNG", @"E:\Tmp\output.PNG");
GetPicFromControl(MainCanvas, "TIFF", @"E:\Tmp\output.TIFF");
}

结果:

WPF学习06:转换控件内容为可存储图片的更多相关文章

  1. WPF学习- AllowDrop 用户控件启用拖放功能

    知识点: 创建自定义用户控件(UserControl) 使用户控件成为拖动源 使用户控件成为放置目标 使面板能够接收从用户控件放置的数据 创建项目: 1.新建WPF项目(Wpf-AllowDrop) ...

  2. WPF 入门笔记之控件内容控件

    一.控件类 在WPF中和用户交互的元素,或者说.能够接受焦点,并且接收键盘鼠标输入的元素所有的控件都继承于Control类. 1. 常用属性: 1.1 Foreground:前景画刷/前景色(文本颜色 ...

  3. [WPF 学习] 3.用户控件库使用资源字典的困惑

    项目需要(或者前后端分离的需要),前端我使用了用户控件库,由后端用代码加载和控制. 然而用户控件库没法指定资源字典,于是在用户控件的xaml文件里面手工添加了资源字典 <UserControl. ...

  4. 【WPF】 OxyPlot图表控件学习

    最近在学习OxyPlot图表控件,一些基本的学习心得,在这里记录一下,方便以后进行查找.   一.引用 OxyPlot控件可以直接在VS的 " Nuget " 里面下载   选择: ...

  5. WPF编程,将控件所呈现的内容保存成图像的一种方法。

    原文:WPF编程,将控件所呈现的内容保存成图像的一种方法. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/article/detai ...

  6. WPF打印控件内容

    当我们想打印控件内容时,如一个Grid中的内容,可以用WPF中PrintDialog类的PrintVisual()方法来实现 界面如下: XAML代码如下 <Grid> <Grid. ...

  7. WPF保存包含Winform控件的XAML页面问题

    原文:WPF保存包含Winform控件的XAML页面问题 最近的工作中,用到了WPF调用Winform控件 但是在保存XAML页面的时候发现了问题,就是Winform页面黑黑的,没有任何渲染的波形曲线 ...

  8. WPF使用Winform PDFView控件

    最近开发wpf项目中有一个模块需要显示PDF文件内容.由于WPF本身没有PDF加载控件(似乎有收费的我查到过类似的资料.如果有新的pdf控件也请通知我一下谢谢). 项目使用之前也是从网上获取的资料,因 ...

  9. 在WPF中使用WinForm控件方法

    1.      首先添加对如下两个dll文件的引用:WindowsFormsIntegration.dll,System.Windows.Forms.dll. 2.      在要使用WinForm控 ...

随机推荐

  1. Android开发 SDK NDK下载

    2014.7版本 ADT Bundle http://dl.google.com/android/adt/adt-bundle-windows-x86-20140702.ziphttp://dl.go ...

  2. Git 对象

    Git 提供了很多方法可以方便地访问 Git 库中的对象: ♦ 采用不分的 SHA1 哈希值.不必把 40 位的哈希值写全,只采用开头的部分(4 位以上),只要不与现有的其他哈希值冲突即可. ♦ 使用 ...

  3. cent os 6 安装 nginx

    cent os 6 默认的库是没有nginx的,所以直接 yum install nginx不行. 解决办法: $ wget http://nginx.org/packages/centos/6/no ...

  4. Oracle基础(四) 用户管理

    一.用户 当创建一个数据实例时,Oracle会创建一些默认的数据库用户,如SYS,SYSTEM和SCOTT等用户.SYS和SYSTEM用户都是ORACLE的系统用户.而Scott用户是Oracle数据 ...

  5. C#中Dictionary小记

    使用C#中Dictionary的一下细节小记: 一.Dictionary.Add(key,value) 与 Dictionary[key]=value的区别: 如果Dictionary中已经有了key ...

  6. CF Preparing Olympiad (DFS)

    Preparing Olympiad time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. poj 2029 二维树状数组

    思路:简单树状数组 #include<map> #include<set> #include<cmath> #include<queue> #inclu ...

  8. js验证连续两位数字递增或递减和连续三位数字相同

    <!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...

  9. MongoDB - MongoDB CRUD Operations, Query Documents

    Query Method MongoDB provides the db.collection.find() method to read documents from a collection. T ...

  10. .NET DLL 保护措施详解(四)各操作系统运行情况

    我准备了WEB应用程序及WinForm应用程序,分别在WIN SERVER 2012/2008/2003.Win7/10上实测,以下为实测结果截图: 2012 2008 2003 WIN7 WIN10 ...