原文:
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、效果图: