发布:2023/12/7 15:52:06作者:大数据 来源:大数据 浏览次数:578
bytesContent为图片进制字节数据或数组。
1 2 3 4 5 |
FileStream fStream = new FileStream("d:\\1.jpg", FileMode.Create); BinaryWriter bw = new BinaryWriter(fStream); bw.Write(bytesContent); bw.Close(); fStream.Close(); |
.net core 将图片二进制流或图片arraybuffer字节码还原成/生成图片
1 2 3 |
var ms = new MemoryStream(arrayBuffer); Bitmap bitmap = new Bitmap(ms); bitmap.Save("d:\\12.jpg", ImageFormat.Jpeg); |
1 2 3 |
var ms = new MemoryStream(arrayBuffer); var img = Image.FromStream(ms); img.Save(@"d:\abc.jpg"); |
1、不需要安装插件httppost请求
1 2 3 4 5 6 7 8 9 10 |
string token = OAuth.GetToken(); var requestUri = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + token; HttpClient httpClient = new HttpClient(); var body = "{\"page\": \"pages/my/my\",\"scene\": \"parentId=" + memberId + "\",\"check_path\": false,\"env_version\": \"trial\"}"; HttpContent content = new StringContent(body); content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); var arrayBuffer = httpClient.PostAsync(requestUri, content).Result.Content.ReadAsByteArrayAsync().Result; var base64str = "data:image/png;base64," + Convert.ToBase64String(arrayBuffer); return Ok(base64str); |
token为你自己从微信获取的token,还需要注意的是Content.ReadAsByteArrayAsync().Result,因微信接口返回的是arrayBuffer所以读取用字节数组,一般结果是json的话,那需要使用ReadAsStringAsync
2、使用restsharp插件
需要从nuget安装RestSharp插件,版本为106.15.0
Probably, the most popular REST API client library for .NET
参考:https://github.com/restsharp/RestSharp
https://restsharp.dev/
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public IActionResult QrCode(string memberId) { var client = new RestClient("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + OAuth.GetToken()); client.Timeout = -1; var request = new RestRequest(Method.POST); request.AddHeader("Content-Type", "application/json"); var body = "{\"page\": \"pages/my/my\",\"scene\": \"parentId=" + memberId + "\",\"check_path\": false,\"env_version\": \"trial\"}"; request.AddParameter("application/json", body, ParameterType.RequestBody); IRestResponse response = client.Execute(request); var arrayBuffer = response.RawBytes; var base64str = "data:image/png;base64," + Convert.ToBase64String(arrayBuffer); return Ok(base64str); } |
返回结果为base64字符串,在微信小程序直接请求服务器该接口,将结果写到<image src={{base64Str}} ></image>显示出图片即可。
© Copyright 2014 - 2024 柏港建站平台 ejk5.com. 渝ICP备16000791号-4