/// <summary> /// Rotates all existing rings (Except those flagged ignore) /// </summary> /// <param name="degrees"></param> /// <param name="origin"></param> /// <returns></returns> public void Rotate(double degrees, PointF origin) { foreach (PointSet ps in ringList) { if (ps.flags != PointFlags.Ignored) { ps.points = PolygonMath.RotatePoly(ps.points, degrees, origin); } } }
public LayoutInformation(ImageState state) { this.instructions = new NameValueCollection(state.settings); this.sourceRect = new SizeOnly(state.originalSize); this.finalRect = new SizeOnly(state.destSize); this.imageSourcePoly = new PolyRect(state.copyRect); if (state.layout.ContainsRing("image")) { this.imageDestPoly = new PolyRect(state.layout["image"]); } if (state.layout.ContainsRing("imageArea")) { this.imageDestAreaPoly = new PolyRect(state.layout["imageArea"]); } // Check to see if sFlip/sRotate has altered the original raw image // rectangle. The check must be the same as in // ImageBuilder.PrepareSourceBitmap(). Note that the adjustment // happens only when there's an actual bitmap, regardless of the // sFlip/sRotate settings. if (state.sourceBitmap != null && (state.settings.SourceFlip != RotateFlipType.RotateNoneFlipNone || !string.IsNullOrEmpty(state.settings["sRotate"]))) { // We need to calculate the original rect/poly by *reversing* the // requested sFlip/sRotate. We determine what the requested change // was, then calculate the reverse. var angle = state.settings.Get <double>("sRotate", 0); var flipRotate = (int)PolygonMath.CombineFlipAndRotate(state.settings.SourceFlip, angle); var copyPoly = PolygonMath.ToPoly(state.copyRect); var trueOriginalSize = state.originalSize; // The RotateFlipType values are ordered such that odd values // transpose the size of the rectangle, %4 gives the rotation // and /4 (=> 0 or 1) whether there's been an x-flip. We can // use this to streamline our calculations. if (flipRotate % 2 == 1) { trueOriginalSize = new Size(state.originalSize.Height, state.originalSize.Width); } this.preAdjustedSourceRect = new SizeOnly(trueOriginalSize); // Remember that the sFlip/sRotate change performed the rotation // first and then the flip, so we have to do the opposite to go // backwards. if (flipRotate / 4 == 1) { copyPoly = PolygonMath.ScalePoints(copyPoly, -1, 1, PointF.Empty); copyPoly = PolygonMath.MovePoly(copyPoly, new PointF(trueOriginalSize.Width, 0)); } // It's possible to calculate a rotation-origin that will place // the original pre-sRotate (0,0) point back at (0,0) again... // but since it involves sqrt(), there would be rounding errors // that we should be able to avoid. (We might, in fact, want to // avoid using PolygonMath entirely, and hand-map the points // backwards for accuracy.) switch (flipRotate % 4) { case 0: // no rotation // no-op! break; case 1: // 90 degrees, clockwise copyPoly = PolygonMath.RotatePoly(copyPoly, -90); copyPoly = PolygonMath.MovePoly(copyPoly, new PointF(0, trueOriginalSize.Height)); break; case 2: // 180 degrees, clockwise copyPoly = PolygonMath.RotatePoly(copyPoly, -180); copyPoly = PolygonMath.MovePoly(copyPoly, new PointF(trueOriginalSize.Width, trueOriginalSize.Height)); break; case 3: // 270 degrees, clockwise copyPoly = PolygonMath.RotatePoly(copyPoly, -270); copyPoly = PolygonMath.MovePoly(copyPoly, new PointF(trueOriginalSize.Width, 0)); break; } this.preAdjustedImageSourcePoly = new PolyRect(copyPoly); } }
public override void RenderTo(Resizing.ImageState s) { if (string.IsNullOrEmpty(Text)) { return; } string finalText = Text; if (finalText.IndexOf('#') > -1) { Regex r = new Regex("\\#\\{([^}]+)\\}"); finalText = r.Replace(finalText, delegate(Match m){ string val = s.settings[m.Groups[1].Value]; if (val == null) { return(""); } else { return(val); } }); } SizeF naturalSize = SizeF.Empty; SizeF unrotatedSize = SizeF.Empty; RectangleF bounds = this.CalculateLayerCoordinates(s, delegate(double maxwidth, double maxheight) { using (Font f = GetFont()) using (StringFormat sf = GetFormat()){ naturalSize = s.destGraphics.MeasureString(finalText, f, new PointF(), sf); SizeF size = naturalSize; unrotatedSize = Fill ? PolygonMath.ScaleInside(size, new SizeF((float)maxwidth, (float)maxheight)) : size; if (Angle != 0) { size = PolygonMath.GetBoundingBox(PolygonMath.RotatePoly(PolygonMath.ToPoly(new RectangleF(new PointF(0, 0), size)), Angle)).Size; } if (Fill) { size = PolygonMath.ScaleInside(size, new SizeF((float)maxwidth, (float)maxheight)); } f.FontFamily.Dispose(); return(PolygonMath.RoundPoints(size)); } }, true); using (Font f = GetFont()) { s.destGraphics.SmoothingMode = SmoothingMode.HighQuality; s.destGraphics.TextRenderingHint = Rendering; // Utils.parseEnum<TextRenderingHint>(s.settings["watermark.rendering"], this.Rendering); ; s.destGraphics.PixelOffsetMode = PixelOffsetMode.HighQuality; s.destGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic; s.destGraphics.CompositingMode = CompositingMode.SourceOver; s.destGraphics.CompositingQuality = CompositingQuality.HighQuality; s.destGraphics.ResetTransform(); if (Angle != 0) { s.destGraphics.RotateTransform((float)Angle); } s.destGraphics.ScaleTransform(unrotatedSize.Width / naturalSize.Width, unrotatedSize.Height / naturalSize.Height); s.destGraphics.TranslateTransform(bounds.X, bounds.Y, MatrixOrder.Append); using (StringFormat sf = GetFormat()) { DrawString(s.destGraphics, finalText, f, new Point(0, 0), sf); } s.destGraphics.ResetTransform(); f.FontFamily.Dispose(); } }