superstar_v2/PrimaryFormParts/PrimaryForm.QRCode.cs

121 lines
4.6 KiB
C#
Raw Normal View History

2025-04-07 16:54:10 +08:00
using System.IO;
namespace DualScreenDemo
{
public partial class PrimaryForm : Form
{
public PictureBox pictureBoxQRCode;
public Button closeQRCodeButton;
2025-04-07 16:54:10 +08:00
public void OverlayQRCodeOnImage(string randomFolderPath)
2025-04-07 16:54:10 +08:00
{
var data = LoadBtnConfigData();
2025-04-07 16:54:10 +08:00
try
{
2025-08-13 16:42:22 +08:00
string imagePath = Path.Combine(serverPath, data["QrCode"]["QrCodeBaseUI"]);
2025-04-07 16:54:10 +08:00
if (!File.Exists(imagePath))
{
Console.WriteLine("Base image not found: " + imagePath);
return;
}
using (Image baseImage = Image.FromFile(imagePath))
{
string qrImagePath = Path.Combine(Application.StartupPath, "themes/superstar/_www", randomFolderPath, "qrcode.png");
if (!File.Exists(qrImagePath))
{
Console.WriteLine("布局 image not found: " + qrImagePath);
2025-04-07 16:54:10 +08:00
return;
}
string tempPath = Path.GetTempFileName();
File.Copy(qrImagePath, tempPath, true); // 複製一份再讀,避開快取
2025-04-07 16:54:10 +08:00
Image qrCodeImage = null;
for (int i = 0; i < 3; i++)
{
try
{
using (var fs = new FileStream(tempPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
2025-04-07 16:54:10 +08:00
{
qrCodeImage = Image.FromStream(fs);
}
break;
}
catch (Exception ex)
{
Console.WriteLine("Error loading QR code image: " + ex.Message);
Thread.Sleep(100);
2025-04-07 16:54:10 +08:00
}
}
File.Delete(tempPath); // 用完記得刪除
2025-04-07 16:54:10 +08:00
if (qrCodeImage == null)
{
Console.WriteLine("Failed to load QR code image after multiple attempts.");
return;
}
using (qrCodeImage)
{
2025-04-07 16:54:10 +08:00
using (Bitmap bitmap = new Bitmap(baseImage.Width, baseImage.Height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
2025-06-17 09:31:00 +08:00
// g.DrawImage(baseImage, 0, 0);
// cropped qrcode 版型不同設定調整
g.DrawImage(baseImage, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
2025-08-06 10:47:43 +08:00
Rectangle qrCodeRect = new Rectangle(41, 50, 227, 227);
2025-04-07 16:54:10 +08:00
g.DrawImage(qrCodeImage, qrCodeRect);
}
2025-04-07 16:54:10 +08:00
pictureBoxQRCode.Image = new Bitmap(bitmap);
}
}
}
2025-04-07 16:54:10 +08:00
ResizeAndPositionControl(pictureBoxQRCode, 975, 442, 226, 274);
2025-08-13 16:42:22 +08:00
Bitmap originalImage = new Bitmap(Path.Combine(serverPath, data["QrCode"]["QrCodeBaseUI"]));
2025-04-07 16:54:10 +08:00
2025-08-06 10:47:43 +08:00
//Rectangle closeQRCodeCropArea = new Rectangle(198, 6, 22, 22);
2025-04-07 16:54:10 +08:00
2025-04-07 16:54:10 +08:00
2025-08-06 10:47:43 +08:00
2025-04-07 16:54:10 +08:00
closeQRCodeButton = new Button { Text = "" };
closeQRCodeButton.Name = "closeQRCodeButton";
2025-08-06 10:47:43 +08:00
closeQRCodeButton.BackColor = Color.Transparent;
2025-04-07 16:54:10 +08:00
closeQRCodeButton.BackgroundImageLayout = ImageLayout.Stretch;
closeQRCodeButton.FlatStyle = FlatStyle.Flat;
2025-08-06 10:47:43 +08:00
closeQRCodeButton.FlatAppearance.BorderSize = 0;
closeQRCodeButton.FlatAppearance.MouseOverBackColor = Color.Transparent;
closeQRCodeButton.FlatAppearance.MouseDownBackColor = Color.Transparent;
2025-04-07 16:54:10 +08:00
closeQRCodeButton.Click += CloseQRCodeButton_Click;
2025-08-06 10:47:43 +08:00
pictureBoxQRCode.Controls.Add(closeQRCodeButton);
ResizeAndPositionControl(closeQRCodeButton, 193, 4, 25, 25);
2025-04-07 16:54:10 +08:00
}
catch (Exception ex)
{
Console.WriteLine("Error in OverlayQRCodeOnImage: " + ex.Message);
if (ex.InnerException != null)
{
Console.WriteLine("Inner exception: " + ex.InnerException.Message);
}
}
}
private void CloseQRCodeButton_Click(object sender, EventArgs e)
{
pictureBoxQRCode.Visible = false;
closeQRCodeButton.Visible = false;
}
}
}