原文: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. 洛谷 P4783 【模板】矩阵求逆

    题目分析 模板题. #include <bits/stdc++.h> using namespace std; typedef long long ll; const int mod=1e ...

  2. unbuntu 14安装 golang

    golang目前有两种编译,一种是golang官方提供的,另外一个是gnu提供的gccgo.这里安装的是Golang,从仓库安装(apt-get)        sudo apt-get instal ...

  3. TensorFlow学习('utf-8' codec can't decode byte 0xff in position 0: invalid start byte)

    使用语句: image_raw_data = tf.gfile.GFile("./picture.jpg", "r").read() 读取图像时报错如下: Un ...

  4. Odoo中报表PDF样式出错的解决办法

    如果发现生成的pdf样式不对,那可能是wkhtmltopdf没办法访问web服务器导致的.wkhtmltopdf进程使用web.base.url作为根url来生成对应文件路径,但这个地址是每次管理员登 ...

  5. 【CSS】iconfont的使用

    说到浏览器对@font-face的兼容问题,这里涉及到一个字体format的问题,因为不同的浏览器对字体格式支持是不一致的,这样大家有必要了解一下,各种版本的浏览器支持什么样的字体,前面也简单带到了有 ...

  6. DPDK中使用VFIO的配置

    VFIO VFIO是一个可以安全地把设备I/O.中断.DMA等暴露到用户空间(userspace),从而可以在用户空间完成设备驱动的框架.用户空间直接设备访问,虚拟机设备分配可以获得更高的IO性能. ...

  7. Altium 技巧 记录

    1.隐藏全部网络,即隐藏全部的飞线,便于布局,在 PCB 编辑器下,选择工具→连接→显示或隐藏全部网络即可 2.元器件非常多时,模块化布局的小技巧,参考:Altium Designer(DXP)小技巧 ...

  8. Struts2框架学习笔记--strtus2初识

    struts2概述: 1.struts2框架应用于javaEE三层结构中的Web层框架 2.struts2框架是在struts1和webwork基础之上发展的全新框架(脱胎换骨 ,用法完全不一样)ps ...

  9. win 10 如何关闭自动更新

    1.右键“此电脑”图标,点击“管理”,打开“计算机管理”窗口; 2.在“计算机管理”窗口中找到“服务和应用程序”,点击“服务”,打开“服务”窗口; 3.在“服务”中找到“Windows Update” ...

  10. 控制台打印九九乘法表(for循环的使用)

    控制台输出九九乘法表 for(int i=1;i<=9;i++){ for(int j = 1; j <= i; j ++) { System.out.print(j+"*&qu ...