原文:WPF GDI+字符串绘制成图片(一)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/BYH371256/article/details/83410269

本章讲述:在WPF中,使用GDI+技术,把字符串数据,根据文本字体样式,大小绘制成字符串图片;

1、XAML前台代码

<Window x:Class="WPF_GDI_Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="625">
<Grid>
<Image Name="img"/>
</Grid>
</Window>

2、后台逻辑实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing; namespace WPF_GDI_Test
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Drawing();
} private void Drawing()
{
string path = "C:\\WPF_GDI_Test.png";
string fontFamily = "Microsoft YaHei";
float fontSize = 100;
string str1 = "Microsoft YaHei字体字符串测";
Font font = new Font(fontFamily,fontSize); Bitmap map1 = new Bitmap(1920, 1080);
Graphics gg = Graphics.FromImage(map1);
SizeF sizeF = gg.MeasureString(str1, new Font(fontFamily, fontSize));
var size = TextRenderer.MeasureText(gg, str1, font, new System.Drawing.Size(0, 0)); FormattedText forma = Get_StrWidth(str1,fontFamily,fontSize);
string strszie = string.Format("W = {0}, H = {1}", forma.Width, forma.Height);
string strszie1 = string.Format("W = {0}, H = {1}", sizeF.Width, sizeF.Height);
string strszie2 = string.Format("W = {0}, H = {1}", size.Width, size.Height); Bitmap map = new Bitmap(1920, 1080);
Graphics g = Graphics.FromImage(map);
g.PageUnit = GraphicsUnit.Pixel;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(System.Drawing.Color.Black);
g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.Red, 4), 10, 10, 1900, 1060); g.DrawString(str1, font, System.Drawing.Brushes.Green, 0, 0);
g.DrawString(strszie, font, System.Drawing.Brushes.Yellow, 0, 200);
g.DrawString(strszie1, font, System.Drawing.Brushes.OrangeRed, 0, 400);
g.DrawString(strszie2, font, System.Drawing.Brushes.Yellow, 0, 600); g.Dispose();
map.Save(path, System.Drawing.Imaging.ImageFormat.Png);
map.Dispose(); BitmpToImageSource(path);
} private FormattedText Get_StrWidth(string txt, string fontFamily, double fontSize)
{
FormattedText formattedText = new FormattedText(
txt,
System.Globalization.CultureInfo.InvariantCulture,
System.Windows.FlowDirection.LeftToRight,
new Typeface(fontFamily.ToString()),
fontSize,
System.Windows.Media.Brushes.Red
); return formattedText;
} private void BitmpToImageSource(string filepath)
{
System.IO.FileStream fs =new System.IO.FileStream(filepath,System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
fs.Dispose(); System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = ms;
bitmapImage.EndInit(); img.Source = bitmapImage;
}
}
}

3、效果图:

 

WPF GDI+字符串绘制成图片(一)的更多相关文章

  1. WPF GDI+字符串绘制成图片(二)

    原文:WPF GDI+字符串绘制成图片(二) 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/BYH371256/article/details/83 ...

  2. C# 在网页中将Base64编码的字符串显示成图片

    在写一个接口,返回的json里面有图片,是Base64编码的字符串. 测试接口的时候,发现原来在html显示,是直接可以将Base64编码的字符串显示成图片的. 格式如下: <img src=d ...

  3. Gson字符串编码,字符串转换成图片保存,二进制转换成图片保存

    import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.File; import ...

  4. base64字符串转化成图片

    package com.dhht.wechat.util; import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder; import ja ...

  5. c# 图片转二进制/字符串 二进制/字符串反转成图片

    protected void Button1_Click(object sender, EventArgs e) { //图片转二进制 byte[] imageByte = GetPictureDat ...

  6. C# Base64字符串转换成图片及图片转换为Base64

    最近有朋友经常会问我一些问题,例如,如何把一个字符串转换成base64字符串,如何把一个二进制文件转换成Base64文件,以及如何转换回原有的文件,在此我把方法写一下   字符串与Base64相互转换 ...

  7. python 将base64字符串还原成图片保存

    import os,base64 strs='''/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAoHBwgHBgoICAgLCgoLDhgQDg0NDh0VFhEYIx8lJCI ...

  8. base64转换成图片

    前端代码JS: 前端图片为canvsa绘图转base64格式 function putTextInfo() { var canvasImg = painting.canvas.toDataURL('i ...

  9. java 后台将base64字符串保存为图片

    直接上代码: import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; impo ...

随机推荐

  1. 001Java输入、eclipse快捷键

    内容:Java实现键盘输入,eclipse常用快捷键 ######################################################################### ...

  2. eclipse 检测App的内存占用和泄漏【转载】

    前段时间开发的Android应用,每次都是在运行了半个小时左右后突然挂掉了,很是莫名其妙,也不知道哪里出了问题,后来一步步排查,发现问题出在JNI层,一个被频繁调用的函数分配的内存忘记释放,导致内存泄 ...

  3. 使用python 操作liunx的svn,方案二

    在对liunx操作svn的方式,做了改动,使用python的,subprocess进行操作 在第一种方案中,我使用了先拉到本地,然后再创建,在进行上传,实际在svn中可以直接创建文件,并进行文件复制, ...

  4. Kali-linux使用SET实施攻击

    前面介绍了社会工程学工具包(SET)的简单使用.为了能帮助用户更容易的理解社会工程学的强大功能.本节将介绍使用社会工程学工具包实施各种攻击. 7.4.1 针对性钓鱼攻击向量 针对性钓鱼攻击向量通过构造 ...

  5. [转]对象映射类AutoMapper的使用

    由于原文太长了,此处就直接贴上原文的超链接,大家自行学习. codeproject中的一篇文章: AutoMapper

  6. Android自定义控件——有弹性的ListView,ScrollView

    上一次我们试验了有弹性的ScrollView.详情 这一次,我们来试验有弹性的ScrollView. 国际惯例,效果图: 主要代码: [java] view plaincopy import andr ...

  7. 内核调试工具——strace

    简介 strace常用来跟踪进程执行时的系统调用和所接收的信号. 在Linux世界,进程不能直接访问硬件设备,当进程需要访问硬件设备(比如读取磁盘文件,接收网络数据等等)时,必须由用户态模式切换至内核 ...

  8. iOS之禁止所有输入法的表情

    - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSSt ...

  9. ThinkPHP微信扫码支付接口

    最近折腾微信扫码支付,看了微信官方文档,找了很多网页,发现和文档/demo不匹配,现在自己算是弄出来了(文件名称有所更改),贴出来分享一下 一.将有用的官方lib文件和使用的相关文件放置到vendor ...

  10. MySQL8.0.12版本的数据库驱动

    jdbcDriver=com.mysql.cj.jdbc.DriverjdbcUrl=jdbc:mysql://127.0.0.1:3306/jtsys?useUnicode=true&cha ...