1703neshinavarfolomeev2/QRCodeViewerForm.cs
2026-03-17 14:33:47 +04:00

121 lines
4.1 KiB
C#

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;
namespace Neshina1703
{
public partial class QRCodeViewerForm : Form
{
private int medicationId;
private string qrCodePath;
public QRCodeViewerForm(int medicationId)
{
InitializeComponent();
this.medicationId = medicationId;
}
private void QRCodeViewerForm_Load(object sender, EventArgs e)
{
LoadQRCode();
}
private void LoadQRCode()
{
// Получить путь к QR-коду из БД
var query = "SELECT qr_code_path, medication_name FROM medications WHERE id = @id";
var parameters = new MySql.Data.MySqlClient.MySqlParameter[]
{
new MySql.Data.MySqlClient.MySqlParameter("@id", medicationId)
};
var table = DB.ExecuteQuery(query, parameters);
if (table.Rows.Count > 0)
{
string name = table.Rows[0]["medication_name"].ToString();
qrCodePath = table.Rows[0]["qr_code_path"]?.ToString();
this.Text = $"QR-код: {name}";
if (!string.IsNullOrEmpty(qrCodePath) && File.Exists(qrCodePath))
{
pictureBoxQR.Image = Image.FromFile(qrCodePath);
}
else
{
// Сгенерировать если не существует
qrCodePath = QRCodeHelper.GenerateQRCodeForMedication(medicationId, name);
if (!string.IsNullOrEmpty(qrCodePath))
pictureBoxQR.Image = Image.FromFile(qrCodePath);
}
}
}
private void btnPrint_Click(object sender, EventArgs e)
{
if (pictureBoxQR.Image != null)
{
PrintDocument printDoc = new PrintDocument();
printDoc.PrintPage += (s, ev) =>
{
ev.Graphics.DrawImage(pictureBoxQR.Image,
new Rectangle(100, 100, 400, 400));
};
PrintDialog printDialog = new PrintDialog();
printDialog.Document = printDoc;
if (printDialog.ShowDialog() == DialogResult.OK)
printDoc.Print();
}
}
private void btnSave_Click(object sender, EventArgs e)
{
if (pictureBoxQR.Image != null)
{
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.Filter = "PNG Images|*.png|JPEG Images|*.jpg";
saveDialog.Title = "Сохранить QR-код";
if (saveDialog.ShowDialog() == DialogResult.OK)
{
pictureBoxQR.Image.Save(saveDialog.FileName);
MessageBox.Show("QR-код сохранен!", "Успех",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
private void btnGenerateNew_Click(object sender, EventArgs e)
{
var query = "SELECT medication_name FROM medications WHERE id = @id";
var parameters = new MySql.Data.MySqlClient.MySqlParameter[]
{
new MySql.Data.MySqlClient.MySqlParameter("@id", medicationId)
};
var table = DB.ExecuteQuery(query, parameters);
if (table.Rows.Count > 0)
{
string name = table.Rows[0]["medication_name"].ToString();
qrCodePath = QRCodeHelper.GenerateQRCodeForMedication(medicationId, name);
if (!string.IsNullOrEmpty(qrCodePath))
{
pictureBoxQR.Image = Image.FromFile(qrCodePath);
MessageBox.Show("QR-код перегенерирован!", "Успех",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
}