MultiWallpaperMaker/MultiWallpaperMaker/Form1.cs
2016-11-05 15:45:33 +01:00

156 lines
5.7 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Trigger.Classes.Screen;
namespace MultiWallpaperMaker
{
public partial class Form1 : Form
{
private string imagePath;
public string ImagePath {
get { return this.imagePath; }
set {
try {
this.Image = new Bitmap(System.Drawing.Image.FromFile(value));
this.imagePath = value;
} catch (Exception e) {
MessageBox.Show(e.Message);
}
}
}
public event EventHandler ImageChanged;
private Bitmap image;
public Bitmap Image {
get { return this.image; }
private set {
if ((object)this.image != null) this.image.Dispose();
this.image = value;
if (this.ImageChanged != null) this.ImageChanged(this, new EventArgs());
}
}
public Bitmap DisplayedImage {
get {
if ((object)this.image == null) return null;
Bitmap image = this.image.Clone() as Bitmap;
using (Graphics g = Graphics.FromImage(image)) {
Color c = Color.FromArgb((int)((uint)new Random().Next(0x00FFFFFF) | 0xFF000000));
SolidBrush brush = new SolidBrush(c);
Pen pen = new Pen(brush, 2);
foreach (var screen in this.Screens) {
g.DrawRectangle(pen, this.getRectangle(screen));
}
pen.Dispose();
}
return image;
}
}
private void updateDisplay() {
if ((object)this.pictureBox1.Image != null) this.pictureBox1.Image.Dispose();
this.pictureBox1.Image = this.DisplayedImage;
}
public double downscale;
public double Downscale {
get { return this.downscale; }
private set {
if (this.downscale == value) return;
this.downscale = value;
if (this.DownscaleChanged != null) this.DownscaleChanged(this, new EventArgs());
}
}
public event EventHandler DownscaleChanged;
public Point startPoint;
public Point StartPoint
{
get { return this.startPoint; }
private set {
if (this.startPoint == value) return;
this.startPoint = value;
if (this.StartPointChanged != null) this.StartPointChanged(this, new EventArgs());
}
}
public event EventHandler StartPointChanged;
public List<Trigger.Classes.Screen.ScreenEx> Screens { get; private set; }
private Nullable<Point> mouseDragStart;
public Form1()
{
InitializeComponent();
this.AllowDrop = true;
this.DragEnter += Form1_DragEnter;
this.DragDrop += Form1_DragDrop;
this.MouseWheel += (s, e) => this.Downscale += e.Delta / 10000.0;
this.ImageChanged += (s, e) => this.updateDisplay();
this.DownscaleChanged += (s, e) => this.updateDisplay();
this.StartPointChanged += (s, e) => this.updateDisplay();
this.pictureBox1.MouseDown += (s, e) => this.mouseDragStart = e.Location;
this.pictureBox1.MouseUp += (s, e) => this.mouseDragStart = null;
this.pictureBox1.MouseMove += Form1_MouseMove;
this.Screens = Trigger.Status.Screen.AllScreens;
this.Downscale = 1;
}
void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (this.mouseDragStart == null) return;
this.StartPoint += new Size(e.Location - new Size(this.mouseDragStart.Value));
this.mouseDragStart = e.Location;
}
void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy;
else e.Effect = DragDropEffects.None;
}
void Form1_DragDrop(object sender, DragEventArgs e)
{
if (!e.Data.GetDataPresent(DataFormats.FileDrop)) return;
string[] paths = (string[])e.Data.GetData(DataFormats.FileDrop);
this.ImagePath = paths[0];
}
private void button1_Click(object sender, EventArgs e)
{
if ((object)this.Image == null) return;
this.Image.RotateFlip(RotateFlipType.RotateNoneFlipX);
if (this.ImageChanged != null) this.ImageChanged(this, new EventArgs());
}
private void button3_Click(object sender, EventArgs e)
{
if (this.Image == null) return;
string file = Path.GetDirectoryName(this.imagePath);
file = Path.Combine(file, Path.GetFileNameWithoutExtension(this.imagePath));
for (int i = 0; i < this.Screens.Count; i++) {
Bitmap image = this.Image.Clone(this.getRectangle(this.Screens[i]), this.Image.PixelFormat);
image.Save(file + i + ".bmp");
}
}
Rectangle getRectangle(ScreenEx screen) {
var location = this.StartPoint + new Size((int)(screen.Bounds.Location.X * this.Downscale), (int)(screen.Bounds.Location.Y * this.Downscale));
var size = new Size((int)(screen.Bounds.Size.Width * this.Downscale), (int)(screen.Bounds.Size.Height * this.Downscale));
return new Rectangle(location, size);
}
}
}