DrawingVisual

using System;
using System.Windows.Media.Imaging;
using Neu.Uasd.HrPrinter.Utility.entity.cardPrint;
using System.Windows.Media;
using System.Windows;
using System.Globalization;
using System.IO;
using System.Drawing.Printing;

namespace Neu.Uasd.HrPrinter.Utility.Tools
{
    public class Class1
    {

        public RenderTargetBitmap CreateImage(EmpCardInfo cardInfo)
        {
            try
            {
                var dv = new DrawingVisual();
                var c = 10;
                var width = 204 * c;
                var height = 323 * c;
                var logoWidth = 124 * c;
                var logoY = 34 * c;
                var logoHeight = 24 * c;
                var photoMargin = 21 * c;
                var photoWidth = 143 * c;
                var photoRadius = 10 * c;
                var nameY = 240 * c;
                var codeY = 272 * c;
                using (var dc = dv.RenderOpen())
                {
                    var rectFrame = new  Rect(0, 0, width, height);
                    dc.DrawRectangle(new SolidColorBrush((Color)ColorConverter.ConvertFromString(cardInfo.Color)), null, rectFrame);

                    var rectLogo = new Rect((width - logoWidth) / 2, logoY, logoWidth, logoHeight);
                    var logo = new BitmapImage();
                    dc.DrawImage(logo, rectLogo);

                    var rectPhoto = new Rect((width - photoWidth) / 2, logoY + logoHeight + photoMargin, photoWidth, photoWidth);
                    var photo = new BitmapImage();
                    if (photo.Width < rectPhoto.Width)
                    {
                        dc.DrawImage(photo, rectPhoto);
                    }
                    else
                    {
                        var curRect = new Int32Rect(0, 0, photoWidth, photoWidth);
                        var stride = photo.Format.BitsPerPixel * photoWidth / 8;
                        byte[] data = new byte[photoWidth * stride];
                        photo.CopyPixels(curRect, data, stride, 0);
                        var curPhoto = BitmapSource.Create(photoWidth, photoWidth, 0, 0, PixelFormats.Bgr32, null, data, stride);
                        dc.DrawImage(curPhoto, rectPhoto);
                    }
                    var pf = new PathFigure()
                    {
                        StartPoint = new Point(rectPhoto.X + photoWidth - photoRadius, rectPhoto.Y),
                        IsClosed = true
                    };
                    pf.Segments.Add(new ArcSegment() { Point = new Point(rectPhoto.X + photoWidth, rectPhoto.Y + photoRadius), Size = new Size(photoRadius, photoRadius), SweepDirection = SweepDirection.Clockwise });
                    pf.Segments.Add(new PolyLineSegment(new Point[] { new Point(rectPhoto.X + photoWidth, rectPhoto.Y + photoRadius), new Point(rectPhoto.X + photoWidth, rectPhoto.Y), pf.StartPoint }, true));
                    var rt = new PathGeometry(new PathFigure[] { pf });
                    dc.DrawGeometry(new SolidColorBrush((Color)ColorConverter.ConvertFromString(cardInfo.Color)), null, rt);

                    var pointName = new Point((width - cardInfo.EmpName.Length * 22 * c) / 2, nameY);
                    var text = new FormattedText(cardInfo.EmpName, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Source Han Sans"), 22 * c, Brushes.White);
                    dc.DrawText(text, pointName);

                    var pointCode = new Point((width - cardInfo.EmpCode.Length * 8.8 * c) / 2, codeY);
                    var code = new FormattedText(cardInfo.EmpCode, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Source Han Sans"), 16 * c, Brushes.White);
                    dc.DrawText(code, pointCode);
                }
                var rtb = new RenderTargetBitmap(width, height, 96d, 96d, PixelFormats.Default);
                rtb.Render(dv);
                return rtb;
            }
            catch (Exception ex)
            {
                 
            }
            return null;
        }


        public void Test()
        {

            var printCard = CreateImage(new EmpCardInfo()
            {
                CompanyLogoFile = @"D:\Logo.png",
                EmpImage = "/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT/wAARCADxALEDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFR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mNsB2JjqO9T3UjzZOdq1SaweQg7iqEevevPnUeyO2lQUXzTHpCs7ZySAe5rSt4ghUYxkVFaxbQMgVZYksBxjHp1rOMVHU1nO+gwvywZfungg0/OVyOncUgjC8E5NS4Pl8d+a0VzBsWiiioIPG9P6N9f61rR9E/H+VFFeJA+nn8SJD9w02X7/4/wBKKKiRC3Im6TfT/Gsuy/4/5fo1FFZPdHWtjb0/7p+tNuvun60UVv0MV8TKn8Iq+P8AXGiivcwhw4n+GWYurfQ1sWfUfQUUV7C3Z5MtjRT+Cpf4TRRWUjnZYi6H6VXuKKK5avwsrDboe/8Aq6Vf9UPp/WiiuGPQ9Ce7JYei/U/zqSX/AFY+tFFbo5J7jXq0Put9KKK06EdCOiiisRH/2Q==",
                EmpName = "xxx",
                EmpCode = "xxxx"
            });

            byte[] data = null;
            using (var ms = new MemoryStream())
            {
                var encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(printCard));
                encoder.Save(ms);
                data = ms.ToArray();
            }


            using (var ms = new MemoryStream(data))
            {
                var decoder = BmpBitmapDecoder.Create(ms, BitmapCreateOptions.None, BitmapCacheOption.None);
                var result = PrintCard((BitmapSource)decoder.Frames[0]);
                if (result != null)
                {
                    //hasError = true;
                    //Dispatcher.Invoke(new Action(() =>
                    //{
                    //    needStop = true;
                    //    textBlockState.Text = "打印错误";
                       
                    //}));
                    //break;
                }
            }
        }


        public string PrintCard(BitmapSource source)
        {
            string printerName =  "Microsoft XPS Document Writer"; 
            var message = string.Empty; 

            
            {
                var bitmap = ConvertFromBitmapSource(source);
                try
                {
                    var pd = new PrintDocument();
                    pd.PrinterSettings.PrinterName = printerName;
                    pd.PrintPage += (sender2, e2) =>
                    {
                        int _cardWidth = e2.PageSettings.PaperSize.Width;
                        int _cardHeight = e2.PageSettings.PaperSize.Height;
                        System.Drawing.Graphics g = e2.Graphics;
                        g.TranslateTransform(0.0F, _cardHeight);
                        g.RotateTransform(-90.0F);
                        g.TranslateTransform(0.0F + 122, 0);
                        e2.Graphics.DrawImage(bitmap, 0, 0, _cardWidth, _cardHeight);
                        e2.HasMorePages = false;
                        Debug("开始绘制完成。");
                    };

                    pd.PrintController = new StandardPrintController();
                    pd.Print();

                    Debug("开始打印。"); 
                    do
                    {
                        Debug("休眠1秒。state:" + state);
                        Thread.Sleep(1000);
                    }
                    while ((state = ph.GetStatus(ref message)) == 2);
                    Debug("打印结束。state:" + state);
                    if (state == 0)
                    {

                        return null;


                    }
                    else
                    {
                        return message;
                    }
                }
                catch (Exception ex)
                {
                   
                    return "打印异常。";
                }
            }
            else
            {
                return message;
            }
        }


        private System.Drawing.Bitmap ConvertFromBitmapSource(BitmapSource bs)
        {
            using (var ms = new MemoryStream())
            {
                var encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(bs));
                encoder.Save(ms);
                return new System.Drawing.Bitmap(ms);
            }
        }
    }
}

 

你可能感兴趣的:(DotNet,wpf,DrawingVisual)