// // Helper function to generate shadow // private void GenerateShapeShadow(CGContext context, UIBezierPath shapeObject, nfloat xOffset, nfloat yOffset, nfloat blurValue, CGBlendMode blendingMode, UIColor shadowColor, nfloat borderWidth, int borderPosition, nfloat iWidth, nfloat iHeight) { CGPoint basePoint; CGPoint offsetPoint; CGSize calculatedShadowOffset; nfloat calculatedShadowBlur; CGPoint constPointZero; constPointZero = new CGPoint(0, 0); basePoint = baseTransform.TransformPoint(context.PointToDeviceSpace(constPointZero)); offsetPoint = baseTransform.TransformPoint(context.PointToDeviceSpace(new CGPoint(xOffset, yOffset))); calculatedShadowOffset = new CGSize(offsetPoint.X - basePoint.X, offsetPoint.Y - basePoint.Y); if (blurValue == 0) { calculatedShadowBlur = 0; } else { calculatedShadowBlur = Hypot(calculatedShadowOffset.Width, calculatedShadowOffset.Height) / blurValue; } context.SetShadow(calculatedShadowOffset, calculatedShadowBlur, shadowColor.CGColor); context.SetBlendMode(blendingMode); context.BeginTransparencyLayer(null); UIColor.Black.SetFill(); shapeObject.Fill(); if (borderWidth > 0) { if (borderPosition == 0) { context.SaveState(); shapeObject.LineWidth = borderWidth; UIColor.Black.SetStroke(); shapeObject.Stroke(); context.RestoreState(); } if (borderPosition == 1) { context.BeginPath(); context.AddPath(shapeObject.CGPath); context.EOClip(); } if (borderPosition == 2) { context.BeginPath(); context.AddPath(shapeObject.CGPath); context.AddRect(RectangleFExtensions.Inset(shapeObject.Bounds, iWidth, iHeight)); context.EOClip(); } } context.EndTransparencyLayer(); }
void DrawBackground(CGRect rect, CGContext context, CGColor color, UIBezierPath roundedRectanglePath) { context.SaveState(); context.BeginTransparencyLayer(null); if (roundedRectanglePath != null) { roundedRectanglePath.AddClip(); } context.SetFillColor(color); context.FillRect(rect); context.EndTransparencyLayer(); context.RestoreState(); }
// http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_shadings/dq_shadings.html#//apple_ref/doc/uid/TP30001066-CH207-BBCECJBF internal override void Setup(Graphics graphics, bool fill) { CGContext context = graphics.context; // if fill is false then we are being called from a Pen stroke so // we need to setup a transparency layer // http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_shadings/dq_shadings.html#//apple_ref/doc/uid/TP30001066-CH207-BBCECJBF if (!fill) { context.BeginTransparencyLayer(); hasTransparencyLayer = true; // Make sure we set a color here so that the gradient shows up graphics.lastBrushColor = Color.Black; return; } // if this is the same as the last that was set and no changes have been made // then return. if (graphics.LastBrush != this || changed) { //setupShadingColors(); } // Transform the start and end points using the brush's transformation matrix gradientTransform.TransformPoints(pathPoints); RasterizePolygon(context, centerPoint, pathPoints, surroundColors, centerColor); // If we are in a Transparency layer then we need to end the transparency if (hasTransparencyLayer) { context.EndTransparencyLayer(); } changed = false; graphics.LastBrush = this; // We will reset the last pen so that it can be setup again // so that we do not loose the settings after stroking the gradient // not sure where the setting are being reset so this may be a hack // and things are just not being restored correctly. graphics.LastPen = null; // I am setting this to be used for Text coloring in DrawString graphics.lastBrushColor = surroundColors[surroundColors.Length - 1]; }
// http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_shadings/dq_shadings.html#//apple_ref/doc/uid/TP30001066-CH207-BBCECJBF internal override void Setup(Graphics graphics, bool fill) { CGContext context = graphics.context; // if fill is false then we are being called from a Pen stroke so // we need to setup a transparency layer // http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_shadings/dq_shadings.html#//apple_ref/doc/uid/TP30001066-CH207-BBCECJBF if (!fill) { context.BeginTransparencyLayer(); hasTransparencyLayer = true; // Make sure we set a color here so that the gradient shows up graphics.lastBrushColor = Color.Black; return; } // if this is the same as the last that was set and no changes have been made // then return. if (graphics.LastBrush != this || changed) { setupShadingColors(); } CGSize gradientRegionCg = context.GetClipBoundingBox().Size; SizeF gradientRegion = new SizeF((float)gradientRegionCg.Width, (float)gradientRegionCg.Height); // SizeF gradientRegionPath = context.GetPathBoundingBox().Size; PointF sp = startPoint; PointF ep = endPoint; PointF[] points = { sp, ep }; // Transform the start and end points using the brush's transformation matrix gradientTransform.TransformPoints(points); sp = points[0]; ep = points[1]; var cgf = CyclicGradientFunction(gradientRegion, ref sp, ref ep); var colorSpace = CGColorSpace.CreateDeviceRGB(); CGPoint cgsp = new CGPoint(sp.X, sp.Y), cgep = new CGPoint(ep.X, ep.Y); var shading = CGShading.CreateAxial(colorSpace, cgsp, cgep, cgf, false, false); colorSpace.Dispose(); context.SaveState(); // if path is empty here then we are being called from a Pen stroke so // we set the blend mode for strokes and clip the path for fills if (!context.IsPathEmpty()) { context.Clip(); } else { context.SetBlendMode(CGBlendMode.SourceIn); } context.DrawShading(shading); context.RestoreState(); // If we are in a Transparency layer then we need to end the transparency if (hasTransparencyLayer) { context.EndTransparencyLayer(); } cgf.Dispose(); shading.Dispose(); shading = null; changed = false; graphics.LastBrush = this; // We will reset the last pen so that it can be setup again // so that we do not loose the settings after stroking the gradient // not sure where the setting are being reset so this may be a hack // and things are just not being restored correctly. graphics.LastPen = null; // I am setting this to be used for Text coloring in DrawString graphics.lastBrushColor = colors[0]; }
public override void Draw(CGRect area) { if (image == null) { return; } CGContext context = UIGraphics.GetCurrentContext(); // //----------------------------------------------------------- // Draw bottom layer containing image preview //----------------------------------------------------------- // if (layerBottom == null) { CGLayer layer = CGLayer.Create(context, this.Bounds.Size); CGContext layerContext = layer.Context; layerContext.SetFillColor(this.BackgroundColor.CGColor); layerContext.FillRect(this.Bounds); CGAffineTransform transform = new CGAffineTransform(1, 0, 0, -1, 0, this.Bounds.Size.Height); layerContext.ConcatCTM(transform); layerContext.DrawImage(WholeImageRectInView(), image.CGImage); layerBottom = layer; } context.DrawLayer(layerBottom, new CGPoint(0, 0)); //----------------------------------------------------------- // Draw transparency layer containing edges around page area //----------------------------------------------------------- { CGPoint[] point = new CGPoint[4]; GetCornersCoordinates(point, false); // Preserve the current drawing state context.SaveState(); context.BeginTransparencyLayer(null); context.SetLineWidth(1.0f); context.SetStrokeColor(0.0f, 0.0f, 1.0f, 1.0f); //----------------------------------------------------------------- // Create complex path as intersection of subpath 1 with subpath 2 //----------------------------------------------------------------- // Create subpath 1 with the whole image size context.SetFillColor(0, 0, 0, 0.4f); context.AddRect(this.Bounds); // Create subpath 2 which cuts a page area from the subpath 1 context.MoveTo(point[0].X, point[0].Y); context.AddLineToPoint(point[1].X, point[1].Y); context.AddLineToPoint(point[3].X, point[3].Y); context.AddLineToPoint(point[2].X, point[2].Y); context.AddLineToPoint(point[0].X, point[0].Y); context.EOFillPath(); //----------------------------------------------------------------- // Create a new path which draws edges around page area //----------------------------------------------------------------- // Draw edge around page context.SetFillColor(0, 0, 1, 1); context.BeginPath(); context.MoveTo(point[0].X, point[0].Y); context.AddLineToPoint(point[1].X, point[1].Y); context.AddLineToPoint(point[3].X, point[3].Y); context.AddLineToPoint(point[2].X, point[2].Y); context.AddLineToPoint(point[0].X, point[0].Y); context.StrokePath(); context.EndTransparencyLayer(); // Restore the previous drawing state. context.RestoreState(); } }