最近发现通过小程序接口返回的二维码保存后,却无法打开。
前段时间还可以的,不知道是微信改了什么吗?
请帮忙看看。
下面是代码
public void CreateQrCode() { var service = new CommonService(); var token = service.GetAccessToken(); var qrcodeContent = Guid.NewGuid().ToString(); var body = $ "{{\"path\":\"pages/xxx?q='{qrcodeContent}'\"}}" ; var results = HttpPost(url, body); ToImage(results); } public Stream HttpPost( string url, string body) { Encoding encoding = Encoding.UTF8; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST" ; //request.ContentType = "application/json; charset=utf-8"; byte [] buffer = encoding.GetBytes(body); request.ContentLength = buffer.Length; var witer = request.GetRequestStream(); witer.Write(buffer, 0, buffer.Length); witer.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); return response.GetResponseStream(); } private static void ToImage(Stream fileSteam) { var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test2.jpg" ); byte [] bytes; using (MemoryStream ms = new MemoryStream()) { fileSteam.CopyTo(ms); bytes = ms.ToArray(); } using ( var fs = File.OpenWrite(path)) { fs.Write(bytes, 0, bytes.Length); } Process.Start(path); } |
楼主解决了吗?遇到同样的问题,返回的图片 buffer 存入本地图片文件无法预览:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/qrcode-link/qr-code/getUnlimitedQRCode.html
请具体描述问题,具体是什么表现?报什么错?
然后如果把Stream转成Image对象时,会报参数不正确的错误,说明是图片的内容不正确,导致转换失败。