ShareX/ShareX.HelpersLib/Forms/ImageViewer.cs

403 lines
13 KiB
C#
Raw Normal View History

2013-11-03 23:53:49 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2024-01-03 12:57:14 +13:00
Copyright (c) 2007-2024 ShareX Team
2013-11-03 23:53:49 +13:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
2022-07-27 14:18:10 +12:00
using ShareX.HelpersLib.Properties;
2013-11-03 23:53:49 +13:00
using System;
2022-03-17 08:40:15 +13:00
using System.Collections.Generic;
2013-11-03 23:53:49 +13:00
using System.Drawing;
using System.Windows.Forms;
2014-12-11 09:25:20 +13:00
namespace ShareX.HelpersLib
2013-11-03 23:53:49 +13:00
{
public class ImageViewer : Form
2013-11-03 23:53:49 +13:00
{
public Image CurrentImage { get; private set; }
2022-03-16 19:42:06 +13:00
public string CurrentImageFilePath { get; private set; }
2022-03-17 20:52:44 +13:00
public bool SupportWrap { get; set; }
public bool CanNavigate => Images != null && Images.Length > 1;
public bool CanNavigateLeft => CanNavigate && (SupportWrap || CurrentImageIndex > 0);
public bool CanNavigateRight => CanNavigate && (SupportWrap || CurrentImageIndex < Images.Length - 1);
public string[] Images { get; private set; }
public int CurrentImageIndex { get; private set; }
2022-03-29 02:27:35 +13:00
public int NavigationButtonWidth { get; set; } = 100;
2022-04-01 05:02:52 +13:00
public string Status { get; private set; }
2013-11-03 23:53:49 +13:00
2020-11-27 02:39:30 +13:00
private ImageViewer(Image img)
2013-11-03 23:53:49 +13:00
{
InitializeComponent();
ShareXResources.ApplyTheme(this);
LoadImage(img);
}
private ImageViewer(string[] images, int currentImageIndex = 0)
{
2013-11-03 23:53:49 +13:00
InitializeComponent();
2019-07-06 18:58:22 +12:00
ShareXResources.ApplyTheme(this);
Images = images;
CurrentImageIndex = currentImageIndex;
2022-03-17 08:40:15 +13:00
FilterImageFiles();
LoadCurrentImage();
}
private void LoadImage(Image img)
{
CurrentImage?.Dispose();
CurrentImage = img;
pbPreview.LoadImage(CurrentImage);
}
private void LoadCurrentImage()
{
if (Images != null && Images.Length > 0)
{
CurrentImageIndex = CurrentImageIndex.Clamp(0, Images.Length - 1);
CurrentImageFilePath = Images[CurrentImageIndex];
Image img = ImageHelpers.LoadImage(CurrentImageFilePath);
LoadImage(img);
}
2022-04-01 05:02:52 +13:00
UpdateStatus();
}
private void NavigateImage(int position)
{
if (CanNavigate)
{
int nextImageIndex = CurrentImageIndex + position;
if (SupportWrap)
2022-03-17 20:52:44 +13:00
{
if (nextImageIndex > Images.Length - 1)
{
nextImageIndex = 0;
}
else if (nextImageIndex < 0)
{
nextImageIndex = Images.Length - 1;
}
2022-03-17 20:52:44 +13:00
}
nextImageIndex = nextImageIndex.Clamp(0, Images.Length - 1);
2022-03-17 20:52:44 +13:00
if (CurrentImageIndex != nextImageIndex)
{
CurrentImageIndex = nextImageIndex;
LoadCurrentImage();
}
}
2013-11-03 23:53:49 +13:00
}
2022-03-17 08:40:15 +13:00
private void FilterImageFiles()
{
List<string> filteredImages = new List<string>();
for (int i = 0; i < Images.Length; i++)
{
string imageFilePath = Images[i];
bool isImageFile = !string.IsNullOrEmpty(imageFilePath) && FileHelpers.IsImageFile(imageFilePath);
2022-03-17 08:40:15 +13:00
if (i == CurrentImageIndex)
{
if (isImageFile)
{
CurrentImageIndex = filteredImages.Count;
}
else
{
CurrentImageIndex = Math.Max(filteredImages.Count - 1, 0);
2022-03-17 08:40:15 +13:00
}
}
if (isImageFile)
{
filteredImages.Add(imageFilePath);
}
}
Images = filteredImages.ToArray();
}
2022-04-01 05:02:52 +13:00
private void UpdateStatus()
2022-03-16 10:14:38 +13:00
{
2022-04-01 05:02:52 +13:00
Status = "";
2022-03-24 21:20:00 +13:00
if (CanNavigate)
2022-03-16 19:42:06 +13:00
{
2022-04-01 05:02:52 +13:00
AppendStatus($"{CurrentImageIndex + 1} / {Images.Length}");
2022-03-24 21:20:00 +13:00
}
string fileName = FileHelpers.GetFileNameSafe(CurrentImageFilePath);
2022-03-24 21:20:00 +13:00
if (!string.IsNullOrEmpty(fileName))
{
fileName = fileName.Truncate(128, "...");
2022-04-01 05:02:52 +13:00
AppendStatus(fileName);
2022-03-24 21:20:00 +13:00
}
if (CurrentImage != null)
{
2022-04-01 05:02:52 +13:00
AppendStatus($"{CurrentImage.Width} x {CurrentImage.Height}");
2022-03-16 19:42:06 +13:00
}
2022-03-24 21:20:00 +13:00
2022-04-01 05:02:52 +13:00
lblStatus.Visible = !string.IsNullOrEmpty(Status);
lblStatus.Text = Status;
2022-03-29 02:27:35 +13:00
lblStatus.Location = new Point((ClientSize.Width - lblStatus.Width) / 2, 0);
2022-03-16 10:14:38 +13:00
}
2022-04-01 05:02:52 +13:00
private void AppendStatus(string text)
{
if (!string.IsNullOrEmpty(Status))
{
Status += " │ ";
}
Status += text;
}
2013-11-03 23:53:49 +13:00
public static void ShowImage(Image img)
{
if (img != null)
{
2020-11-27 02:39:30 +13:00
using (Image tempImage = img.CloneSafe())
2013-11-03 23:53:49 +13:00
{
2020-11-27 02:39:30 +13:00
if (tempImage != null)
{
using (ImageViewer viewer = new ImageViewer(tempImage))
{
viewer.ShowDialog();
}
}
2013-11-03 23:53:49 +13:00
}
}
}
public static void ShowImage(string filePath)
2013-11-03 23:53:49 +13:00
{
using (Bitmap bmp = ImageHelpers.LoadImage(filePath))
2013-11-03 23:53:49 +13:00
{
if (bmp != null)
2013-11-03 23:53:49 +13:00
{
using (ImageViewer viewer = new ImageViewer(bmp))
{
viewer.ShowDialog();
}
2013-11-03 23:53:49 +13:00
}
}
}
public static void ShowImage(string[] files, int imageIndex = 0)
{
if (files != null && files.Length > 0)
{
using (ImageViewer viewer = new ImageViewer(files, imageIndex))
{
viewer.ShowDialog();
}
}
}
private void ImageViewer_Shown(object sender, EventArgs e)
2013-11-03 23:53:49 +13:00
{
2022-04-01 05:02:52 +13:00
UpdateStatus();
2022-03-16 10:14:38 +13:00
this.ForceActivate();
2013-11-03 23:53:49 +13:00
}
private void ImageViewer_Deactivate(object sender, EventArgs e)
2013-11-03 23:53:49 +13:00
{
Close();
}
2022-03-27 21:59:45 +13:00
private void lblLeft_MouseDown(object sender, MouseEventArgs e)
2013-11-03 23:53:49 +13:00
{
2022-03-27 20:37:31 +13:00
if (e.Button == MouseButtons.Left)
2022-03-18 20:20:48 +13:00
{
2022-03-27 21:59:45 +13:00
NavigateImage(-1);
2022-03-27 23:17:29 +13:00
lblLeft.Visible = CanNavigateLeft;
2022-03-27 21:59:45 +13:00
}
}
private void lblRight_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
NavigateImage(1);
2022-03-27 23:17:29 +13:00
lblRight.Visible = CanNavigateRight;
2022-03-18 20:20:48 +13:00
}
2022-03-27 21:59:45 +13:00
}
2022-03-27 20:37:31 +13:00
2022-03-27 21:59:45 +13:00
private void pbPreview_MouseClick(object sender, MouseEventArgs e)
{
2022-03-27 20:37:31 +13:00
Close();
2013-11-03 23:53:49 +13:00
}
private void pbPreview_MouseMove(object sender, MouseEventArgs e)
{
2022-04-01 05:02:52 +13:00
lblStatus.Visible = !string.IsNullOrEmpty(Status) && !new Rectangle(lblStatus.Location, lblStatus.Size).Contains(e.Location);
2022-03-27 21:59:45 +13:00
lblLeft.Visible = CanNavigateLeft && new Rectangle(lblLeft.Location, lblLeft.Size).Contains(e.Location);
lblRight.Visible = CanNavigateRight && new Rectangle(lblRight.Location, lblRight.Size).Contains(e.Location);
}
2022-03-17 19:34:43 +13:00
private void pbPreview_MouseWheel(object sender, MouseEventArgs e)
{
if (CanNavigateLeft && e.Delta > 0)
2022-03-17 19:34:43 +13:00
{
NavigateImage(-1);
}
else if (CanNavigateRight && e.Delta < 0)
2022-03-17 19:34:43 +13:00
{
NavigateImage(1);
}
}
2013-11-03 23:53:49 +13:00
private void pbPreview_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
2013-11-03 23:53:49 +13:00
{
case Keys.Left:
NavigateImage(-1);
break;
case Keys.Right:
NavigateImage(1);
break;
}
}
private void pbPreview_KeyUp(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Escape:
case Keys.Enter:
case Keys.Space:
Close();
break;
}
}
private void pbPreview_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Left:
case Keys.Right:
e.IsInputKey = true;
break;
2013-11-03 23:53:49 +13:00
}
}
2022-03-24 21:01:17 +13:00
private void lblStatus_MouseEnter(object sender, EventArgs e)
{
lblStatus.Visible = false;
}
2013-11-03 23:53:49 +13:00
#region Windows Form Designer generated code
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
2015-06-28 02:01:22 +12:00
}
2013-11-03 23:53:49 +13:00
CurrentImage?.Dispose();
2013-11-03 23:53:49 +13:00
base.Dispose(disposing);
}
private void InitializeComponent()
{
2018-10-10 00:30:22 +13:00
pbPreview = new MyPictureBox();
2022-03-16 19:42:06 +13:00
lblStatus = new Label();
2022-03-27 21:59:45 +13:00
lblLeft = new Label();
lblRight = new Label();
2018-10-10 00:30:22 +13:00
SuspendLayout();
BackColor = SystemColors.Window;
Bounds = CaptureHelpers.GetActiveScreenBounds();
DoubleBuffered = true;
FormBorderStyle = FormBorderStyle.None;
2022-07-27 14:18:10 +12:00
Text = Resources.ShareXImageViewer;
2018-10-10 00:30:22 +13:00
TopMost = true;
WindowState = FormWindowState.Normal;
StartPosition = FormStartPosition.Manual;
2022-03-16 19:42:06 +13:00
lblStatus.AutoSize = true;
2022-03-24 21:20:00 +13:00
lblStatus.Font = new Font("Arial", 13f);
2022-04-09 08:06:08 +12:00
lblStatus.Padding = new Padding(5);
2022-03-16 19:42:06 +13:00
lblStatus.TextAlign = ContentAlignment.MiddleCenter;
Controls.Add(lblStatus);
2022-03-16 10:14:38 +13:00
2022-03-27 21:59:45 +13:00
lblLeft.Cursor = Cursors.Hand;
lblLeft.Font = new Font("Arial", 50f, FontStyle.Bold);
lblLeft.Location = new Point(0, 0);
lblLeft.Text = "";
lblLeft.TextAlign = ContentAlignment.MiddleCenter;
lblLeft.Size = new Size(NavigationButtonWidth, Bounds.Height);
lblLeft.MouseDown += lblLeft_MouseDown;
Controls.Add(lblLeft);
lblRight.Cursor = Cursors.Hand;
lblRight.Font = new Font("Arial", 50f, FontStyle.Bold);
lblRight.Location = new Point(Bounds.Width - NavigationButtonWidth, 0);
lblRight.Text = "";
lblRight.TextAlign = ContentAlignment.MiddleCenter;
lblRight.Size = new Size(NavigationButtonWidth, Bounds.Height);
lblRight.MouseDown += lblRight_MouseDown;
Controls.Add(lblRight);
2020-11-27 02:39:30 +13:00
pbPreview.Dock = DockStyle.Fill;
2018-10-10 00:30:22 +13:00
pbPreview.DrawCheckeredBackground = true;
2020-11-27 02:39:30 +13:00
pbPreview.Location = new Point(0, 0);
pbPreview.Size = new Size(100, 100);
2018-10-10 00:30:22 +13:00
pbPreview.TabIndex = 0;
Controls.Add(pbPreview);
Shown += ImageViewer_Shown;
Deactivate += ImageViewer_Deactivate;
2022-03-27 20:37:31 +13:00
pbPreview.MouseClick += pbPreview_MouseClick;
pbPreview.MouseMove += pbPreview_MouseMove;
2022-03-17 19:34:43 +13:00
pbPreview.MouseWheel += pbPreview_MouseWheel;
2013-11-03 23:53:49 +13:00
pbPreview.KeyDown += pbPreview_KeyDown;
pbPreview.KeyUp += pbPreview_KeyUp;
pbPreview.PreviewKeyDown += pbPreview_PreviewKeyDown;
2022-03-24 21:01:17 +13:00
lblStatus.MouseEnter += lblStatus_MouseEnter;
2013-11-03 23:53:49 +13:00
2018-10-10 00:30:22 +13:00
ResumeLayout(false);
2013-11-03 23:53:49 +13:00
}
private MyPictureBox pbPreview;
2022-03-16 19:42:06 +13:00
private Label lblStatus;
2022-03-27 21:59:45 +13:00
private Label lblLeft;
private Label lblRight;
2013-11-03 23:53:49 +13:00
#endregion Windows Form Designer generated code
}
}