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+字符串绘制成图片(一)的更多相关文章
- WPF GDI+字符串绘制成图片(二)
原文:WPF GDI+字符串绘制成图片(二) 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/BYH371256/article/details/83 ...
- C# 在网页中将Base64编码的字符串显示成图片
在写一个接口,返回的json里面有图片,是Base64编码的字符串. 测试接口的时候,发现原来在html显示,是直接可以将Base64编码的字符串显示成图片的. 格式如下: <img src=d ...
- Gson字符串编码,字符串转换成图片保存,二进制转换成图片保存
import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.File; import ...
- base64字符串转化成图片
package com.dhht.wechat.util; import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder; import ja ...
- c# 图片转二进制/字符串 二进制/字符串反转成图片
protected void Button1_Click(object sender, EventArgs e) { //图片转二进制 byte[] imageByte = GetPictureDat ...
- C# Base64字符串转换成图片及图片转换为Base64
最近有朋友经常会问我一些问题,例如,如何把一个字符串转换成base64字符串,如何把一个二进制文件转换成Base64文件,以及如何转换回原有的文件,在此我把方法写一下 字符串与Base64相互转换 ...
- python 将base64字符串还原成图片保存
import os,base64 strs='''/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAoHBwgHBgoICAgLCgoLDhgQDg0NDh0VFhEYIx8lJCI ...
- base64转换成图片
前端代码JS: 前端图片为canvsa绘图转base64格式 function putTextInfo() { var canvasImg = painting.canvas.toDataURL('i ...
- java 后台将base64字符串保存为图片
直接上代码: import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; impo ...
随机推荐
- 9、Node.js Stream(流)
#########################################################################介绍Node.js Stream(流)Stream 是 ...
- ubuntu命令安装jdk
1.ubuntu使用的是openjdk,所以我们需要先找到合适的jdk版本.在命令行中输入命令: $apt-cache search openjdk 1 返回结果列表(因个人电脑而有所不同): def ...
- Java HttpURLConnection模拟请求Rest接口解决中文乱码问题
转自:http://blog.csdn.net/hwj3747/article/details/53635539 在Java使用HttpURLConnection请求rest接口的时候出现了POST请 ...
- Fedora Server 上配置 MariaDb 集群
下载与安装 MariaDB Galera Cluster 10.1之前的版本安装,输入以下命令进行安装: sudo dnf install mariadb-galera-server 如果电脑上还没安 ...
- 定义一个类Point,代表一个点,public属性有x和y,方法有显示点坐标 show(),构造函数有两个参数分别给x,y赋值,在main方法中构造两个对象,再创建一方法(getMiddle)为取两个点构成线段的中点的坐标,参数为2个点对象,调用此方法后得到一个新的点,编写Application,显示该对象的坐标值。
这个题让我更加明白了类创建对象的实质 代码中用到:1.对象作形参;2.对象作返回值 以下用代码具体分析: class Point1{ public double x; public double y; ...
- PAT——1043. 输出PATest
给定一个长度不超过10000的.仅由英文字母构成的字符串.请将字符重新调整顺序,按“PATestPATest....”这样的顺序输出,并忽略其它字符.当然,六种字符的个数不一定是一样多的,若某种字符已 ...
- [转载]WebDriver工作原理
转载自:https://www.cnblogs.com/testermark/p/3546287.html WebDriver的工作原理: 在我们new一个WebDriver的过程中,Seleniu ...
- 一个实现 手机端“输入验证码 ”效果Demo
之前在“掘金”上看到这样一个demo 我觉得很有意思,于是今天把它搬下来,记在自己的“小本本”里也许会对以后的项目有点用,若要自己去实现这样一个案例也能实现,但是可能没有那么“妙”. 想法: 1.使用 ...
- ./redis-trib.rb [ERR] Sorry, can't connect to node 192.168.*.*
原因在于在redis.conf中绑定了127.0.0.1 改为自己虚拟机地址.重新启动
- eclipse删除的文件如何恢复。
1.如果不小心删除了单个文件或者文件夹. 鼠标点击上级目录 Ctrl+z就可以恢复了. 如果没有恢复,就右击项目,选择Resore from Local History 然后选择你所删除的文件,选择恢 ...