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 ...
随机推荐
- UIView 中 hidden、alpha、clear color 与 opaque 的区别
透明度与图层混合相关,影响到图片绘制的效率. hidden 此属性为 BOOL 值,用来表示 UIView 是否隐藏.关于隐藏大家都知道就是让 UIView 不显示而已,但是需要注意的是: 当前 UI ...
- sum函数
>>> np.sum([0.5, 1.5]) 2.0 >>> np.sum([0.5, 0.7, 0.2, 1.5], dtype=np.int32) 1 > ...
- array和matrix
array:数组 matrix:矩阵 list:列表 a = [[1,2,3],[4,5,6]] 两种array的定义方式,第一种方式可以看出list不是array,但却有很大的联系 a = np.a ...
- XCODE7 和IOS9适配后的一些问题
网上比较常规的几个问题就不细说了. 什么HTTPS.bitcode.什么什么的. 记录几个自己又遇到了但是网上没有说的. 启动应用报错, *** Terminating app due to unca ...
- 【转】Java做服务器开发语言
版权声明:本文为博主原创文章,未经博主允许不得转载. 随着游戏市场的兴起,特别是网页游戏.手机游戏的崛起,对游戏开发技术的需求越来越多.网络游戏开发是一个庞大的体系,总体来说是客户端与服务器端.客户端 ...
- HomeKit 开发指南(中文版)
转载自cocoachina 本文由CocoaChina翻译组成员iBenjamin_Go和浅夏@旧时光翻译自苹果开发文档:HomeKit Developer Guide,敬请勘误. 本文档内容包括 第 ...
- CCF认证201803-2 碰撞的小球 java代码实现。
问题描述 数轴上有一条长度为L(L为偶数)的线段,左端点在原点,右端点在坐标L处.有n个不计体积的小球在线段上,开始时所有的小球都处在偶数坐标上,速度方向向右,速度大小为1单位长度每秒. 当小球到达线 ...
- Notes 20180310 : String第二讲_String的声明与创建
1 字符串的声明与创建 学习String的第一步就是创建(声明)字符串,我们在这里之所以分为创建和声明(其实是一个意思,都是创建字符串,但两者却有本质的区别)是因为String是一个很特殊的类,它的 ...
- web前端时间戳转时间类型显示
1.jsp头部加:<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> ...
- Unity 4.7 导出工程在XCode10.1上编译报错
Unity 4.7 导出工程在XCode 10.1上编译报错,而在XCode 9.3上是可以正常编译运行的.原因是Unity4.7所依赖的头文件和库文件在XCode10上没有了,解决办法如下,把XCo ...