ShareX/ShareX.HelpersLib/Forms/ImageViewer.cs

237 lines
7 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
2022-01-12 05:32:17 +13:00
Copyright (c) 2007-2022 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)
using System;
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; }
public bool SupportsImageNavigation => Images != null && Images.Length > 0;
public string[] Images { get; private set; }
public int CurrentImageIndex { 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;
LoadCurrentImage();
}
private void LoadImage(Image img)
{
CurrentImage?.Dispose();
CurrentImage = img;
pbPreview.LoadImage(CurrentImage);
}
private void LoadCurrentImage()
{
if (!SupportsImageNavigation) return;
CurrentImageIndex = CurrentImageIndex.Clamp(0, Images.Length - 1);
string imageFilePath = Images[CurrentImageIndex];
Image img = ImageHelpers.LoadImage(imageFilePath);
LoadImage(img);
}
private void NavigateImage(int position)
{
if (!SupportsImageNavigation) return;
int nextImageIndex = CurrentImageIndex + position;
if (nextImageIndex > Images.Length - 1)
{
nextImageIndex = 0;
}
else if (nextImageIndex < 0)
{
nextImageIndex = Images.Length - 1;
}
if (CurrentImageIndex != nextImageIndex)
{
CurrentImageIndex = nextImageIndex;
LoadCurrentImage();
}
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[] images, int currentImageIndex = 0)
{
if (images != null && images.Length > 0)
{
using (ImageViewer viewer = new ImageViewer(images, currentImageIndex))
{
viewer.ShowDialog();
}
}
}
private void ImageViewer_Shown(object sender, EventArgs e)
2013-11-03 23:53:49 +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();
}
private void pbPreview_MouseDown(object sender, MouseEventArgs e)
{
Close();
}
private void pbPreview_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
2013-11-03 23:53:49 +13:00
{
case Keys.Escape:
case Keys.Enter:
case Keys.Space:
Close();
break;
case Keys.Left:
NavigateImage(-1);
break;
case Keys.Right:
NavigateImage(1);
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
}
}
#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();
SuspendLayout();
BackColor = SystemColors.Window;
Bounds = CaptureHelpers.GetActiveScreenBounds();
DoubleBuffered = true;
FormBorderStyle = FormBorderStyle.None;
// TODO: Translate
2018-10-10 00:30:22 +13:00
Text = "ShareX - Image viewer";
TopMost = true;
WindowState = FormWindowState.Normal;
StartPosition = FormStartPosition.Manual;
pbPreview.Cursor = Cursors.Hand;
2020-11-27 02:39:30 +13:00
pbPreview.Dock = DockStyle.Fill;
2018-10-10 00:30:22 +13:00
pbPreview.DrawCheckeredBackground = true;
pbPreview.FullscreenOnClick = false;
2020-11-27 02:39:30 +13:00
pbPreview.Location = new Point(0, 0);
2018-10-10 00:30:22 +13:00
pbPreview.Name = "pbPreview";
2019-07-06 18:58:22 +12:00
pbPreview.ShowImageSizeLabel = true;
2020-11-27 02:39:30 +13:00
pbPreview.Size = new Size(96, 100);
2018-10-10 00:30:22 +13:00
pbPreview.TabIndex = 0;
Controls.Add(pbPreview);
Shown += ImageViewer_Shown;
Deactivate += ImageViewer_Deactivate;
2013-11-03 23:53:49 +13:00
pbPreview.MouseDown += pbPreview_MouseDown;
pbPreview.KeyDown += pbPreview_KeyDown;
pbPreview.PreviewKeyDown += pbPreview_PreviewKeyDown;
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;
#endregion Windows Form Designer generated code
}
}