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

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

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

1、XAML前台代码

  1. <Window x:Class="WPF_GDI_Test.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="MainWindow" Height="350" Width="625">
  5. <Grid>
  6. <Image Name="img"/>
  7. </Grid>
  8. </Window>

2、后台逻辑实现

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Forms;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using System.Drawing;
  16. namespace WPF_GDI_Test
  17. {
  18. /// <summary>
  19. /// MainWindow.xaml 的交互逻辑
  20. /// </summary>
  21. public partial class MainWindow : Window
  22. {
  23. public MainWindow()
  24. {
  25. InitializeComponent();
  26. Drawing();
  27. }
  28. private void Drawing()
  29. {
  30. string path = "C:\\WPF_GDI_Test.png";
  31. string fontFamily = "Microsoft YaHei";
  32. float fontSize = 100;
  33. string str1 = "Microsoft YaHei字体字符串测";
  34. Font font = new Font(fontFamily,fontSize);
  35. Bitmap map1 = new Bitmap(1920, 1080);
  36. Graphics gg = Graphics.FromImage(map1);
  37. SizeF sizeF = gg.MeasureString(str1, new Font(fontFamily, fontSize));
  38. var size = TextRenderer.MeasureText(gg, str1, font, new System.Drawing.Size(0, 0));
  39. FormattedText forma = Get_StrWidth(str1,fontFamily,fontSize);
  40. string strszie = string.Format("W = {0}, H = {1}", forma.Width, forma.Height);
  41. string strszie1 = string.Format("W = {0}, H = {1}", sizeF.Width, sizeF.Height);
  42. string strszie2 = string.Format("W = {0}, H = {1}", size.Width, size.Height);
  43. Bitmap map = new Bitmap(1920, 1080);
  44. Graphics g = Graphics.FromImage(map);
  45. g.PageUnit = GraphicsUnit.Pixel;
  46. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  47. g.Clear(System.Drawing.Color.Black);
  48. g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.Red, 4), 10, 10, 1900, 1060);
  49. g.DrawString(str1, font, System.Drawing.Brushes.Green, 0, 0);
  50. g.DrawString(strszie, font, System.Drawing.Brushes.Yellow, 0, 200);
  51. g.DrawString(strszie1, font, System.Drawing.Brushes.OrangeRed, 0, 400);
  52. g.DrawString(strszie2, font, System.Drawing.Brushes.Yellow, 0, 600);
  53. g.Dispose();
  54. map.Save(path, System.Drawing.Imaging.ImageFormat.Png);
  55. map.Dispose();
  56. BitmpToImageSource(path);
  57. }
  58. private FormattedText Get_StrWidth(string txt, string fontFamily, double fontSize)
  59. {
  60. FormattedText formattedText = new FormattedText(
  61. txt,
  62. System.Globalization.CultureInfo.InvariantCulture,
  63. System.Windows.FlowDirection.LeftToRight,
  64. new Typeface(fontFamily.ToString()),
  65. fontSize,
  66. System.Windows.Media.Brushes.Red
  67. );
  68. return formattedText;
  69. }
  70. private void BitmpToImageSource(string filepath)
  71. {
  72. System.IO.FileStream fs =new System.IO.FileStream(filepath,System.IO.FileMode.Open, System.IO.FileAccess.Read);
  73. byte[] buffer = new byte[fs.Length];
  74. fs.Read(buffer, 0, buffer.Length);
  75. fs.Close();
  76. fs.Dispose();
  77. System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer);
  78. BitmapImage bitmapImage = new BitmapImage();
  79. bitmapImage.BeginInit();
  80. bitmapImage.StreamSource = ms;
  81. bitmapImage.EndInit();
  82. img.Source = bitmapImage;
  83. }
  84. }
  85. }

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. IOS http(上传和下载)

    HttpTool.h #import <Foundation/Foundation.h> typedef void (^HttpToolProgressBlock)(CGFloat pro ...

  2. 洛谷 P4841 城市规划

    构造简单无向图的EGF: \[ G(x)=\sum_{i}^{\infty}2^{\binom{i}{2}}\cdot\frac{x^i}{i!} \] 构造简单无向连通图的EGF: \[ F(x)= ...

  3. 3、Spring Cloud - Eureka(高可用Eureka Server集群)

    在实际的项目中,可能有几十个或者几百个的微服务实例,这时 Eureka Server 承担了非 常高的负载.由于 Eureka Server 在微服务架构中有着举足重轻的作用,所以需要对 Eureka ...

  4. rand7生成rand10,rand1生成rand6,rand2生成rand5(包含了rand2生成rand3)

    这种题要分两步,第一步是“插空儿”,第二步是“筛” 1.rand7生成rand10 只要是10的倍数就好 int rand10() { int num; do{ num = (rand7() - ) ...

  5. OAuth2.0认证和授权机制讲解

    第一章.OAuth2.0 介绍 OAuth认证 OAuth认证是为了做到第三方应用在未获取到用户敏感信息(如:账号密码.用户PIN等)的情况下,能让用户授权予他来访问开放平台(主要访问平台中的资源服务 ...

  6. android之View组件的XML属性集合

    XML属性 相关方法 说明 android:alpha setAlpha(float) 设置该组件的透明度 android:background setBackgroundResource(int) ...

  7. Unity3D-制作火焰效果

    1.插件的准备 随着官网上的迭代更新,连下载按钮都找了好久,今天制作的火焰效果要依赖一个插件,LowPoly Environment Pack 输入网址unity3d.com在Assert Store ...

  8. geomesa hbase geoserver

    在geoserver中配置hbase ln -s /root/hbase/hbase-1.4.8/conf/hbase-site.xml /root/tomcat/apache-tomcat-7.0. ...

  9. 映射Xml文件中的数据到JavaBean中

    使用Java原生的javax.xml.bind包下的JAXBContext将一个Xml文件中的数据映射到一个JavaBean中 import java.io.File; import java.io. ...

  10. Maven 高级应用

    Maven 的高级应用主要体现在 ==依赖==,==聚合==,==继承== * 依赖 就是在当前项目的pom.xml 总引入依赖的坐标 最最经常用到的 <dependencies> < ...