Made resize work, cleaned up code so it should be easier to port the changes to 2.x

This commit is contained in:
RKrom 2014-06-01 20:25:52 +02:00
parent 5cb877f8c8
commit aced477cef
8 changed files with 446 additions and 474 deletions

View file

@ -68,7 +68,7 @@ namespace Greenshot.Drawing {
} }
} }
public override bool hasContextMenu { public override bool HasContextMenu {
get { get {
// No context menu for the CropContainer // No context menu for the CropContainer
return false; return false;

View file

@ -43,10 +43,10 @@ namespace Greenshot.Drawing {
/// OnPropertyChanged whenever a public property has been changed. /// OnPropertyChanged whenever a public property has been changed.
/// </summary> /// </summary>
[Serializable] [Serializable]
public abstract class DrawableContainer : AbstractFieldHolderWithChildren, INotifyPropertyChanged, IDrawableContainer { public abstract class DrawableContainer : AbstractFieldHolderWithChildren, IDrawableContainer {
private static readonly ILog LOG = LogManager.GetLogger(typeof(DrawableContainer)); private static readonly ILog LOG = LogManager.GetLogger(typeof(DrawableContainer));
protected static readonly EditorConfiguration editorConfig = IniConfig.GetIniSection<EditorConfiguration>(); protected static readonly EditorConfiguration EditorConfig = IniConfig.GetIniSection<EditorConfiguration>();
private bool _isMadeUndoable = false; private bool _isMadeUndoable;
protected EditStatus _defaultEditMode = EditStatus.DRAWING; protected EditStatus _defaultEditMode = EditStatus.DRAWING;
public EditStatus DefaultEditMode { public EditStatus DefaultEditMode {
@ -61,20 +61,22 @@ namespace Greenshot.Drawing {
} }
protected virtual void Dispose(bool disposing) { protected virtual void Dispose(bool disposing) {
if (disposing) { if (!disposing) {
if (_grippers != null) { return;
for (int i = 0; i < _grippers.Length; i++) {
if (_grippers[i] != null) {
_grippers[i].Dispose();
_grippers[i] = null;
}
}
_grippers = null;
}
FieldAggregator aggProps = _parent.FieldAggregator;
aggProps.UnbindElement(this);
} }
if (_grippers != null) {
for (int i = 0; i < _grippers.Length; i++) {
if (_grippers[i] == null) {
continue;
}
_grippers[i].Dispose();
_grippers[i] = null;
}
_grippers = null;
}
FieldAggregator aggProps = _parent.FieldAggregator;
aggProps.UnbindElement(this);
} }
~DrawableContainer() { ~DrawableContainer() {
@ -82,10 +84,10 @@ namespace Greenshot.Drawing {
} }
[NonSerialized] [NonSerialized]
private PropertyChangedEventHandler propertyChanged; private PropertyChangedEventHandler _propertyChanged;
public event PropertyChangedEventHandler PropertyChanged { public event PropertyChangedEventHandler PropertyChanged {
add { propertyChanged += value; } add { _propertyChanged += value; }
remove{ propertyChanged -= value; } remove{ _propertyChanged -= value; }
} }
public List<IFilter> Filters { public List<IFilter> Filters {
@ -108,7 +110,7 @@ namespace Greenshot.Drawing {
} }
[NonSerialized] [NonSerialized]
protected Gripper[] _grippers; protected Gripper[] _grippers;
private bool layoutSuspended = false; private bool _layoutSuspended;
[NonSerialized] [NonSerialized]
private Gripper _targetGripper; private Gripper _targetGripper;
@ -120,7 +122,7 @@ namespace Greenshot.Drawing {
} }
[NonSerialized] [NonSerialized]
private bool _selected = false; private bool _selected;
public bool Selected { public bool Selected {
get {return _selected;} get {return _selected;}
set { set {
@ -141,47 +143,51 @@ namespace Greenshot.Drawing {
} }
private int _left = 0; private int _left;
public int Left { public int Left {
get { return _left; } get { return _left; }
set { set {
if(value != _left) { if (value == _left) {
_left = value; return;
DoLayout();
} }
_left = value;
DoLayout();
} }
} }
private int _top = 0; private int _top;
public int Top { public int Top {
get { return _top; } get { return _top; }
set { set {
if(value != _top) { if (value == _top) {
_top = value; return;
DoLayout();
} }
_top = value;
DoLayout();
} }
} }
private int _width = 0; private int _width;
public int Width { public int Width {
get { return _width; } get { return _width; }
set { set {
if(value != _width) { if (value == _width) {
_width = value; return;
DoLayout();
} }
_width = value;
DoLayout();
} }
} }
private int _height = 0; private int _height;
public int Height { public int Height {
get { return _height; } get { return _height; }
set { set {
if(value != _height) { if (value == _height) {
_height = value; return;
DoLayout();
} }
_height = value;
DoLayout();
} }
} }
@ -189,46 +195,50 @@ namespace Greenshot.Drawing {
get { get {
return new Point(_left, _top); return new Point(_left, _top);
} }
set {
_left = value.X;
_top = value.Y;
}
} }
public Size Size { public Size Size {
get { get {
return new Size(_width, _height); return new Size(_width, _height);
} }
set {
_width = value.Width;
_height = value.Height;
}
} }
[NonSerialized] [NonSerialized]
/// <summary> // will store current bounds of this DrawableContainer before starting a resize
/// will store current bounds of this DrawableContainer before starting a resize
/// </summary>
private Rectangle _boundsBeforeResize = Rectangle.Empty; private Rectangle _boundsBeforeResize = Rectangle.Empty;
[NonSerialized] [NonSerialized]
/// <summary> // "workbench" rectangle - used for calculatoing bounds during resizing (to be applied to this DrawableContainer afterwards)
/// "workbench" rectangle - used for calculatoing bounds during resizing (to be applied to this DrawableContainer afterwards)
/// </summary>
private RectangleF _boundsAfterResize = RectangleF.Empty; private RectangleF _boundsAfterResize = RectangleF.Empty;
public Rectangle Bounds { public Rectangle Bounds {
get { return GuiRectangle.GetGuiRectangle(Left, Top, Width, Height); } get { return GuiRectangle.GetGuiRectangle(Left, Top, Width, Height); }
set { set {
Left = round(value.Left); Left = Round(value.Left);
Top = round(value.Top); Top = Round(value.Top);
Width = round(value.Width); Width = Round(value.Width);
Height = round(value.Height); Height = Round(value.Height);
} }
} }
public virtual void ApplyBounds(RectangleF newBounds) { public virtual void ApplyBounds(RectangleF newBounds) {
Left = round(newBounds.Left); Left = Round(newBounds.Left);
Top = round(newBounds.Top); Top = Round(newBounds.Top);
Width = round(newBounds.Width); Width = Round(newBounds.Width);
Height = round(newBounds.Height); Height = Round(newBounds.Height);
} }
public DrawableContainer(Surface parent) { public DrawableContainer(Surface parent) {
InitializeFields(); InitializeFields();
this._parent = parent; _parent = parent;
InitControls(); InitControls();
} }
@ -240,19 +250,13 @@ namespace Greenshot.Drawing {
RemoveChild(filter); RemoveChild(filter);
} }
private int round(float f) { private static int Round(float f) {
if(float.IsPositiveInfinity(f) || f>int.MaxValue/2) return int.MaxValue/2; if(float.IsPositiveInfinity(f) || f>int.MaxValue/2) return int.MaxValue/2;
if (float.IsNegativeInfinity(f) || f<int.MinValue/2) return int.MinValue/2; if (float.IsNegativeInfinity(f) || f<int.MinValue/2) return int.MinValue/2;
return (int)Math.Round(f); return (int)Math.Round(f);
} }
private int round(double d) { private bool _accountForShadowChange;
if(Double.IsPositiveInfinity(d) || d>int.MaxValue/2) return int.MaxValue/2;
if (Double.IsNegativeInfinity(d) || d<int.MinValue/2) return int.MinValue/2;
return (int)Math.Round(d);
}
private bool accountForShadowChange = false;
public virtual Rectangle DrawingBounds { public virtual Rectangle DrawingBounds {
get { get {
foreach(IFilter filter in Filters) { foreach(IFilter filter in Filters) {
@ -268,8 +272,8 @@ namespace Greenshot.Drawing {
int offset = lineThickness/2; int offset = lineThickness/2;
int shadow = 0; int shadow = 0;
if (accountForShadowChange || (HasField(FieldType.SHADOW) && GetFieldValueAsBool(FieldType.SHADOW))){ if (_accountForShadowChange || (HasField(FieldType.SHADOW) && GetFieldValueAsBool(FieldType.SHADOW))){
accountForShadowChange = false; _accountForShadowChange = false;
shadow += 10; shadow += 10;
} }
return new Rectangle(Bounds.Left-offset, Bounds.Top-offset, Bounds.Width+lineThickness+shadow, Bounds.Height+lineThickness+shadow); return new Rectangle(Bounds.Left-offset, Bounds.Top-offset, Bounds.Width+lineThickness+shadow, Bounds.Height+lineThickness+shadow);
@ -335,9 +339,9 @@ namespace Greenshot.Drawing {
Parent = _parent, Parent = _parent,
Location = location Location = location
}; };
_targetGripper.MouseDown += gripperMouseDown; _targetGripper.MouseDown += GripperMouseDown;
_targetGripper.MouseUp += gripperMouseUp; _targetGripper.MouseUp += GripperMouseUp;
_targetGripper.MouseMove += gripperMouseMove; _targetGripper.MouseMove += GripperMouseMove;
if (_parent != null) { if (_parent != null) {
_parent.Controls.Add(_targetGripper); // otherwise we'll attach them in switchParent _parent.Controls.Add(_targetGripper); // otherwise we'll attach them in switchParent
} }
@ -349,9 +353,9 @@ namespace Greenshot.Drawing {
for(int i=0; i<_grippers.Length; i++) { for(int i=0; i<_grippers.Length; i++) {
_grippers[i] = new Gripper(); _grippers[i] = new Gripper();
_grippers[i].Position = i; _grippers[i].Position = i;
_grippers[i].MouseDown += gripperMouseDown; _grippers[i].MouseDown += GripperMouseDown;
_grippers[i].MouseUp += gripperMouseUp; _grippers[i].MouseUp += GripperMouseUp;
_grippers[i].MouseMove += gripperMouseMove; _grippers[i].MouseMove += GripperMouseMove;
_grippers[i].Visible = false; _grippers[i].Visible = false;
_grippers[i].Parent = _parent; _grippers[i].Parent = _parent;
} }
@ -365,11 +369,11 @@ namespace Greenshot.Drawing {
} }
public void SuspendLayout() { public void SuspendLayout() {
layoutSuspended = true; _layoutSuspended = true;
} }
public void ResumeLayout() { public void ResumeLayout() {
layoutSuspended = false; _layoutSuspended = false;
DoLayout(); DoLayout();
} }
@ -377,9 +381,9 @@ namespace Greenshot.Drawing {
if (_grippers == null) { if (_grippers == null) {
return; return;
} }
if (!layoutSuspended) { if (!_layoutSuspended) {
int[] xChoords = new int[]{Left-2,Left+Width/2-2,Left+Width-2}; int[] xChoords = {Left-2,Left+Width/2-2,Left+Width-2};
int[] yChoords = new int[]{Top-2,Top+Height/2-2,Top+Height-2}; int[] yChoords = {Top-2,Top+Height/2-2,Top+Height-2};
_grippers[Gripper.POSITION_TOP_LEFT].Left = xChoords[0]; _grippers[Gripper.POSITION_TOP_LEFT].Top = yChoords[0]; _grippers[Gripper.POSITION_TOP_LEFT].Left = xChoords[0]; _grippers[Gripper.POSITION_TOP_LEFT].Top = yChoords[0];
_grippers[Gripper.POSITION_TOP_CENTER].Left = xChoords[1]; _grippers[Gripper.POSITION_TOP_CENTER].Top = yChoords[0]; _grippers[Gripper.POSITION_TOP_CENTER].Left = xChoords[1]; _grippers[Gripper.POSITION_TOP_CENTER].Top = yChoords[0];
@ -412,11 +416,7 @@ namespace Greenshot.Drawing {
} }
} }
int mx; private void GripperMouseDown(object sender, MouseEventArgs e) {
int my;
private void gripperMouseDown(object sender, MouseEventArgs e) {
mx = e.X;
my = e.Y;
Gripper originatingGripper = (Gripper)sender; Gripper originatingGripper = (Gripper)sender;
if (originatingGripper != _targetGripper) { if (originatingGripper != _targetGripper) {
Status = EditStatus.RESIZING; Status = EditStatus.RESIZING;
@ -428,7 +428,7 @@ namespace Greenshot.Drawing {
_isMadeUndoable = false; _isMadeUndoable = false;
} }
private void gripperMouseUp(object sender, MouseEventArgs e) { private void GripperMouseUp(object sender, MouseEventArgs e) {
Gripper originatingGripper = (Gripper)sender; Gripper originatingGripper = (Gripper)sender;
if (originatingGripper != _targetGripper) { if (originatingGripper != _targetGripper) {
_boundsBeforeResize = Rectangle.Empty; _boundsBeforeResize = Rectangle.Empty;
@ -439,7 +439,7 @@ namespace Greenshot.Drawing {
Invalidate(); Invalidate();
} }
private void gripperMouseMove(object sender, MouseEventArgs e) { private void GripperMouseMove(object sender, MouseEventArgs e) {
Gripper originatingGripper = (Gripper)sender; Gripper originatingGripper = (Gripper)sender;
int absX = originatingGripper.Left + e.X; int absX = originatingGripper.Left + e.X;
int absY = originatingGripper.Top + e.Y; int absY = originatingGripper.Top + e.Y;
@ -473,18 +473,7 @@ namespace Greenshot.Drawing {
Invalidate(); Invalidate();
} }
} }
private void childLabelMouseMove(object sender, MouseEventArgs e) {
if (Status.Equals(EditStatus.RESIZING)) {
Invalidate();
SuspendLayout();
Left += e.X - mx;
Top += e.Y - my;
ResumeLayout();
Invalidate();
}
}
public bool hasFilters { public bool hasFilters {
get { get {
return Filters.Count > 0; return Filters.Count > 0;
@ -648,7 +637,9 @@ namespace Greenshot.Drawing {
InitControls(); InitControls();
} }
_parent = newParent; _parent = newParent;
_parent.Controls.AddRange(_grippers); if (_grippers != null) {
_parent.Controls.AddRange(_grippers);
}
foreach(IFilter filter in Filters) { foreach(IFilter filter in Filters) {
filter.Parent = this; filter.Parent = this;
} }
@ -657,9 +648,9 @@ namespace Greenshot.Drawing {
// drawablecontainers are regarded equal if they are of the same type and their bounds are equal. this should be sufficient. // drawablecontainers are regarded equal if they are of the same type and their bounds are equal. this should be sufficient.
public override bool Equals(object obj) { public override bool Equals(object obj) {
bool ret = false; bool ret = false;
if (obj != null && GetType().Equals(obj.GetType())) { if (obj != null && GetType() == obj.GetType()) {
DrawableContainer other = obj as DrawableContainer; DrawableContainer other = obj as DrawableContainer;
if (_left==other._left && _top==other._top && _width==other._width && _height==other._height) { if (other != null && _left==other._left && _top==other._top && _width==other._width && _height==other._height) {
ret = true; ret = true;
} }
} }
@ -671,8 +662,8 @@ namespace Greenshot.Drawing {
} }
protected void OnPropertyChanged(string propertyName) { protected void OnPropertyChanged(string propertyName) {
if (propertyChanged != null) { if (_propertyChanged != null) {
propertyChanged(this, new PropertyChangedEventArgs(propertyName)); _propertyChanged(this, new PropertyChangedEventArgs(propertyName));
Invalidate(); Invalidate();
} }
} }
@ -696,7 +687,7 @@ namespace Greenshot.Drawing {
public void HandleFieldChanged(object sender, FieldChangedEventArgs e) { public void HandleFieldChanged(object sender, FieldChangedEventArgs e) {
LOG.DebugFormat("Field {0} changed", e.Field.FieldType); LOG.DebugFormat("Field {0} changed", e.Field.FieldType);
if (e.Field.FieldType == FieldType.SHADOW) { if (e.Field.FieldType == FieldType.SHADOW) {
accountForShadowChange = true; _accountForShadowChange = true;
} }
Invalidate(); Invalidate();
} }
@ -712,11 +703,19 @@ namespace Greenshot.Drawing {
return; return;
} }
Point center = new Point(Left + (Width/2), Top + (Height/2)); Point center = new Point(Left + (Width/2), Top + (Height/2));
Point[] points = { center }; Point[] points;
if (TargetGripper != null) {
points = new[] {center, TargetGripper.Location};
} else {
points = new[] { center};
}
matrix.TransformPoints(points); matrix.TransformPoints(points);
Left = points[0].X - (Width / 2); Location = new Point(points[0].X - (Width/2), points[0].Y - (Height/2));
Top = points[0].Y - (Height / 2); if (TargetGripper != null) {
TargetGripper.Location = points[1];
}
} }
public virtual void Rotate(RotateFlipType rotateFlipType) { public virtual void Rotate(RotateFlipType rotateFlipType) {
@ -778,13 +777,13 @@ namespace Greenshot.Drawing {
return ScaleHelper.ShapeAngleRoundBehavior.Instance; return ScaleHelper.ShapeAngleRoundBehavior.Instance;
} }
public virtual bool hasContextMenu { public virtual bool HasContextMenu {
get { get {
return true; return true;
} }
} }
public virtual bool hasDefaultSize { public virtual bool HasDefaultSize {
get { get {
return false; return false;
} }

View file

@ -509,7 +509,7 @@ namespace Greenshot.Drawing {
bool canReset = false; bool canReset = false;
foreach (var drawableContainer in this) { foreach (var drawableContainer in this) {
var container = (DrawableContainer) drawableContainer; var container = (DrawableContainer) drawableContainer;
if (container.hasDefaultSize) { if (container.HasDefaultSize) {
canReset = true; canReset = true;
} }
} }
@ -519,7 +519,7 @@ namespace Greenshot.Drawing {
item.Click += delegate { item.Click += delegate {
foreach (var drawableContainer in this) { foreach (var drawableContainer in this) {
var container = (DrawableContainer) drawableContainer; var container = (DrawableContainer) drawableContainer;
if (!container.hasDefaultSize) { if (!container.HasDefaultSize) {
continue; continue;
} }
Size defaultSize = container.DefaultSize; Size defaultSize = container.DefaultSize;
@ -538,7 +538,7 @@ namespace Greenshot.Drawing {
bool hasMenu = false; bool hasMenu = false;
foreach (var drawableContainer in this) { foreach (var drawableContainer in this) {
var container = (DrawableContainer) drawableContainer; var container = (DrawableContainer) drawableContainer;
if (!container.hasContextMenu) { if (!container.HasContextMenu) {
continue; continue;
} }
hasMenu = true; hasMenu = true;

View file

@ -58,7 +58,8 @@ namespace Greenshot.Drawing {
protected override void InitializeFields() { protected override void InitializeFields() {
AddField(GetType(), FieldType.LINE_THICKNESS, 3); AddField(GetType(), FieldType.LINE_THICKNESS, 3);
AddField(GetType(), FieldType.LINE_COLOR, Color.Red); AddField(GetType(), FieldType.LINE_COLOR, Color.Red);
} }
protected void Init() { protected void Init() {
if (_grippers != null) { if (_grippers != null) {
@ -108,10 +109,10 @@ namespace Greenshot.Drawing {
public override bool HandleMouseMove(int mouseX, int mouseY) { public override bool HandleMouseMove(int mouseX, int mouseY) {
Point previousPoint = capturePoints[capturePoints.Count-1]; Point previousPoint = capturePoints[capturePoints.Count-1];
if (GeometryHelper.Distance2D(previousPoint.X, previousPoint.Y, mouseX, mouseY) >= (2*editorConfig.FreehandSensitivity)) { if (GeometryHelper.Distance2D(previousPoint.X, previousPoint.Y, mouseX, mouseY) >= (2*EditorConfig.FreehandSensitivity)) {
capturePoints.Add(new Point(mouseX, mouseY)); capturePoints.Add(new Point(mouseX, mouseY));
} }
if (GeometryHelper.Distance2D(lastMouse.X, lastMouse.Y, mouseX, mouseY) >= editorConfig.FreehandSensitivity) { if (GeometryHelper.Distance2D(lastMouse.X, lastMouse.Y, mouseX, mouseY) >= EditorConfig.FreehandSensitivity) {
//path.AddCurve(new Point[]{lastMouse, new Point(mouseX, mouseY)}); //path.AddCurve(new Point[]{lastMouse, new Point(mouseX, mouseY)});
freehandPath.AddLine(lastMouse, new Point(mouseX, mouseY)); freehandPath.AddLine(lastMouse, new Point(mouseX, mouseY));
lastMouse = new Point(mouseX, mouseY); lastMouse = new Point(mouseX, mouseY);
@ -127,7 +128,7 @@ namespace Greenshot.Drawing {
/// </summary> /// </summary>
public override void HandleMouseUp(int mouseX, int mouseY) { public override void HandleMouseUp(int mouseX, int mouseY) {
// Make sure we don't loose the ending point // Make sure we don't loose the ending point
if (GeometryHelper.Distance2D(lastMouse.X, lastMouse.Y, mouseX, mouseY) >= editorConfig.FreehandSensitivity) { if (GeometryHelper.Distance2D(lastMouse.X, lastMouse.Y, mouseX, mouseY) >= EditorConfig.FreehandSensitivity) {
capturePoints.Add(new Point(mouseX, mouseY)); capturePoints.Add(new Point(mouseX, mouseY));
} }
RecalculatePath(); RecalculatePath();

View file

@ -87,7 +87,7 @@ namespace Greenshot.Drawing {
} }
} }
public override bool hasDefaultSize { public override bool HasDefaultSize {
get { get {
return true; return true;
} }

View file

@ -197,7 +197,7 @@ namespace Greenshot.Drawing {
} }
} }
public override bool hasDefaultSize { public override bool HasDefaultSize {
get { get {
return true; return true;
} }

File diff suppressed because it is too large Load diff

View file

@ -62,9 +62,9 @@ namespace GreenshotPlugin.Core {
} }
try { try {
// Get the index of the orientation property. // Get the index of the orientation property.
int orientation_index = Array.IndexOf(image.PropertyIdList, EXIF_ORIENTATION_ID); int orientationIndex = Array.IndexOf(image.PropertyIdList, EXIF_ORIENTATION_ID);
// If there is no such property, return Unknown. // If there is no such property, return Unknown.
if (orientation_index < 0) { if (orientationIndex < 0) {
return; return;
} }
PropertyItem item = image.GetPropertyItem(EXIF_ORIENTATION_ID); PropertyItem item = image.GetPropertyItem(EXIF_ORIENTATION_ID);
@ -129,18 +129,18 @@ namespace GreenshotPlugin.Core {
int srcWidth=image.Width; int srcWidth=image.Width;
int srcHeight=image.Height; int srcHeight=image.Height;
if (thumbHeight < 0) { if (thumbHeight < 0) {
thumbHeight = (int)(thumbWidth * ((float)srcHeight / (float)srcWidth)); thumbHeight = (int)(thumbWidth * (srcHeight / (float)srcWidth));
} }
if (thumbWidth < 0) { if (thumbWidth < 0) {
thumbWidth = (int)(thumbHeight * ((float)srcWidth / (float)srcHeight)); thumbWidth = (int)(thumbHeight * (srcWidth / (float)srcHeight));
} }
if (maxWidth > 0 && thumbWidth > maxWidth) { if (maxWidth > 0 && thumbWidth > maxWidth) {
thumbWidth = Math.Min(thumbWidth, maxWidth); thumbWidth = Math.Min(thumbWidth, maxWidth);
thumbHeight = (int)(thumbWidth * ((float)srcHeight / (float)srcWidth)); thumbHeight = (int)(thumbWidth * (srcHeight / (float)srcWidth));
} }
if (maxHeight > 0 && thumbHeight > maxHeight) { if (maxHeight > 0 && thumbHeight > maxHeight) {
thumbHeight = Math.Min(thumbHeight, maxHeight); thumbHeight = Math.Min(thumbHeight, maxHeight);
thumbWidth = (int)(thumbHeight * ((float)srcWidth / (float)srcHeight)); thumbWidth = (int)(thumbHeight * (srcWidth / (float)srcHeight));
} }
Bitmap bmp = new Bitmap(thumbWidth, thumbHeight); Bitmap bmp = new Bitmap(thumbWidth, thumbHeight);
@ -174,12 +174,13 @@ namespace GreenshotPlugin.Core {
LOG.Warn("Can't crop a null/zero size image!"); LOG.Warn("Can't crop a null/zero size image!");
return false; return false;
} }
/// <summary> /// <summary>
/// Private helper method for the FindAutoCropRectangle /// Private helper method for the FindAutoCropRectangle
/// </summary> /// </summary>
/// <param name="fastBitmap"></param> /// <param name="fastBitmap"></param>
/// <param name="colorPoint"></param> /// <param name="colorPoint"></param>
/// <param name="cropDifference"></param>
/// <returns>Rectangle</returns> /// <returns>Rectangle</returns>
private static Rectangle FindAutoCropRectangle(IFastBitmap fastBitmap, Point colorPoint, int cropDifference) { private static Rectangle FindAutoCropRectangle(IFastBitmap fastBitmap, Point colorPoint, int cropDifference) {
Rectangle cropRectangle = Rectangle.Empty; Rectangle cropRectangle = Rectangle.Empty;
@ -194,24 +195,26 @@ namespace GreenshotPlugin.Core {
int diffR = Math.Abs(currentColor.R - referenceColor.R); int diffR = Math.Abs(currentColor.R - referenceColor.R);
int diffG = Math.Abs(currentColor.G - referenceColor.G); int diffG = Math.Abs(currentColor.G - referenceColor.G);
int diffB = Math.Abs(currentColor.B - referenceColor.B); int diffB = Math.Abs(currentColor.B - referenceColor.B);
if (((diffR + diffG + diffB) / 3) > cropDifference) { if (((diffR + diffG + diffB)/3) <= cropDifference) {
if (x < min.X) min.X = x; continue;
if (y < min.Y) min.Y = y;
if (x > max.X) max.X = x;
if (y > max.Y) max.Y = y;
} }
if (x < min.X) min.X = x;
if (y < min.Y) min.Y = y;
if (x > max.X) max.X = x;
if (y > max.Y) max.Y = y;
} }
} }
} else { } else {
for (int y = 0; y < fastBitmap.Height; y++) { for (int y = 0; y < fastBitmap.Height; y++) {
for (int x = 0; x < fastBitmap.Width; x++) { for (int x = 0; x < fastBitmap.Width; x++) {
Color currentColor = fastBitmap.GetColorAt(x, y); Color currentColor = fastBitmap.GetColorAt(x, y);
if (referenceColor.Equals(currentColor)) { if (!referenceColor.Equals(currentColor)) {
if (x < min.X) min.X = x; continue;
if (y < min.Y) min.Y = y;
if (x > max.X) max.X = x;
if (y > max.Y) max.Y = y;
} }
if (x < min.X) min.X = x;
if (y < min.Y) min.Y = y;
if (x > max.X) max.X = x;
if (y > max.Y) max.Y = y;
} }
} }
} }
@ -228,10 +231,11 @@ namespace GreenshotPlugin.Core {
/// Get a rectangle for the image which crops the image of all colors equal to that on 0,0 /// Get a rectangle for the image which crops the image of all colors equal to that on 0,0
/// </summary> /// </summary>
/// <param name="image"></param> /// <param name="image"></param>
/// <param name="cropDifference"></param>
/// <returns>Rectangle</returns> /// <returns>Rectangle</returns>
public static Rectangle FindAutoCropRectangle(Image image, int cropDifference) { public static Rectangle FindAutoCropRectangle(Image image, int cropDifference) {
Rectangle cropRectangle = Rectangle.Empty; Rectangle cropRectangle = Rectangle.Empty;
Rectangle currentRectangle = Rectangle.Empty; Rectangle currentRectangle;
List<Point> checkPoints = new List<Point>(); List<Point> checkPoints = new List<Point>();
// Top Left // Top Left
checkPoints.Add(new Point(0, 0)); checkPoints.Add(new Point(0, 0));
@ -391,20 +395,20 @@ namespace GreenshotPlugin.Core {
/// <summary> /// <summary>
/// Get the number of icon in the file /// Get the number of icon in the file
/// </summary> /// </summary>
/// <param name="icon"></param>
/// <param name="location">Location of the EXE or DLL</param> /// <param name="location">Location of the EXE or DLL</param>
/// <returns></returns> /// <returns></returns>
public static int CountAssociatedIcons(string location) { public static int CountAssociatedIcons(string location) {
IntPtr large = IntPtr.Zero; IntPtr large;
IntPtr small = IntPtr.Zero; IntPtr small;
return Shell32.ExtractIconEx(location, -1, out large, out small, 0); return Shell32.ExtractIconEx(location, -1, out large, out small, 0);
} }
/// <summary> /// <summary>
/// Apply the effect to the bitmap /// Apply the effect to the bitmap
/// </summary> /// </summary>
/// <param name="sourceBitmap">Bitmap</param> /// <param name="sourceImage">Bitmap</param>
/// <param name="effect">IEffect</param> /// <param name="effect">IEffect</param>
/// <param name="matrix"></param>
/// <returns>Bitmap</returns> /// <returns>Bitmap</returns>
public static Image ApplyEffect(Image sourceImage, IEffect effect, Matrix matrix) { public static Image ApplyEffect(Image sourceImage, IEffect effect, Matrix matrix) {
List<IEffect> effects = new List<IEffect>(); List<IEffect> effects = new List<IEffect>();
@ -415,8 +419,9 @@ namespace GreenshotPlugin.Core {
/// <summary> /// <summary>
/// Apply the effects in the supplied order to the bitmap /// Apply the effects in the supplied order to the bitmap
/// </summary> /// </summary>
/// <param name="sourceBitmap">Bitmap</param> /// <param name="sourceImage">Bitmap</param>
/// <param name="effects">List<IEffect></param> /// <param name="effects">List<IEffect></param>
/// <param name="matrix"></param>
/// <returns>Bitmap</returns> /// <returns>Bitmap</returns>
public static Image ApplyEffects(Image sourceImage, List<IEffect> effects, Matrix matrix) { public static Image ApplyEffects(Image sourceImage, List<IEffect> effects, Matrix matrix) {
Image currentImage = sourceImage; Image currentImage = sourceImage;
@ -449,8 +454,8 @@ namespace GreenshotPlugin.Core {
Image returnImage = CreateEmpty(sourceImage.Width, sourceImage.Height, PixelFormat.Format32bppArgb, Color.Empty, sourceImage.HorizontalResolution, sourceImage.VerticalResolution); Image returnImage = CreateEmpty(sourceImage.Width, sourceImage.Height, PixelFormat.Format32bppArgb, Color.Empty, sourceImage.HorizontalResolution, sourceImage.VerticalResolution);
using (GraphicsPath path = new GraphicsPath()) { using (GraphicsPath path = new GraphicsPath()) {
Random random = new Random(); Random random = new Random();
int HorizontalRegions = (int)(sourceImage.Width / horizontalToothRange); int horizontalRegions = sourceImage.Width / horizontalToothRange;
int VerticalRegions = (int)(sourceImage.Height / verticalToothRange); int verticalRegions = sourceImage.Height / verticalToothRange;
// Start // Start
Point previousEndingPoint = new Point(0,0); Point previousEndingPoint = new Point(0,0);
@ -458,8 +463,8 @@ namespace GreenshotPlugin.Core {
if (edges[0]) { if (edges[0]) {
previousEndingPoint = new Point(horizontalToothRange, random.Next(1, toothHeight)); previousEndingPoint = new Point(horizontalToothRange, random.Next(1, toothHeight));
// Top // Top
for (int i = 0; i < HorizontalRegions; i++) { for (int i = 0; i < horizontalRegions; i++) {
int x = (int)previousEndingPoint.X + horizontalToothRange; int x = previousEndingPoint.X + horizontalToothRange;
int y = random.Next(1, toothHeight); int y = random.Next(1, toothHeight);
newEndingPoint = new Point(x, y); newEndingPoint = new Point(x, y);
path.AddLine(previousEndingPoint, newEndingPoint); path.AddLine(previousEndingPoint, newEndingPoint);
@ -472,9 +477,9 @@ namespace GreenshotPlugin.Core {
} }
if (edges[1]) { if (edges[1]) {
// Right // Right
for (int i = 0; i < VerticalRegions; i++) { for (int i = 0; i < verticalRegions; i++) {
int x = sourceImage.Width - random.Next(1, toothHeight); int x = sourceImage.Width - random.Next(1, toothHeight);
int y = (int)previousEndingPoint.Y + verticalToothRange; int y = previousEndingPoint.Y + verticalToothRange;
newEndingPoint = new Point(x, y); newEndingPoint = new Point(x, y);
path.AddLine(previousEndingPoint, newEndingPoint); path.AddLine(previousEndingPoint, newEndingPoint);
previousEndingPoint = newEndingPoint; previousEndingPoint = newEndingPoint;
@ -486,8 +491,8 @@ namespace GreenshotPlugin.Core {
} }
if (edges[2]) { if (edges[2]) {
// Bottom // Bottom
for (int i = 0; i < HorizontalRegions; i++) { for (int i = 0; i < horizontalRegions; i++) {
int x = (int)previousEndingPoint.X - horizontalToothRange; int x = previousEndingPoint.X - horizontalToothRange;
int y = sourceImage.Height - random.Next(1, toothHeight); int y = sourceImage.Height - random.Next(1, toothHeight);
newEndingPoint = new Point(x, y); newEndingPoint = new Point(x, y);
path.AddLine(previousEndingPoint, newEndingPoint); path.AddLine(previousEndingPoint, newEndingPoint);
@ -500,9 +505,9 @@ namespace GreenshotPlugin.Core {
} }
if (edges[3]) { if (edges[3]) {
// Left // Left
for (int i = 0; i < VerticalRegions; i++) { for (int i = 0; i < verticalRegions; i++) {
int x = random.Next(1, toothHeight); int x = random.Next(1, toothHeight);
int y = (int)previousEndingPoint.Y - verticalToothRange; int y = previousEndingPoint.Y - verticalToothRange;
newEndingPoint = new Point(x, y); newEndingPoint = new Point(x, y);
path.AddLine(previousEndingPoint, newEndingPoint); path.AddLine(previousEndingPoint, newEndingPoint);
previousEndingPoint = newEndingPoint; previousEndingPoint = newEndingPoint;
@ -510,7 +515,7 @@ namespace GreenshotPlugin.Core {
} else { } else {
newEndingPoint = new Point(0, 0); newEndingPoint = new Point(0, 0);
path.AddLine(previousEndingPoint, newEndingPoint); path.AddLine(previousEndingPoint, newEndingPoint);
previousEndingPoint = newEndingPoint; //previousEndingPoint = newEndingPoint;
} }
path.CloseFigure(); path.CloseFigure();
@ -522,7 +527,7 @@ namespace GreenshotPlugin.Core {
graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
using (Brush brush = new TextureBrush(sourceImage)) { using (Brush brush = new TextureBrush(sourceImage)) {
// Imporant note: If the target wouldn't be at 0,0 we need to translate-transform!! // Important note: If the target wouldn't be at 0,0 we need to translate-transform!!
graphics.FillPath(brush, path); graphics.FillPath(brush, path);
} }
} }
@ -530,24 +535,6 @@ namespace GreenshotPlugin.Core {
return returnImage; return returnImage;
} }
/// <summary>
/// Helper Method for the ApplyBlur
/// </summary>
/// <param name="amount"></param>
/// <returns></returns>
private static int[] CreateGaussianBlurRow(int amount) {
int size = 1 + (amount * 2);
int[] weights = new int[size];
for (int i = 0; i <= amount; ++i) {
// 1 + aa - aa + 2ai - ii
weights[i] = 16 * (i + 1);
weights[weights.Length - i - 1] = weights[i];
}
return weights;
}
/// <summary> /// <summary>
/// Apply BoxBlur to the destinationBitmap /// Apply BoxBlur to the destinationBitmap
/// </summary> /// </summary>
@ -638,7 +625,6 @@ namespace GreenshotPlugin.Core {
/// <summary> /// <summary>
/// BoxBlurHorizontal is a private helper method for the BoxBlur, only for IFastBitmaps with alpha channel /// BoxBlurHorizontal is a private helper method for the BoxBlur, only for IFastBitmaps with alpha channel
/// </summary> /// </summary>
/// <param name="bbbSrc">Source BitmapBuffer</param>
/// <param name="targetFastBitmap">Target BitmapBuffer</param> /// <param name="targetFastBitmap">Target BitmapBuffer</param>
/// <param name="range">Range must be odd!</param> /// <param name="range">Range must be odd!</param>
private static void BoxBlurHorizontalAlpha(IFastBitmap targetFastBitmap, int range) { private static void BoxBlurHorizontalAlpha(IFastBitmap targetFastBitmap, int range) {
@ -694,11 +680,8 @@ namespace GreenshotPlugin.Core {
if (targetFastBitmap.hasAlphaChannel) { if (targetFastBitmap.hasAlphaChannel) {
throw new NotSupportedException("BoxBlurVertical should NOT be called for bitmaps with alpha channel"); throw new NotSupportedException("BoxBlurVertical should NOT be called for bitmaps with alpha channel");
} }
int w = targetFastBitmap.Width;
int halfRange = range / 2; int halfRange = range / 2;
Color[] newColors = new Color[targetFastBitmap.Height]; Color[] newColors = new Color[targetFastBitmap.Height];
int oldPixelOffset = -(halfRange + 1) * w;
int newPixelOffset = (halfRange) * w;
byte[] tmpColor = new byte[4]; byte[] tmpColor = new byte[4];
for (int x = targetFastBitmap.Left; x < targetFastBitmap.Right; x++) { for (int x = targetFastBitmap.Left; x < targetFastBitmap.Right; x++) {
int hits = 0; int hits = 0;
@ -745,11 +728,8 @@ namespace GreenshotPlugin.Core {
throw new NotSupportedException("BoxBlurVerticalAlpha should be called for bitmaps with alpha channel"); throw new NotSupportedException("BoxBlurVerticalAlpha should be called for bitmaps with alpha channel");
} }
int w = targetFastBitmap.Width;
int halfRange = range / 2; int halfRange = range / 2;
Color[] newColors = new Color[targetFastBitmap.Height]; Color[] newColors = new Color[targetFastBitmap.Height];
int oldPixelOffset = -(halfRange + 1) * w;
int newPixelOffset = (halfRange) * w;
byte[] tmpColor = new byte[4]; byte[] tmpColor = new byte[4];
for (int x = targetFastBitmap.Left; x < targetFastBitmap.Right; x++) { for (int x = targetFastBitmap.Left; x < targetFastBitmap.Right; x++) {
int hits = 0; int hits = 0;
@ -818,13 +798,14 @@ namespace GreenshotPlugin.Core {
/// <param name="darkness">How dark is the shadow</param> /// <param name="darkness">How dark is the shadow</param>
/// <param name="shadowSize">Size of the shadow</param> /// <param name="shadowSize">Size of the shadow</param>
/// <param name="targetPixelformat">What pixel format must the returning bitmap have</param> /// <param name="targetPixelformat">What pixel format must the returning bitmap have</param>
/// <param name="offset">How many pixels is the original image moved?</param> /// <param name="shadowOffset"></param>
/// <param name="matrix">The transform matrix which describes how the elements need to be transformed to stay at the same location</param>
/// <returns>Bitmap with the shadow, is bigger than the sourceBitmap!!</returns> /// <returns>Bitmap with the shadow, is bigger than the sourceBitmap!!</returns>
public static Bitmap CreateShadow(Image sourceBitmap, float darkness, int shadowSize, Point shadowOffset, Matrix matrix, PixelFormat targetPixelformat) { public static Bitmap CreateShadow(Image sourceBitmap, float darkness, int shadowSize, Point shadowOffset, Matrix matrix, PixelFormat targetPixelformat) {
Point offset = shadowOffset; Point offset = shadowOffset;
offset.X += shadowSize - 1; offset.X += shadowSize - 1;
offset.Y += shadowSize - 1; offset.Y += shadowSize - 1;
matrix.Translate(offset.X, offset.Y); matrix.Translate(offset.X, offset.Y, MatrixOrder.Append);
// Create a new "clean" image // Create a new "clean" image
Bitmap returnImage = CreateEmpty(sourceBitmap.Width + (shadowSize * 2), sourceBitmap.Height + (shadowSize * 2), targetPixelformat, Color.Empty, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution); Bitmap returnImage = CreateEmpty(sourceBitmap.Width + (shadowSize * 2), sourceBitmap.Height + (shadowSize * 2), targetPixelformat, Color.Empty, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution);
// Make sure the shadow is odd, there is no reason for an even blur! // Make sure the shadow is odd, there is no reason for an even blur!
@ -865,7 +846,7 @@ namespace GreenshotPlugin.Core {
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
// draw original with a TextureBrush so we have nice antialiasing! // draw original with a TextureBrush so we have nice antialiasing!
using (Brush textureBrush = new TextureBrush(sourceBitmap, WrapMode.Clamp)) { using (Brush textureBrush = new TextureBrush(sourceBitmap, WrapMode.Clamp)) {
// We need to do a translate-tranform otherwise the image is wrapped // We need to do a translate-transform otherwise the image is wrapped
graphics.TranslateTransform(offset.X, offset.Y); graphics.TranslateTransform(offset.X, offset.Y);
graphics.FillRectangle(textureBrush, 0, 0, sourceBitmap.Width, sourceBitmap.Height); graphics.FillRectangle(textureBrush, 0, 0, sourceBitmap.Width, sourceBitmap.Height);
} }
@ -880,7 +861,7 @@ namespace GreenshotPlugin.Core {
/// <returns>Negative bitmap</returns> /// <returns>Negative bitmap</returns>
public static Bitmap CreateNegative(Image sourceImage) { public static Bitmap CreateNegative(Image sourceImage) {
Bitmap clone = (Bitmap)Clone(sourceImage); Bitmap clone = (Bitmap)Clone(sourceImage);
ColorMatrix invertMatrix = new ColorMatrix(new float[][] { ColorMatrix invertMatrix = new ColorMatrix(new[] {
new float[] {-1, 0, 0, 0, 0}, new float[] {-1, 0, 0, 0, 0},
new float[] {0, -1, 0, 0, 0}, new float[] {0, -1, 0, 0, 0},
new float[] {0, 0, -1, 0, 0}, new float[] {0, 0, -1, 0, 0},
@ -980,12 +961,12 @@ namespace GreenshotPlugin.Core {
/// <param name="borderSize">Size of the border</param> /// <param name="borderSize">Size of the border</param>
/// <param name="borderColor">Color of the border</param> /// <param name="borderColor">Color of the border</param>
/// <param name="targetPixelformat">What pixel format must the returning bitmap have</param> /// <param name="targetPixelformat">What pixel format must the returning bitmap have</param>
/// <param name="offset">How many pixels is the original image moved?</param> /// <param name="matrix">The transform matrix which describes how the elements need to be transformed to stay at the same location</param>
/// <returns>Bitmap with the shadow, is bigger than the sourceBitmap!!</returns> /// <returns>Bitmap with the shadow, is bigger than the sourceBitmap!!</returns>
public static Image CreateBorder(Image sourceImage, int borderSize, Color borderColor, PixelFormat targetPixelformat, Matrix matrix) { public static Image CreateBorder(Image sourceImage, int borderSize, Color borderColor, PixelFormat targetPixelformat, Matrix matrix) {
// "return" the shifted offset, so the caller can e.g. move elements // "return" the shifted offset, so the caller can e.g. move elements
Point offset = new Point(borderSize, borderSize); Point offset = new Point(borderSize, borderSize);
matrix.Translate(offset.X, offset.Y); matrix.Translate(offset.X, offset.Y, MatrixOrder.Append);
// Create a new "clean" image // Create a new "clean" image
Bitmap newImage = CreateEmpty(sourceImage.Width + (borderSize * 2), sourceImage.Height + (borderSize * 2), targetPixelformat, Color.Empty, sourceImage.HorizontalResolution, sourceImage.VerticalResolution); Bitmap newImage = CreateEmpty(sourceImage.Width + (borderSize * 2), sourceImage.Height + (borderSize * 2), targetPixelformat, Color.Empty, sourceImage.HorizontalResolution, sourceImage.VerticalResolution);
@ -1039,11 +1020,15 @@ namespace GreenshotPlugin.Core {
attributes.SetGamma(gamma, ColorAdjustType.Bitmap); attributes.SetGamma(gamma, ColorAdjustType.Bitmap);
return attributes; return attributes;
} }
/// <summary> /// <summary>
/// Adjust the brightness, contract or gamma of an image. /// Adjust the brightness, contract or gamma of an image.
/// Use the value "1.0f" for no changes. /// Use the value "1.0f" for no changes.
/// </summary> /// </summary>
/// <param name="sourceImage">Original bitmap</param> /// <param name="sourceImage">Original bitmap</param>
/// <param name="brightness"></param>
/// <param name="contrast"></param>
/// <param name="gamma"></param>
/// <returns>Bitmap with grayscale</returns> /// <returns>Bitmap with grayscale</returns>
public static Image Adjust(Image sourceImage, float brightness, float contrast, float gamma) { public static Image Adjust(Image sourceImage, float brightness, float contrast, float gamma) {
//create a blank bitmap the same size as original //create a blank bitmap the same size as original
@ -1284,9 +1269,10 @@ namespace GreenshotPlugin.Core {
/// <param name="right"></param> /// <param name="right"></param>
/// <param name="top"></param> /// <param name="top"></param>
/// <param name="bottom"></param> /// <param name="bottom"></param>
/// <param name="matrix"></param>
/// <returns>a new bitmap with the source copied on it</returns> /// <returns>a new bitmap with the source copied on it</returns>
public static Image ResizeCanvas(Image sourceImage, Color backgroundColor, int left, int right, int top, int bottom, Matrix matrix) { public static Image ResizeCanvas(Image sourceImage, Color backgroundColor, int left, int right, int top, int bottom, Matrix matrix) {
matrix.Translate(left, top); matrix.Translate(left, top, MatrixOrder.Append);
Bitmap newBitmap = CreateEmpty(sourceImage.Width + left + right, sourceImage.Height + top + bottom, sourceImage.PixelFormat, backgroundColor, sourceImage.HorizontalResolution, sourceImage.VerticalResolution); Bitmap newBitmap = CreateEmpty(sourceImage.Width + left + right, sourceImage.Height + top + bottom, sourceImage.PixelFormat, backgroundColor, sourceImage.HorizontalResolution, sourceImage.VerticalResolution);
using (Graphics graphics = Graphics.FromImage(newBitmap)) { using (Graphics graphics = Graphics.FromImage(newBitmap)) {
graphics.DrawImageUnscaled(sourceImage, left, top); graphics.DrawImageUnscaled(sourceImage, left, top);
@ -1301,6 +1287,7 @@ namespace GreenshotPlugin.Core {
/// <param name="maintainAspectRatio">true to maintain the aspect ratio</param> /// <param name="maintainAspectRatio">true to maintain the aspect ratio</param>
/// <param name="newWidth"></param> /// <param name="newWidth"></param>
/// <param name="newHeight"></param> /// <param name="newHeight"></param>
/// <param name="matrix"></param>
/// <returns></returns> /// <returns></returns>
public static Image ResizeImage(Image sourceImage, bool maintainAspectRatio, int newWidth, int newHeight, Matrix matrix) { public static Image ResizeImage(Image sourceImage, bool maintainAspectRatio, int newWidth, int newHeight, Matrix matrix) {
return ResizeImage(sourceImage, maintainAspectRatio, false, Color.Empty, newWidth, newHeight, matrix); return ResizeImage(sourceImage, maintainAspectRatio, false, Color.Empty, newWidth, newHeight, matrix);
@ -1310,7 +1297,7 @@ namespace GreenshotPlugin.Core {
/// Count how many times the supplied color exists /// Count how many times the supplied color exists
/// </summary> /// </summary>
/// <param name="sourceImage">Image to count the pixels of</param> /// <param name="sourceImage">Image to count the pixels of</param>
/// <param name="colorToCount">Color to count/param> /// <param name="colorToCount">Color to count</param>
/// <param name="includeAlpha">true if Alpha needs to be checked</param> /// <param name="includeAlpha">true if Alpha needs to be checked</param>
/// <returns>int with the number of pixels which have colorToCount</returns> /// <returns>int with the number of pixels which have colorToCount</returns>
public static int CountColor(Image sourceImage, Color colorToCount, bool includeAlpha) { public static int CountColor(Image sourceImage, Color colorToCount, bool includeAlpha) {
@ -1340,9 +1327,11 @@ namespace GreenshotPlugin.Core {
/// </summary> /// </summary>
/// <param name="sourceImage">Image to scale</param> /// <param name="sourceImage">Image to scale</param>
/// <param name="maintainAspectRatio">true to maintain the aspect ratio</param> /// <param name="maintainAspectRatio">true to maintain the aspect ratio</param>
/// <param name="canvasUseNewSize"></param>
/// <param name="backgroundColor">The color to fill with, or Color.Empty to take the default depending on the pixel format</param> /// <param name="backgroundColor">The color to fill with, or Color.Empty to take the default depending on the pixel format</param>
/// <param name="newWidth">new width</param> /// <param name="newWidth">new width</param>
/// <param name="newHeight">new height</param> /// <param name="newHeight">new height</param>
/// <param name="matrix"></param>
/// <returns>a new bitmap with the specified size, the source-Image scaled to fit with aspect ratio locked</returns> /// <returns>a new bitmap with the specified size, the source-Image scaled to fit with aspect ratio locked</returns>
public static Image ResizeImage(Image sourceImage, bool maintainAspectRatio, bool canvasUseNewSize, Color backgroundColor, int newWidth, int newHeight, Matrix matrix) { public static Image ResizeImage(Image sourceImage, bool maintainAspectRatio, bool canvasUseNewSize, Color backgroundColor, int newWidth, int newHeight, Matrix matrix) {
int destX = 0; int destX = 0;
@ -1351,8 +1340,8 @@ namespace GreenshotPlugin.Core {
float nPercentW = 0; float nPercentW = 0;
float nPercentH = 0; float nPercentH = 0;
nPercentW = ((float)newWidth / (float)sourceImage.Width); nPercentW = (newWidth / (float)sourceImage.Width);
nPercentH = ((float)newHeight / (float)sourceImage.Height); nPercentH = (newHeight / (float)sourceImage.Height);
if (maintainAspectRatio) { if (maintainAspectRatio) {
if (nPercentW == 1) { if (nPercentW == 1) {
nPercentW = nPercentH; nPercentW = nPercentH;
@ -1388,10 +1377,10 @@ namespace GreenshotPlugin.Core {
Image newImage = null; Image newImage = null;
if (maintainAspectRatio && canvasUseNewSize) { if (maintainAspectRatio && canvasUseNewSize) {
newImage = CreateEmpty(newWidth, newHeight, sourceImage.PixelFormat, backgroundColor, sourceImage.HorizontalResolution, sourceImage.VerticalResolution); newImage = CreateEmpty(newWidth, newHeight, sourceImage.PixelFormat, backgroundColor, sourceImage.HorizontalResolution, sourceImage.VerticalResolution);
matrix.Scale(sourceImage.Width / newWidth, sourceImage.Height / newHeight); matrix.Scale((float)newWidth / sourceImage.Width, (float)newHeight / sourceImage.Height);
} else { } else {
newImage = CreateEmpty(destWidth, destHeight, sourceImage.PixelFormat, backgroundColor, sourceImage.HorizontalResolution, sourceImage.VerticalResolution); newImage = CreateEmpty(destWidth, destHeight, sourceImage.PixelFormat, backgroundColor, sourceImage.HorizontalResolution, sourceImage.VerticalResolution);
matrix.Scale(sourceImage.Width / destWidth, sourceImage.Height / destHeight); matrix.Scale((float)destWidth / sourceImage.Width, (float)destHeight / sourceImage.Height);
} }
using (Graphics graphics = Graphics.FromImage(newImage)) { using (Graphics graphics = Graphics.FromImage(newImage)) {