1. 程式人生 > >WPF GDI+字符串繪制成圖片(一)

WPF GDI+字符串繪制成圖片(一)

inf lan acc [] isp tails mea summary blog

原文: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+字符串繪制成圖片(一)