zoukankan      html  css  js  c++  java
  • 生成带内嵌图片的二维码

    在博问上看到有同学在问如何实现一个带内嵌图片的二维码,所以准备记录下来,供同学们参考。

    1、首先准备一个用于内嵌的图片。

    2、既然生成二维码码,那肯定需要将什么样的内容生成二维码,这里我用http://www.baidu.com作为生成二维码的字符串

            private string QcodeSource
            {
                get
                {
                    return "http://www.baidu.com";
                }
            }

    3、我们来看看根据QcodeSource生成二维码的方法,这里返回Byte[]。PS:这里用了 Gma.QrCodeNet.Encoding.Net35.dll 生成二维码的库,点击链接下载

            public static byte[] GetQrCodeBitmapImage(string qrcode)
            {
                try
                {
                    if (string.IsNullOrWhiteSpace(qrcode)) return null;
    
                    // Get QrCode GraphicsRenderer
                    var qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
                    var qrCode = qrEncoder.Encode(qrcode);
                    var renderer = new GraphicsRenderer(new FixedModuleSize(24, QuietZoneModules.Four), Brushes.Black, Brushes.White);
    
                    using (var stream = new MemoryStream())
                    {
                        renderer.WriteToStream(qrCode.Matrix, ImageFormat.Jpeg, stream);
                        stream.Seek(0, SeekOrigin.Begin);
                        stream.Flush();
                        return stream.ToArray();
                    }
                }
                catch (Exception)
                {
                    return null;
                }
    
            }

    4、既然上面返回了图片的Byte[],所以接下来我们把准备内嵌的图片和这个二维码图片拼成一个图片,如何将内嵌图片编程Byte[]这里不做熬述。

            /// <summary>  
            /// 调用此函数后使此两种图片合并,类似相册,有个  
            /// 背景图,中间贴自己的目标图片  
            /// </summary>  
            /// <param name="sourceImage">粘贴的源图片</param>  
            /// <param name="destBitmap">粘贴的目标图片</param>  
            public static Bitmap CombinImage(Bitmap sourceImage, Bitmap destBitmap)
            {
                if (destBitmap.Height != 250 || destBitmap.Width != 250)
                {
                    destBitmap = KiResizeImage(destBitmap, 250, 250, 0);
                }
    
                using (var g = Graphics.FromImage(sourceImage))
                {
                    //g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高); 
                    g.DrawImage(sourceImage, 0, 0, sourceImage.Width, sourceImage.Height);
    
                    //g.FillRectangle(System.Drawing.Brushes.White, imgBack.Width / 2 - img.Width / 2 - 1, imgBack.Width / 2 - img.Width / 2 - 1,1,1);//相片四周刷一层黑色边框  
                    g.DrawImage(destBitmap, sourceImage.Width / 2 - destBitmap.Width / 2, sourceImage.Width / 2 - destBitmap.Width / 2, destBitmap.Width, destBitmap.Height);
                    GC.Collect();
                    return sourceImage;
    
                }
            }

    5、下面上效果图

    总结:生成结束,主要用了Gma.QrCodeNet.Encoding.Net35.dll 这个库,代码还是相对来讲比较简单的。

  • 相关阅读:
    TAM实施范例
    xmanager连接到RHEL6.
    TAM安装过程中遇到的问题
    db29.1FP2升级FP12
    WAS常见问题及解答
    在 Lotus Quickr for Domino 环境中使用 Tivoli Access Manager WebSEAL 作为反向代理服务器
    TAM包含的内容全面的指南自IBM
    Setting up the Web Admin Tool in LDAP 6.x to communicate via SSL
    oracle字符集。
    redhat中设置环境变量PATH的方法和只显示目录的Tree
  • 原文地址:https://www.cnblogs.com/yangtongnet/p/3928813.html
Copyright © 2011-2022 走看看