ShareX/ShareX.HelpersLib/Forms/ImageViewer.cs

159 lines
4.9 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
2019-01-02 20:43:52 +13:00
Copyright (c) 2007-2019 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.IO;
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
{
private Image screenshot;
private ImageViewer(Image image)
{
screenshot = image;
2013-11-03 23:53:49 +13:00
InitializeComponent();
Icon = ShareXResources.Icon;
2013-11-03 23:53:49 +13:00
}
public static void ShowImage(Image img)
{
if (img != null)
{
using (Image tempImage = (Image)img.Clone())
using (ImageViewer viewer = new ImageViewer(tempImage))
{
viewer.ShowDialog();
}
}
}
public static void ShowImage(string filepath)
{
if (!string.IsNullOrEmpty(filepath) && File.Exists(filepath))
{
2013-11-21 06:59:00 +13:00
using (Image img = ImageHelpers.LoadImage(filepath))
2013-11-03 23:53:49 +13:00
using (ImageViewer viewer = new ImageViewer(img))
{
viewer.ShowDialog();
}
}
}
private void ShowScreenshot_Shown(object sender, EventArgs e)
{
this.ForceActivate();
2013-11-03 23:53:49 +13:00
}
private void ShowScreenshot_Deactivate(object sender, EventArgs e)
{
Close();
}
private void pbPreview_MouseDown(object sender, MouseEventArgs e)
{
Close();
}
private void pbPreview_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape || e.KeyCode == Keys.Enter || e.KeyCode == Keys.Space)
{
Close();
}
}
#region Windows Form Designer generated code
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
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
2015-06-28 02:01:22 +12:00
if (screenshot != null)
{
screenshot.Dispose();
2013-11-03 23:53:49 +13:00
}
base.Dispose(disposing);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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;
Text = "ShareX - Image viewer";
TopMost = true;
WindowState = FormWindowState.Normal;
StartPosition = FormStartPosition.Manual;
pbPreview.Cursor = Cursors.Hand;
pbPreview.Dock = System.Windows.Forms.DockStyle.Fill;
pbPreview.DrawCheckeredBackground = true;
pbPreview.FullscreenOnClick = false;
pbPreview.Location = new System.Drawing.Point(0, 0);
pbPreview.Name = "pbPreview";
pbPreview.Size = new System.Drawing.Size(96, 100);
pbPreview.TabIndex = 0;
pbPreview.LoadImage(screenshot);
Controls.Add(pbPreview);
Shown += new System.EventHandler(ShowScreenshot_Shown);
Deactivate += new System.EventHandler(ShowScreenshot_Deactivate);
2013-11-03 23:53:49 +13:00
pbPreview.MouseDown += pbPreview_MouseDown;
pbPreview.KeyDown += pbPreview_KeyDown;
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
}
}