• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

微信小程序获取普通二维码

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;
using LitJson;
using System.Drawing;
using System.Drawing.Imaging;

namespace WetChat
{
  public  class Chat
    {
        public string Path;

        public string Width;

        public string Savepath;


        public string Codes;
        public Chat(string savepath,string codes)
        {
            this.Path = "pages/index/index?codes="+ codes;
            this.Savepath = savepath+"qr_"+codes+".png";
            this.Codes = codes;
        }
        /// <summary>
        /// 获取acesstoken
        /// </summary>
        /// <returns></returns>
        public string GetAccess_Token()
        {
            string urls = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=&secret=";
            HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create(urls);  //请求api
            HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
            string respText = "";
            using (Stream resStream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(resStream, Encoding.Default);
                respText = reader.ReadToEnd();
                resStream.Close();
            }
            JavaScriptSerializer Jss = new JavaScriptSerializer();
            Dictionary<string, object> respDic = (Dictionary<string, object>)Jss.DeserializeObject(respText);
            //通过键access_token获取值
            return respDic["access_token"].ToString();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="path"></param>
        /// <param name="width"></param>
        /// <param name="savepath"></param>
        public void GetWxCode()
        {
            string url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=" + GetAccess_Token();
            HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create(url);  //创建url
            request.Method = "POST";
            request.ContentType = "application/json;charset=UTF-8";
            JsonData data = new JsonData();
            data["path"] = this.Path;
            data["width"] = this.Width;

            string json = data.ToJson();
            byte[] load = Encoding.UTF8.GetBytes(json);
            request.ContentLength = load.Length;
            Stream writer = request.GetRequestStream();
            writer.Write(load, 0, load.Length);


            HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
            Stream s = response.GetResponseStream();
            byte[] mg = StreamToBytes(s);
            Image imgs = CaptureImage(mg);
            byte[] mgs = PhotoImageInsert(imgs);
            if (File.Exists(this.Savepath))
            {
                File.Delete(this.Savepath);
                File.WriteAllBytes(this.Savepath, mgs);


            }
            else
            {
             
                File.WriteAllBytes(this.Savepath, mgs);
              


            }
           

        }

        public static byte[] StreamToBytes(Stream stream)
        {

            List<byte> bytes = new List<byte>();
            int temp = stream.ReadByte();
            while (temp != -1)
            {
                bytes.Add((byte)temp);
                temp = stream.ReadByte();
            }
            return bytes.ToArray();

        }

        /// <summary>
        /// 第二部截取:图片转换为文件流
        /// </summary>
        /// <param name="bytes"></param>
        public System.Drawing.Image CaptureImage(byte[] bytes)
        {
            Image formimage = ReturnPhoto(bytes);
            Bitmap bitmap = new Bitmap(440, 440);
            Point p = new Point(0, 0);

            Size s = new Size(455, 455);
            Graphics gra = Graphics.FromImage(bitmap);
            gra.DrawImage(formimage, -15, -15, new Rectangle(p,s), GraphicsUnit.Pixel);
            Image saveimage = Image.FromHbitmap(bitmap.GetHbitmap());

            return saveimage;


        }

        /// <summary>
        /// 第三部:图片转换文件流
        /// </summary>
        /// <param name="imgPhoto"></param>
        /// <returns></returns>
        public byte[] PhotoImageInsert(System.Drawing.Image imgPhoto)
        {
            MemoryStream mstream = new MemoryStream();
            imgPhoto.Save(mstream, System.Drawing.Imaging.ImageFormat.Bmp);
            byte[] byData = new Byte[mstream.Length];
            mstream.Position = 0;
            mstream.Read(byData, 0, byData.Length); mstream.Close();
            return byData;
        }
        /// <summary>
        /// 第一步:将文件流转换为image
        /// </summary>
        /// <param name="streamByte"></param>
        /// <returns></returns>
        public System.Drawing.Image ReturnPhoto(byte[] streamByte)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream(streamByte);
            System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
            return img;
        }












    }
}

  普通二维码有下面的微信的logo 所以截取

  分了三部:

  第一:将文件流转换为image

  第二:将image截取

  第三:将image转换为文件流

  

  生成100个编码总共用时40s(着实有点多,转换哪里太浪费了)

  

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
获取带参数的微信小程序二维码发布时间:2022-07-18
下一篇:
微信扫描二维码链接打开指定小程序并获取链接参数发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap