diff --git a/tools/viewer/IImageViewer.cs b/tools/viewer/IImageViewer.cs new file mode 100644 index 0000000..c97b9fa --- /dev/null +++ b/tools/viewer/IImageViewer.cs @@ -0,0 +1,21 @@ +namespace KortAppZ.Tools.Viewer +{ + public interface IImageViewer + { + /// + /// 加载图片文件 + /// + /// 图片文件路径 + void LoadImage(string filePath); + + /// + /// 显示图片 + /// + void ShowImage(); + + /// + /// 关闭图片查看器 + /// + void CloseViewer(); + } +} diff --git a/tools/viewer/ImageDisplayForm.cs b/tools/viewer/ImageDisplayForm.cs new file mode 100644 index 0000000..2f1bb13 --- /dev/null +++ b/tools/viewer/ImageDisplayForm.cs @@ -0,0 +1,72 @@ +using System; +using System.Drawing; +using System.Windows.Forms; +using System.IO; + +namespace KortAppZ.Tools.Viewer +{ + public class ImageDisplayForm : Form + { + private PictureBox? pictureBox; + private Image? currentImage; + + public ImageDisplayForm() + { + InitializeComponent(); + } + + private void InitializeComponent() + { + this.pictureBox = new PictureBox(); + // + // pictureBox + // + this.pictureBox.Dock = DockStyle.Fill; + this.pictureBox.SizeMode = PictureBoxSizeMode.Zoom; + this.pictureBox.BackColor = Color.Black; + // + // ImageDisplayForm + // + this.Text = "图片查看"; + this.ClientSize = new Size(800, 600); + this.MaximizeBox = false; + this.MinimizeBox = true; + this.FormBorderStyle = FormBorderStyle.FixedDialog; + this.ControlBox = true; + this.ShowIcon = true; + this.MaximizedBounds = Screen.FromHandle(this.Handle).WorkingArea; + this.Controls.Add(this.pictureBox); + this.FormClosing += new FormClosingEventHandler(ImageDisplayForm_FormClosing); + } + + public void LoadImage(string filePath) + { + try + { + if (currentImage != null) + { + currentImage.Dispose(); + currentImage = null; + } + + currentImage = ImageFileHandler.LoadImage(filePath); + pictureBox.Image = currentImage; + this.Text = $"图片查看 - {Path.GetFileName(filePath)}"; + } + catch (Exception ex) + { + MessageBox.Show($"无法加载图片: {ex.Message}", "错误", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void ImageDisplayForm_FormClosing(object sender, FormClosingEventArgs e) + { + if (currentImage != null) + { + currentImage.Dispose(); + currentImage = null; + } + } + } +} diff --git a/tools/viewer/ImageFileHandler.cs b/tools/viewer/ImageFileHandler.cs new file mode 100644 index 0000000..53686d0 --- /dev/null +++ b/tools/viewer/ImageFileHandler.cs @@ -0,0 +1,41 @@ +using System; +using System.Drawing; +using System.IO; + +namespace KortAppZ.Tools.Viewer +{ + public static class ImageFileHandler + { + private static readonly string[] SupportedExtensions = + { + ".jpg", ".jpeg", ".png", ".bmp", ".gif", ".tiff", ".tif" + }; + + public static bool IsImageFile(string filePath) + { + if (string.IsNullOrEmpty(filePath)) + return false; + + string extension = Path.GetExtension(filePath).ToLower(); + return Array.Exists(SupportedExtensions, ext => ext == extension); + } + + public static Image LoadImage(string filePath) + { + if (!IsImageFile(filePath)) + throw new ArgumentException("不支持的图片格式"); + + if (!File.Exists(filePath)) + throw new FileNotFoundException("图片文件不存在", filePath); + + try + { + return Image.FromFile(filePath); + } + catch (Exception ex) + { + throw new Exception($"无法加载图片: {ex.Message}", ex); + } + } + } +} diff --git a/tools/viewer/ImageViewerForm.cs b/tools/viewer/ImageViewerForm.cs new file mode 100644 index 0000000..80850b9 --- /dev/null +++ b/tools/viewer/ImageViewerForm.cs @@ -0,0 +1,93 @@ +using System; +using System.Drawing; +using System.Windows.Forms; +using System.IO; +using KortAppZ.Tools.Viewer; + +namespace KortAppZ.Tools.Viewer +{ + public class ImageViewerForm : Form + { + private Button? openButton; + + public ImageViewerForm() + { + InitializeComponent(); + } + + private void InitializeComponent() + { + this.openButton = new Button(); + // + // openButton + // + this.openButton.Text = "打开图片"; + this.openButton.Size = new Size(120, 40); + this.openButton.Font = new Font("Microsoft YaHei", 10); + this.openButton.Anchor = AnchorStyles.None; + this.openButton.Click += new EventHandler(OpenButton_Click); + + // 初始位置 + CenterOpenButton(); + + // 窗体大小变化时重新居中按钮 + this.Resize += (s, e) => CenterOpenButton(); + + // + // ImageViewerForm + // + this.Text = "图片查看器"; + this.ClientSize = new Size(400, 300); + this.StartPosition = FormStartPosition.CenterScreen; + this.MinimumSize = new Size(300, 200); + this.MaximizeBox = false; + this.FormBorderStyle = FormBorderStyle.FixedDialog; + this.MaximizedBounds = Screen.FromHandle(this.Handle).WorkingArea; + this.Controls.Add(this.openButton); + } + + private void CenterOpenButton() + { + if (openButton != null) + { + openButton.Location = new Point( + (this.ClientSize.Width - openButton.Width) / 2, + (this.ClientSize.Height - openButton.Height) / 2); + } + } + + private void OpenButton_Click(object sender, EventArgs e) + { + OpenFileDialog openFileDialog = new OpenFileDialog(); + openFileDialog.Filter = "图片文件|*.jpg;*.jpeg;*.png;*.bmp;*.gif;*.tiff;*.tif|所有文件|*.*"; + + if (openFileDialog.ShowDialog() == DialogResult.OK) + { + string filePath = openFileDialog.FileName; + ShowImage(filePath); + } + } + + private void ShowImage(string filePath) + { + try + { + if (!ImageFileHandler.IsImageFile(filePath)) + { + MessageBox.Show("不支持的图片格式", "错误", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + ImageDisplayForm displayForm = new ImageDisplayForm(); + displayForm.LoadImage(filePath); + displayForm.Show(); + } + catch (Exception ex) + { + MessageBox.Show($"无法加载图片: {ex.Message}", "错误", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } +} diff --git a/tools/viewer/ImageViewerToolCard.cs b/tools/viewer/ImageViewerToolCard.cs new file mode 100644 index 0000000..f5aee17 --- /dev/null +++ b/tools/viewer/ImageViewerToolCard.cs @@ -0,0 +1,48 @@ +using System; +using System.Drawing; +using System.Windows.Forms; +using System.IO; +using AppStore; + +namespace KortAppZ.Tools.Viewer +{ + public class ImageViewerToolCard : ToolCard + { + public ImageViewerToolCard() + { + this.ToolName = "图片查看"; + + try + { + string iconPath = Path.Combine(Application.StartupPath, "img", "resource", "png", "ImageCompressor.png"); + if (File.Exists(iconPath)) + { + this.ToolIcon = Image.FromFile(iconPath); + } + else + { + this.ToolIcon = SystemIcons.Application.ToBitmap(); + } + } + catch + { + this.ToolIcon = SystemIcons.Application.ToBitmap(); + } + + this.ToolCardClicked += (s, e) => { + try + { + ImageViewerForm viewerForm = new ImageViewerForm(); + viewerForm.Show(); + } + catch (Exception ex) + { + MessageBox.Show($"打开图片查看器失败: {ex.Message}", "错误", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + }; + + this.UpdateDisplay(); + } + } +}