schangxiang@126.com
2025-08-13 a97a624c1fb269a059f97629076433c46b4e8b4c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing.Imaging;
using ZXing.Common;
using ZXing;
using System.Drawing;
using Gma.QrCodeNet.Encoding;
using Gma.QrCodeNet.Encoding.Windows.Render;
using System.IO;
 
namespace autuPrint.printService
{
    class QrCodeAndBarCode
    {
        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="qr">二维码字符串内容</param>
        /// <returns>成功返回二维码图片,失败返回空</returns>
        public static Image printQr(string Qr)
        {
            try
            {
                QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
                QrCode qrCode = new QrCode();
                qrEncoder.TryEncode(Qr, out qrCode);
                GraphicsRenderer renderer = new GraphicsRenderer(new FixedModuleSize(5, QuietZoneModules.Two), Brushes.Black, Brushes.White);
 
                using (MemoryStream ms = new MemoryStream())
                {
                    renderer.WriteToStream(qrCode.Matrix, ImageFormat.Png, ms);
                    Image img = Image.FromStream(ms);
                    //img.Save("E:/二维码.png");
                    return img;
                }
            }
            catch (Exception)
            {
                return null;
            }
 
 
        }
        /// <summary>
        /// 生产条形码
        /// </summary>
        /// <param name="Br">条形码内容</param>
        /// <returns>成功返回图片</returns>
        public static Image printBr(string Br)
        {
            try
            {
                EncodingOptions encodeOption = new EncodingOptions();
                encodeOption.Height = 60; // 必须制定高度、宽度
                encodeOption.Width = 240;
                encodeOption.PureBarcode = true;
                // 2.生成条形码图片并保存
                ZXing.BarcodeWriter wr = new BarcodeWriter();
                wr.Options = encodeOption;
                //  wr.Format = BarcodeFormat.CODE_39;  // 这里可以设定条码的标准
                wr.Format = BarcodeFormat.CODE_128;
                Bitmap img = wr.Write(Br);
                return img;
            }
            catch (Exception)
            {
                return null;
            }
 
 
        }
 
    }
}