diff --git a/Greenshot/Drawing/BitmapContainer.cs b/Greenshot/Drawing/BitmapContainer.cs index 40ac7bc02..fd4cb496c 100644 --- a/Greenshot/Drawing/BitmapContainer.cs +++ b/Greenshot/Drawing/BitmapContainer.cs @@ -100,7 +100,16 @@ namespace Greenshot.Drawing { LOG.Debug("Loaded file: " + filename + " with resolution: " + Height + "," + Width); } } - + + public override void Rotate(RotateFlipType rotateFlipType) { + Bitmap newBitmap = ImageHelper.RotateFlip((Bitmap)bitmap, rotateFlipType); + if (bitmap != null) { + bitmap.Dispose(); + } + bitmap = newBitmap; + base.Rotate(rotateFlipType); + } + public override void Draw(Graphics graphics, RenderMode rm) { if (bitmap != null) { bool shadow = GetFieldValueAsBool(FieldType.SHADOW); diff --git a/Greenshot/Drawing/DrawableContainer.cs b/Greenshot/Drawing/DrawableContainer.cs index 57cbf8ce1..02cb111b2 100644 --- a/Greenshot/Drawing/DrawableContainer.cs +++ b/Greenshot/Drawing/DrawableContainer.cs @@ -590,7 +590,50 @@ namespace Greenshot.Drawing { } Invalidate(); } - + + public virtual void Rotate(RotateFlipType rotateFlipType) { + // somehow the rotation is the wrong way? + int angle = 90; + if (RotateFlipType.Rotate90FlipNone == rotateFlipType) { + angle = 270; + } + int ox = 0; + int oy = 0; + int centerX = parent.Width >> 1; + int centerY = parent.Height >> 1; + // Transform from screen to normal coordinates + int px = Left - centerX; + int py = centerY - Top; + double theta = Math.PI * angle / 180.0; + double x1 = Math.Cos(theta) * (px - ox) - Math.Sin(theta) * (py - oy) + ox; + double y1 = Math.Sin(theta) * (px - ox) + Math.Cos(theta) * (py - oy) + oy; + + // Transform from screen to normal coordinates + px = (Left + Width) - centerX; + py = centerY - (Top + Height); + + double x2 = Math.Cos(theta) * (px - ox) - Math.Sin(theta) * (py - oy) + ox; + double y2 = Math.Sin(theta) * (px - ox) + Math.Cos(theta) * (py - oy) + oy; + + // Transform back to screen coordinates, as we rotate the bitmap we need to switch the center X&Y + x1 += centerY; + y1 = centerX - y1; + x2 += centerY; + y2 = centerX - y2; + + // Calculate to rectangle + double newWidth = x2 - x1; + double newHeight = y2 - y1; + + RectangleF newRectangle = new RectangleF( + (float)x1, + (float)y1, + (float)newWidth, + (float)newHeight + ); + ApplyBounds(newRectangle); + } + protected virtual ScaleHelper.IDoubleProcessor GetAngleRoundProcessor() { return ScaleHelper.ShapeAngleRoundBehavior.Instance; } diff --git a/Greenshot/Drawing/Surface.cs b/Greenshot/Drawing/Surface.cs index e3ba91116..43e80d4f8 100644 --- a/Greenshot/Drawing/Surface.cs +++ b/Greenshot/Drawing/Surface.cs @@ -572,44 +572,16 @@ namespace Greenshot.Drawing { newImage = ImageHelper.CreateGrayscale((Bitmap)Image); break; case Effects.Rotate90: + case Effects.Rotate270: MakeUndoable(new DrawableContainerBoundsChangeMemento(elements.AsIDrawableContainerList()), false); - foreach(DrawableContainer drawableContainer in elements) { - int angle = 270; - int ox = 0; - int oy = 0; - int centerX = Width>>1; - int centerY = Height>>1; - int px = drawableContainer.Bounds.X - centerX; - int py = centerY - drawableContainer.Bounds.Y; - double theta = Math.PI * angle / 180.0; - double x1 = Math.Cos(theta) * (px-ox) - Math.Sin(theta) * (py-oy) + ox; - double y1 = Math.Sin(theta) * (px-ox) + Math.Cos(theta) * (py-oy) + oy; - - // Transform to Cartesian coordinates - px = (drawableContainer.Bounds.X+drawableContainer.Bounds.Width) - centerX; - py = centerY - (drawableContainer.Bounds.Y+drawableContainer.Bounds.Height); - - double x2 = Math.Cos(theta) * (px-ox) - Math.Sin(theta) * (py-oy) + ox; - double y2 = Math.Sin(theta) * (px-ox) + Math.Cos(theta) * (py-oy) + oy; - x1 += centerY; - x2 += centerY; - y1 = centerX - y1; - y2 = centerX - y2; - - // Calculate to rectangle - double newWidth = x2 - x1; - double newHeight = y2 - y1; - - RectangleF newRectangle = new RectangleF( - (float)x1, - (float)y1, - (float)newWidth, - (float)newHeight - ); - LOG.DebugFormat("Bounds before {0}, bounds after {1}", drawableContainer.Bounds, newRectangle); - drawableContainer.ApplyBounds(newRectangle); + RotateFlipType rotateFlipType = RotateFlipType.Rotate270FlipNone; + if (effect == Effects.Rotate90) { + rotateFlipType = RotateFlipType.Rotate90FlipNone; } - newImage = ImageHelper.RotateFlip((Bitmap)Image, RotateFlipType.Rotate90FlipNone); + foreach (DrawableContainer drawableContainer in elements) { + drawableContainer.Rotate(rotateFlipType); + } + newImage = ImageHelper.RotateFlip((Bitmap)Image, rotateFlipType); break; }