For custom rotation, allow the user to draw a line and the picture will be rotated so that the line matches up with the nearest 90 degrees.

This commit is contained in:
Ben Olden-Cooligan 2016-03-24 21:24:46 -04:00
parent 981cfe1d5d
commit f2941230c3
3 changed files with 72 additions and 3 deletions

View File

@ -46,9 +46,14 @@ namespace NAPS2.WinForms
// pictureBox
//
this.pictureBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.pictureBox.Cursor = System.Windows.Forms.Cursors.Cross;
resources.ApplyResources(this.pictureBox, "pictureBox");
this.pictureBox.Name = "pictureBox";
this.pictureBox.TabStop = false;
this.pictureBox.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox_Paint);
this.pictureBox.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox_MouseDown);
this.pictureBox.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox_MouseMove);
this.pictureBox.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox_MouseUp);
//
// btnOK
//
@ -106,7 +111,7 @@ namespace NAPS2.WinForms
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "FRotate";
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.FCrop_FormClosed);
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.FRotate_FormClosed);
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.tbAngle)).EndInit();
this.ResumeLayout(false);

View File

@ -21,6 +21,7 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
@ -152,7 +153,7 @@ namespace NAPS2.WinForms
UpdatePreviewBox();
}
private void FCrop_FormClosed(object sender, FormClosedEventArgs e)
private void FRotate_FormClosed(object sender, FormClosedEventArgs e)
{
workingImage.Dispose();
if (pictureBox.Image != null)
@ -188,5 +189,68 @@ namespace NAPS2.WinForms
txtAngle.Text = (tbAngle.Value / 10.0).ToString("G") + '\u00B0';
UpdateTransform();
}
private bool guideExists;
private Point guideStart, guideEnd;
private const int MIN_LINE_DISTANCE = 50;
private const float LINE_PEN_SIZE = 3;
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
guideExists = true;
guideStart = guideEnd = e.Location;
pictureBox.Invalidate();
}
private void pictureBox_MouseUp(object sender, MouseEventArgs e)
{
guideExists = false;
var dx = guideEnd.X - guideStart.X;
var dy = guideEnd.Y - guideStart.Y;
var distance = Math.Sqrt(dx * dx + dy * dy);
if (distance > MIN_LINE_DISTANCE)
{
var angle = -Math.Atan2(dy, dx) * 180.0 / Math.PI;
while (angle > 45.0)
{
angle -= 90.0;
}
while (angle < -45.0)
{
angle += 90.0;
}
var oldAngle = tbAngle.Value / 10.0;
var newAngle = angle + oldAngle;
while (newAngle > 180.0)
{
newAngle -= 360.0;
}
while (newAngle < -180.0)
{
newAngle += 360.0;
}
tbAngle.Value = (int)Math.Round(newAngle * 10);
tbAngle_Scroll(null, null);
}
pictureBox.Invalidate();
}
private void pictureBox_MouseMove(object sender, MouseEventArgs e)
{
guideEnd = e.Location;
pictureBox.Invalidate();
}
private void pictureBox_Paint(object sender, PaintEventArgs e)
{
if (guideExists)
{
var old = e.Graphics.SmoothingMode;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.DrawLine(new Pen(Color.Black, LINE_PEN_SIZE), guideStart, guideEnd);
e.Graphics.SmoothingMode = old;
}
}
}
}

View File

@ -343,6 +343,6 @@
<value>FRotate</value>
</data>
<data name="&gt;&gt;$this.Type" xml:space="preserve">
<value>NAPS2.WinForms.FormBase, NAPS2.Core, Version=4.2.3.28575, Culture=neutral, PublicKeyToken=null</value>
<value>NAPS2.WinForms.FormBase, NAPS2.Core, Version=5.0.3.36170, Culture=neutral, PublicKeyToken=null</value>
</data>
</root>