代码暂存 [获取二唯码并识别保存二唯码]
public void CreateImage(string imgfile)
{
wx_set mWXSet = wx_set.FetchTopOne();
if (mWXSet.IsNew)
{
throw new Exception("未配置与微信交互的相关参数");
}
FileInfo mFile = new FileInfo(imgfile);
if (mFile.Exists && mFile.LastWriteTime.AddMilliseconds(7) > DateTime.Now)
{
mFile.Open(FileMode.Open).Close();
return;
}
string accessToken = AccessTokenContainer.TryGetToken(mWXSet.appid, mWXSet.appsecret);
//int expireSenconds = 0;
int expireSenconds = 604800;
CreateQrCodeResult createQrCodeResult = QrCodeApi.Create(accessToken, expireSenconds, 10001);
if (!string.IsNullOrEmpty(createQrCodeResult.ticket))
{
using (MemoryStream stream = new MemoryStream())
{
//根据ticket获取二维码
QrCodeApi.ShowQrCode(createQrCodeResult.ticket, stream);
//将获取到的二维码图片转换为Base64String格式
//byte[] imageBytes = stream.ToArray();
//string base64Image = System.Convert.ToBase64String(imageBytes);
//由于SqlServerCompact数据库限制最长字符4000,本测试项目将二维码保存到磁盘,正式项目中可直接保存到数据库
popularizeimagecode =
WH.Business.Utilities.wjGoddess.SaveQRCodeImage(stream,
imgfile);
}
}
}
public static string SaveQRCodeImage(Stream InputStream, string filePath)
{
using (Bitmap bmp = new Bitmap(InputStream))
using (Bitmap bmp2 = new Bitmap(bmp.Width, bmp.Height, PixelFormat.Format32bppArgb))
using (Graphics draw = Graphics.FromImage(bmp2))
{
try
{
//新建第二个bitmap类型的bmp2变量,我这里是根据我的程序需要设置的。
//将第一个bmp拷贝到bmp2中
draw.DrawImage(bmp, 0, 0, bmp.Width, bmp.Height);
bmp2.Save(filePath);
LuminanceSource source = new RGBLuminanceSource(bmp2, bmp2.Width, bmp2.Height);
com.google.zxing.BinaryBitmap bitmap = new com.google.zxing.BinaryBitmap(new HybridBinarizer(source));
com.google.zxing.Result result;
result = new MultiFormatReader().decode(bitmap);
return result.Text;
}
catch (ReaderException re)
{
MyLog.Log(re.ToString());
//MessageBox.Show(re.ToString());
//return;
}
finally
{
draw.Dispose();
bmp.Dispose();//释放bmp文件资源
bmp2.Dispose();
}
}
return "";
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构