本章讲述:在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="525">
<StackPanel>
<Border Margin="1" BorderThickness="1" BorderBrush="Gray">
<StackPanel Orientation="Horizontal" VerticalAlignment="Top">
<Button Margin="10" Content="左对齐" Height="25" Width="50" Click="Button_Click"/>
<Button Margin="10" Content="居中对齐" Height="25" Width="60" Click="Button_Click_1"/>
<Button Margin="10" Content="右对齐" Height="25" Width="50" Click="Button_Click_2"/>
</StackPanel>
</Border>
<Border Margin="1" BorderThickness="1" BorderBrush="Gray">
<Image x:Name="img" />
</Border>
</StackPanel>
</Window>
2、后台逻辑主要代码
private List<string> SourStrs;
private string path = System.Windows.Forms.Application.StartupPath + "\\11.png";
private int mapwidth = 1920;
private int mapheight = 1080;
private float fontSize;
private string fontFamily;
public MainWindow()
{
InitializeComponent();
Drawing();
}
private void Drawing()
{
fontFamily = "Microsoft YaHei";
fontSize = 100f;
string str1 = "一个实用的C#字符串操作类,内置了14个字符串处理函数,几乎囊括了所有常用到的字符串处理操作,比如转换字符串,获取指定字符分";
int strlen = str1.Length;
int num = Encoding.Default.GetBytes(str1).Length;
Font font = new Font(fontFamily, fontSize);
this.DrawingTextImage(str1, font, 1920, 1080);
}
private void DrawingTextImage(string txt, Font font, int w, int h)
{
int strlen = txt.Length;
int num = Encoding.Default.GetBytes(txt).Length;
Bitmap map = new Bitmap(1920, 1080);
Graphics g = Graphics.FromImage(map);
g.PageUnit = GraphicsUnit.Pixel;
g.SmoothingMode = SmoothingMode.HighQuality;
g.Clear(Color.Black);
g.DrawRectangle(new Pen(Color.Red, 4f), 10, 10, 0x76c, 0x424);
SizeF size1 = this.Get_StrWidth1(g, "字", font);
SizeF size2 = this.Get_StrWidth1(g, "H", font);
int n = (int)(((float)w) / size2.Width);
char[] txtSour = txt.ToArray<char>();
List<string> bkstrs = this.GetStrList(g, font, txt, 1080);
this.SourStrs = bkstrs;
int row = 0;
foreach (string tmp in bkstrs)
{
g.DrawString(tmp, font, Brushes.OrangeRed, (float)0f, (float)(row * size2.Height));
row++;
}
g.Dispose();
map.Save(this.path, System.Drawing.Imaging.ImageFormat.Png);
map.Dispose();
this.BitmpToImageSource(this.path);
}
//居左、居中、居右显示
private void DrawStringBitmap(int mode)
{
Bitmap map = new Bitmap(1920, 1080);
Graphics g = Graphics.FromImage(map);
g.PageUnit = GraphicsUnit.Pixel;
g.SmoothingMode = SmoothingMode.HighQuality;
g.Clear(Color.Black);
g.DrawRectangle(new Pen(Color.Red, 4f), 10, 10, 0x76c, 0x424);
Font font = new Font(this.fontFamily, this.fontSize);
float row = 0f;
float tmph = this.Get_StrWidth1(g, "H", font).Height;
foreach (string tmp in this.SourStrs)
{
SizeF size = this.Get_StrWidth1(g, tmp, font);
float x = 0f;
switch (mode)
{
case 0:
x = 0f;
break;
case 1:
x = (this.mapwidth - size.Width) / 2f;
break;
case 2:
x = this.mapwidth - size.Width;
break;
}
g.DrawString(tmp, font, Brushes.OrangeRed, x, row * size.Height);
row++;
}
g.Dispose();
map.Save(this.path, ImageFormat.Png);
map.Dispose();
this.BitmpToImageSource(this.path);
}
private double Get_StrWidth1(Graphics g, char txt, Font font)
{
return (double)g.MeasureString(txt.ToString(), font).Width;
}
private double Get_StrWidth2(Graphics g, string txt, Font font)
{
return (double)TextRenderer.MeasureText(g, txt, font, new System.Drawing.Size(0, 0)).Width;
}
private SizeF Get_StrWidth1(Graphics g, string txt, Font font)
{
return TextRenderer.MeasureText(g, txt, font, new System.Drawing.Size(0, 0));
}
public static System.Windows.Media.FormattedText Get_StrWidth(string txt, double fontSize, string fontFamily)
{
System.Windows.Media.FormattedText formattedText = new System.Windows.Media.FormattedText(
txt,
System.Globalization.CultureInfo.InvariantCulture,
System.Windows.FlowDirection.LeftToRight,
new System.Windows.Media.Typeface(fontFamily.ToString()),
fontSize,
System.Windows.Media.Brushes.Black
);
return formattedText;
}
3、输入字符串处理逻辑
private int GetRowIndex(Graphics g, Font font, int ridex, string txt, int n, out string otxt, bool isend)
{
int len = n;
string tmptxt = txt.Substring(ridex, n);
double txtw = 0.0;
double maxw = 1920.0;
double tmp = 0.0;
do
{
tmp = this.Get_StrWidth1(g, tmptxt, font).Width;
txtw = tmp;
if (isend)
{
break;
}
if (txtw < maxw)
{
tmptxt = txt.Substring(ridex, ++len);
}
else
{
len--;
tmptxt = tmptxt.Substring(0, len);
}
}
while (txtw < maxw);
otxt = tmptxt;
return len;
}
private List<string> GetStrList(Graphics g, Font font, string txt, int maxh)
{
List<string> bk = new List<string>();
string tmptxt = null;
int idex = 0;
double tmph = 0.0;
double w = 1920.0;
SizeF size2 = this.Get_StrWidth1(g, "H", font);
int n = (int)(w / ((double)size2.Width));
int row = 0;
bool isend = false;
do
{
if ((idex + n) > txt.Length)
{
n = txt.Length - idex;
isend = true;
}
idex += this.GetRowIndex(g, font, idex, txt, n, out tmptxt, isend);
row++;
tmph = row * size2.Height;
if (maxh > tmph)
{
bk.Add(tmptxt);
}
}
while (tmph < maxh);
return bk;
}
4、保存图片
//保存图片
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);
System.Windows.Media.Imaging.BitmapImage bitmapImage = new System.Windows.Media.Imaging.BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = ms;
bitmapImage.EndInit();
img.Source = bitmapImage;
}
5、按钮响应事件
private void Button_Click(object sender, RoutedEventArgs e)
{
if (this.SourStrs != null)
{
DrawStringBitmap(0);//参数0:居左;1:居中;2:居右
}
}
6、效果图