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

本章讲述:在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、效果图:

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF(Windows Presentation Foundation)是一种用于创建用户界面的技术,而GDI(Graphics Device Interface)是一种用于绘制图形的API。在WPF中,可以使用GDI绘制图形,通过使用GDI绘制图形,我们可以实现更加定制化和高级的图形效果。 在WPF中,可以使用GDI绘制各种类型的图形,如直线、矩形、椭圆、多边形等。通过使用GDI,我们可以设置各种样式和属性,例如线条的颜色、线宽、填充颜色等,以及阴影、渐变等效果。使用GDI绘制图形的过程是通过在WPF中创建一个GDI绘图对象,然后调用其相应的方法和属性来实现绘制。 在使用GDI绘制图形时,需要注意GDI是基于像素的,因此绘制的图形会受到屏幕分辨率的影响。在WPF中,可以使用Transform矩阵类来实现图形的缩放、平移和旋转等操作,以适应不同分辨率的屏幕。 尽管WPF本身提供了丰富的图形绘制功能,但在某些情况下,使用GDI绘制图形可能会更加灵活和高效。例如,如果需要实现一些特殊的效果,如镜像、叠加等,可以使用GDI来实现。此外,如果需要与现有的GDI代码进行交互,使用GDI绘制图形也是一种不错的选择。 总之,使用WPFGDI结合绘制图形,可以实现更加丰富和高级的效果。通过使用GDI绘制图形,我们可以更好地控制图形的样式和属性,并且可以适应不同的分辨率和交互需求。这种组合使用可以使我们在图形绘制方面拥有更大的灵活性和创造力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值