整合管理器服务端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.8 KiB

using Net.Codecrete.QrCodeGenerator;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.TextFormatting;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static System.Net.WebRequestMethods;
namespace SunlightAggregationManager.View
{
/// <summary>
/// StatePage.xaml 的交互逻辑
/// </summary>
public partial class imgQR : UserControl
{
//调用配置文件
private readonly UserClass.IniFile.IniFiles Configini = new UserClass.IniFile.IniFiles(Convert.ToString(System.AppDomain.CurrentDomain.BaseDirectory) + "Configini.ini");
public imgQR()
{
InitializeComponent();
}
/// <summary>
/// 生成二维码
/// 调用示例: imgQR_CREATE("TEST", imgQR);;
/// </summary>
/// <param name="data">[字符串]</param>
/// <param name="Image">Image控件</param>
void imgQR_CREATE(string DAT, System.Windows.Controls.Image image)
{
// 生成二维码对象
QrCode qr = QrCode.EncodeText(DAT, QrCode.Ecc.Medium);
// 手动绘制 Bitmap (不使用扩展方法)
int scale = 10; // 每个点的像素大小
int border = 4; // 边距
int size = (qr.Size + border * 2) * scale;
// 创建位图
using (Bitmap bmp = new Bitmap(size, size))
{
using (Graphics gfx = Graphics.FromImage(bmp))
{
// 填充背景(白色)
gfx.Clear(System.Drawing.Color.White);
// 绘制二维码点阵(黑色)
using (System.Drawing.Brush brush = new SolidBrush(System.Drawing.Color.Black))
{
for (int y = 0; y < qr.Size; y++)
{
for (int x = 0; x < qr.Size; x++)
{
if (qr.GetModule(x, y))
{
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(
(x + border) * scale,
(y + border) * scale,
scale, scale);
gfx.FillRectangle(brush, rect);
}
}
}
}
}
// 转换为 WPF 的 BitmapImage 并显示
using (MemoryStream ms = new MemoryStream())
{
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ms.Position = 0;
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = ms;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
// 赋值给 Image 控件 (假设控件名为 imgQR)
image.Source = bitmapImage;
}
}
}
private void imgQR_Loaded(object sender, RoutedEventArgs e)
{
string text_APP = "TEST"; // 要生成二维码的内容
string text_LINK = Configini.IniReadvalue("SYS", "LINK"); // 要生成二维码的内容
imgQR_CREATE(text_APP, imgQR_app);
imgQR_CREATE(text_LINK, imgQR_link);
}
}
}