public override void ViewDidLoad() { base.ViewDidLoad(); chart.BindingX = "Name"; chart.Series.Add(new Series(chart, "Sales, Sales", "Sales")); chart.Series.Add(new Series(chart, "Expenses, Expenses", "Expenses")); chart.ItemsSource = SalesData.GetSalesDataList(); chart.AxisX.Title = "Country"; chart.AxisX.LineWidth = 2; chart.AxisX.MinorTickWidth = 1; chart.AxisX.MajorTickWidth = 0; chart.AxisX.IsHandleXFAxisLabelLoading = true; chart.AxisY.LineWidth = 2; chart.AxisY.MinorGridVisible = true; chart.AxisY.MinorGridWidth = 0.5; chart.AxisY.MinorGridDashes = new double[]{ 4, 4}; chart.AxisY.MinorTickWidth = 1; chart.AxisY.MajorTickWidth = 2; chart.AxisY.MajorGridWidth = 1; chart.AxisY.MajorGridColor = UIColor.FromWhiteAlpha(0.8f, 1.0f); chart.AxisY.MajorGridFill = UIColor.FromWhiteAlpha(0.6f, 0.2f); chart.AxisY.MajorUnit = 1000; chart.AxisY.Max = 10000; chart.AxisY.IsHandleXFAxisLabelLoading = true; chart.HandleLabelLoading += delegate (FlexChart sender, XuniRenderEngine renderEngine, int axisIndex, XuniLabelLoadingEventArgs eventArgs) { if (axisIndex == (int)AxisType.Y) { if (eventArgs.Value <= 3000) { renderEngine.SetTextFill(UIColor.Red); } else if (eventArgs.Value <= 10000 && eventArgs.Value > 6000) { renderEngine.SetTextFill(UIColor.Green); } else { renderEngine.SetTextFill(UIColor.Black); } eventArgs.Label = "$" + (eventArgs.Value / 1000).ToString() + "K"; return false; } else if (axisIndex == (int)AxisType.X) { UIImage img = new UIImage("Images/" + eventArgs.Label); img.Draw(new CoreGraphics.CGRect(eventArgs.Region.Left, eventArgs.Region.Top, eventArgs.Region.Width, eventArgs.Region.Height)); eventArgs.Label = ""; return true; } return false; }; }
public byte[] ResizeImage(byte[] imageData, float width, float height) { UIImage originalImage = ImageFromByteArray(imageData); UIImageOrientation orientImg = originalImage.Orientation; float oldWidth = (float)originalImage.Size.Width; float oldHeight = (float)originalImage.Size.Height; float scaleFactor = 0f; if (oldWidth > oldHeight) { scaleFactor = width / oldWidth; } else { scaleFactor = height / oldHeight; } float newHeight = oldHeight * scaleFactor; float newWidth = oldWidth * scaleFactor; //create a 24bit RGB image using (CGBitmapContext context = new CGBitmapContext(IntPtr.Zero, (int)newWidth, (int)newHeight, 8, (int)(4 * newWidth), CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedFirst)) { RectangleF imageRect = new RectangleF(0, 0, newWidth, newHeight); // draw the image context.DrawImage(imageRect, originalImage.CGImage); //UIKit.UIImage resizedImage = UIKit.UIImage.FromImage(context.ToImage()); //Das skalierte Bild um 90 Grad drehen //UIKit.UIImage resizedImage = UIKit.UIImage.FromImage(context.ToImage(), 1, UIImageOrientation.Right); UIKit.UIImage resizedImage = UIKit.UIImage.FromImage(context.ToImage(), 1, orientImg); //Das gedrehte Bild neu rendern, höhe und Breite vertauscht um das Seitenverhältniss beizubehalten UIGraphics.BeginImageContextWithOptions(new CGSize((float)resizedImage.CGImage.Height, (float)resizedImage.CGImage.Width), true, 1.0f); //TODO: Height und Width sind evtl. vertauscht resizedImage.Draw(new CGRect(0, 0, (float)resizedImage.CGImage.Height, (float)resizedImage.CGImage.Width)); //TODO: Height und Width sind evtl. vertauscht var resultImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); resizedImage = resultImage; return(resizedImage.AsJPEG(100).ToArray()); // save the image as a jpeg //return resizedImage.AsJPEG(100).ToArray(); } }
private UIImage ResizeImage (UIImage view) { UIImage resultImage = null; var newSize = new CGSize (view.Size.Width / 2, view.Size.Height / 2); UIGraphics.BeginImageContext (newSize); view.Draw (new CGRect (0, 0, newSize.Width, newSize.Height)); resultImage = UIGraphics.GetImageFromCurrentImageContext (); UIGraphics.EndImageContext (); return resultImage; }
// resize the image to be contained within a maximum width and height, keeping aspect ratio public UIImage MaxResizeImage(UIImage sourceImage, float maxWidth, float maxHeight) { var sourceSize = sourceImage.Size; var maxResizeFactor = Math.Max(maxWidth / sourceSize.Width, maxHeight / sourceSize.Height); if (maxResizeFactor > 1) return sourceImage; var width = maxResizeFactor * sourceSize.Width; var height = maxResizeFactor * sourceSize.Height; UIGraphics.BeginImageContext(new CGSize(width, height)); sourceImage.Draw(new CGRect(0, 0, width, height)); var resultImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return resultImage; }
public static byte[] ResizeImage(byte[] originalImage, int newHeight, int newWidth) { byte[] resulImage = null; UIKit.UIImage uiImage = originalImage.ToImage(); UIGraphics.BeginImageContext(new CoreGraphics.CGSize(newWidth, newHeight)); uiImage.Draw(new CoreGraphics.CGRect(0, 0, newWidth, newHeight)); UIImage resultUIImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); resulImage = resultUIImage.AsJPEG().ToArray(); return(resulImage); }
public static UIImage MaxResizeImage(UIImage sourceImage, float maxWidth, float maxHeight) { var sourceSize = sourceImage.Size; var maxResizeFactor = Math.Max(maxWidth / sourceSize.Width, maxHeight / sourceSize.Height); if (maxResizeFactor > 1) return sourceImage; float width = (float)(maxResizeFactor * sourceSize.Width); float height = (float)(maxResizeFactor * sourceSize.Height); UIGraphics.BeginImageContext(new SizeF(width, height)); sourceImage.Draw(new RectangleF((float)0, (float)0, width, height)); var resultImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return resultImage; }
public static UIImage FixOrientation(UIImage image) { if (image.Orientation == UIImageOrientation.Up) { return image; } UIGraphics.BeginImageContextWithOptions(image.Size, false, image.CurrentScale); image.Draw(new CGRect(0, 0, image.Size.Width, image.Size.Height)); UIImage newImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return newImage; }
private UIImage InvertedImage(UIImage originalImage) { // Invert the image by applying an affine transformation UIGraphics.BeginImageContext (originalImage.Size); // Apply an affine transformation to the original image to generate a vertically flipped image CGContext context = UIGraphics.GetCurrentContext (); var affineTransformationInvert = new CGAffineTransform(1, 0, 0, -1, 0, originalImage.Size.Height); context.ConcatCTM(affineTransformationInvert); originalImage.Draw (PointF.Empty); UIImage invertedImage = UIGraphics.GetImageFromCurrentImageContext (); UIGraphics.EndImageContext (); return invertedImage; }
public static UIImage ScaledToSize(UIImage image, CGSize newSize){ CGRect scaledImageRect = new CGRect(0,0,0,0); nfloat aspectWidth = newSize.Width / image.Size.Width; nfloat aspectHeight = newSize.Height / image.Size.Height; nfloat aspectRatio = (nfloat)Math.Min (aspectWidth, aspectHeight); scaledImageRect.Size = new CGSize (image.Size.Width * aspectRatio, image.Size.Height * aspectRatio); scaledImageRect.X = (newSize.Width - scaledImageRect.Size.Width) / 2; scaledImageRect.Y = (newSize.Height - scaledImageRect.Size.Height) / 2; UIGraphics.BeginImageContext (newSize); image.Draw (scaledImageRect); UIImage scaledImage = UIGraphics.GetImageFromCurrentImageContext (); UIGraphics.EndImageContext (); return scaledImage; }
/// <summary> /// Resizes the image to have the specified maximum sizes. /// </summary> /// <param name="source">The source image to resize.</param> /// <param name="maxWidth">The max width of the image.</param> /// <param name="maxHeight">The max height of the image.</param> /// <returns>The resized image data.</returns> public override Stream ResizeImage(Stream source, int maxWidth, int maxHeight) { // Load the bitmap using (var originalBitmap = new UIKit.UIImage(NSData.FromStream(source))) { // If the image does not need to be resized var originalWidth = originalBitmap.Size.Width; var originalHeight = originalBitmap.Size.Height; if ((originalWidth <= maxWidth) && (originalHeight <= maxHeight)) { return(source); } // Calculate the resized image size var resizedHeight = (float)originalHeight; var resizedWidth = (float)originalWidth; // If height is greater than the maximum height if (resizedHeight > maxHeight) { resizedHeight = maxHeight; var factor = (float)originalHeight / maxHeight; resizedWidth = (float)originalWidth / factor; } // If the width is greater than the maximum width if (resizedWidth > maxWidth) { resizedWidth = maxWidth; var factor = (float)originalWidth / maxWidth; resizedHeight = (float)originalHeight / factor; } // Resize the image UIGraphics.BeginImageContext(new SizeF(resizedWidth, resizedHeight)); originalBitmap.Draw(new RectangleF(0, 0, resizedWidth, resizedHeight)); var resizedBitmap = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); // Save the rescaled image var imageData = resizedBitmap.AsJPEG(0.95f).ToArray(); resizedBitmap.Dispose(); return(new MemoryStream(imageData)); } }
static UIImage CreateBubbleWithBorder(UIImage bubbleImg, UIColor bubbleColor, UIImage borderImg, UIColor borderColor) { bubbleImg = CreateColoredImage (bubbleColor, bubbleImg); borderImg = CreateColoredImage (borderColor, borderImg); CGSize size = bubbleImg.Size; UIEdgeInsets caps = CenterPointEdgeInsetsForImageSize (size); UIGraphics.BeginImageContextWithOptions (size, false, 0); var rect = new CGRect (CGPoint.Empty, size); bubbleImg.Draw (rect); borderImg.Draw (rect); var result = UIGraphics.GetImageFromCurrentImageContext (); UIGraphics.EndImageContext (); result = result.CreateResizableImage (caps); return result; }
public static UIImage Blend(this UIImage image, nfloat alpha, CGColor fillColor, CGBlendMode blendMode = CGBlendMode.Normal) { UIGraphics.BeginImageContextWithOptions(image.Size, false, 0); var rect = new CGRect(0, 0, image.Size.Width, image.Size.Height); if (fillColor != null) { var context = UIGraphics.GetCurrentContext(); context.SetFillColor(fillColor); context.FillRect(rect); } image.Draw(rect, blendMode, alpha); var newImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return(newImage); }
/// <summary> /// Resizes an image. /// </summary> /// <returns>The resized image.</returns> /// <param name="originalImage">Original image.</param> /// <param name="newHeight">New height.</param> /// <param name="newWidth">New width.</param> /// <param name="imageFormat">Image format Jpg or Png.</param> public async Task <byte[]> ResizeImageAsync(byte[] originalImage, int newHeight, int newWidth, ImageFormat imageFormat) { byte[] resulImage = null; UIKit.UIImage uiImage = await originalImage.ToImageAsync(); UIGraphics.BeginImageContext(new CoreGraphics.CGSize(newWidth, newHeight)); uiImage.Draw(new CoreGraphics.CGRect(0, 0, newWidth, newHeight)); UIImage resultUIImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); if (imageFormat == ImageFormat.JPG) { resulImage = resultUIImage.AsJPEG().ToArray(); } else { resulImage = resultUIImage.AsPNG().ToArray(); } return(resulImage); }
public static UIImage ToRounded(UIImage source, nfloat rad) { nfloat size = (nfloat)Math.Min(source.Size.Width, source.Size.Height); UIGraphics.BeginImageContextWithOptions(new CGSize(size, size), false, (nfloat)0.0); try { CGRect bounds = new CGRect(0, 0, size, size); using (var path = UIBezierPath.FromRoundedRect(bounds, rad)) { path.AddClip(); source.Draw(bounds); var newImage = UIGraphics.GetImageFromCurrentImageContext(); return newImage; } } finally { UIGraphics.EndImageContext(); } }
public override void Draw(CGRect rect) { // Load image UIImage image = new UIImage("logo.png"); // Get drawing context CGContext ctx = UIGraphics.GetCurrentContext(); // get view bounds CGRect bounds = this.Bounds; // Find the center of view CGPoint center = new CGPoint(); center.X = bounds.X + bounds.Width / 2; center.Y = bounds.Y + bounds.Height / 2; // Compute the max radius of the circle float maxRadius = (float)Math.Sqrt(Math.Pow(bounds.Size.Width,2) + Math.Pow(bounds.Size.Height,2)) / 3.0f; ctx.SaveState(); // draw outline circle with 1 pt black shadow ctx.AddArc(center.X, center.Y, maxRadius, 0, (float)(Math.PI * 2), true); CGSize offset = new CGSize(0, 1); CGColor shadowColor = UIColor.Black.CGColor; ctx.SetShadow(offset, 2, shadowColor); ctx.DrawPath(CGPathDrawingMode.Stroke); // clear shadow ctx.RestoreState(); // set clipping circle ctx.AddArc(center.X, center.Y, maxRadius, 0, (float)(Math.PI * 2), true); ctx.Clip(); // Draw the image in the circle image.Draw(bounds); CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB(); nfloat[] components = new nfloat[8]{0.8f, 0.8f, 1, 1, 0.8f, 0.8f, 1, 0}; nfloat[] locations = new nfloat[2]{0.0f, 1}; CGGradient gradient = new CGGradient(colorSpace, components, locations); ctx.DrawLinearGradient(gradient, new CGPoint(bounds.Width / 2, 0), new CGPoint(bounds.Width / 2, bounds.Height / 2), 0); }
// // This method is invoked when the application has loaded and is ready to run. In this // method you should instantiate the window, load the UI into it and then make the window // visible. // // You have 17 seconds to return from this method, or iOS will terminate your application. // public override bool FinishedLaunching (UIApplication app, NSDictionary options) { window = new UIWindow (UIScreen.MainScreen.Bounds); viewController = new MergeImagesViewController (); image1 = UIImage.FromFile ("monkey1.png"); image2 = UIImage.FromFile ("monkey2.png"); UIGraphics.BeginImageContext (UIScreen.MainScreen.Bounds.Size); image1.Draw (new CGRect ( 0, 0, UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height)); image2.Draw (new CGRect ( UIScreen.MainScreen.Bounds.Width / 4, UIScreen.MainScreen.Bounds.Height / 4, UIScreen.MainScreen.Bounds.Width / 2, UIScreen.MainScreen.Bounds.Height / 2)); combinedImage = UIGraphics.GetImageFromCurrentImageContext (); UIGraphics.EndImageContext (); combinedImageView = new UIImageView ( new CGRect (0, 0, UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height)); combinedImageView.Image = combinedImage; viewController.View.AddSubview (combinedImageView); window.RootViewController = viewController; window.MakeKeyAndVisible (); return true; }
private static UIImage FixOrientation(UIImage image) { if (image.Orientation == UIImageOrientation.Up) return image; UIGraphics.BeginImageContextWithOptions(image.Size, false, image.CurrentScale); image.Draw(new CGRect(new CGPoint(0, 0), image.Size)); var normalizedImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return normalizedImage; }
// resize the image to be contained within a maximum width and height, keeping aspect ratio internal static UIImage MaxResizeImage(UIImage sourceImage, nfloat maxWidth, nfloat maxHeight) { if (sourceImage == null || maxWidth <= 0 || maxHeight <= 0) return null; var sourceSize = sourceImage.Size; var maxResizeFactor = (nfloat)Math.Max(maxWidth / sourceSize.Width, maxHeight / sourceSize.Height); if (maxResizeFactor > 1) return sourceImage; var width = maxResizeFactor * sourceSize.Width; var height = maxResizeFactor * sourceSize.Height; UIGraphics.BeginImageContext(new CGSize(width, height)); sourceImage.Draw(new CGRect(0, 0, width, height)); var resultImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return resultImage; }
// Return void for Archiving method of saving public UIImage getThumbnailFromImage(UIImage image) { CGSize origImageSize = image.Size; // The Rectangle of the thumbnail CGRect newRect = new CGRect(0, 0, 40, 40); // Figure out a scaling ration to make sure we maintain the same aspect ratio nfloat ratio = (nfloat)Math.Max(newRect.Size.Width / origImageSize.Width, newRect.Size.Height / origImageSize.Height); // Create a transparent bitmap context with a scaling factor equal to that of the screen UIGraphics.BeginImageContextWithOptions(newRect.Size, false, 0.0f); // Create a path that is a rounded rectangle UIBezierPath path = UIBezierPath.FromRoundedRect(newRect, 5.0f); // Make all subsequent drawing clip to this rounded rectangle path.AddClip(); // Center the image in the thumbnail rectangle CGRect projectRect = new CGRect(); projectRect.Width = ratio * origImageSize.Width; projectRect.Height = ratio * origImageSize.Height; projectRect.X = (newRect.Size.Width - projectRect.Size.Width) / 2.0f; projectRect.Y = (newRect.Size.Height - projectRect.Size.Height) / 2.0f; // Draw the image to the context image.Draw(projectRect); // Get the image from the image context, keep it as our thumbnail UIImage smallImage = UIGraphics.GetImageFromCurrentImageContext(); // thumbnail = smallImage; // Archiving method of saving // // Get the PNG representation of the image and set it as our archivable data // Archiving method of saving // NSData data = smallImage.AsPNG(); // Archiving method of saving // thumbnailData = data; // Archiving method of saving // Clean up image context resources, we're done UIGraphics.EndImageContext(); // return thumbnail image // Archiving method of saving return smallImage; // remove for Archiving method of saving }
protected static UIImage CreateBubbleWithBorder(UIImage bubbleImg, UIColor bubbleColor) { bubbleImg = CreateColoredImage (bubbleColor, bubbleImg); CGSize size = bubbleImg.Size; UIGraphics.BeginImageContextWithOptions (size, false, 0); var rect = new CGRect (CGPoint.Empty, size); bubbleImg.Draw (rect); var result = UIGraphics.GetImageFromCurrentImageContext (); UIGraphics.EndImageContext (); return result; }
// crop the image, without resizing private static UIImage CropImage (UIImage sourceImage, int crop_x, int crop_y, int width, int height) { var imgSize = sourceImage.Size; UIGraphics.BeginImageContext (new CGSize (width, height)); var context = UIGraphics.GetCurrentContext (); var clippedRect = new CGRect (0, 0, width, height); context.ClipToRect (clippedRect); var drawRect = new CGRect (-crop_x, -crop_y, imgSize.Width, imgSize.Height); sourceImage.Draw (drawRect); var modifiedImage = UIGraphics.GetImageFromCurrentImageContext (); UIGraphics.EndImageContext (); return modifiedImage; }
// Prevent accidents by rounding the edges of the image internal static UIImage RemoveSharpEdges (UIImage image) { UIGraphics.BeginImageContext (new SizeF (48, 48)); var c = UIGraphics.GetCurrentContext (); c.BeginPath (); c.MoveTo (48, 24); c.AddArcToPoint (48, 48, 24, 48, 4); c.AddArcToPoint (0, 48, 0, 24, 4); c.AddArcToPoint (0, 0, 24, 0, 4); c.AddArcToPoint (48, 0, 48, 24, 4); c.ClosePath (); c.Clip (); image.Draw (new PointF (0, 0)); var converted = UIGraphics.GetImageFromCurrentImageContext (); UIGraphics.EndImageContext (); return converted; }
public static UIImage ToTransformedCorners(UIImage source, double topLeftCornerSize, double topRightCornerSize, double bottomLeftCornerSize, double bottomRightCornerSize, CornerTransformType cornersTransformType, double cropWidthRatio, double cropHeightRatio) { double sourceWidth = source.Size.Width; double sourceHeight = source.Size.Height; double desiredWidth = sourceWidth; double desiredHeight = sourceHeight; double desiredRatio = cropWidthRatio / cropHeightRatio; double currentRatio = sourceWidth / sourceHeight; if (currentRatio > desiredRatio) desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio); else if (currentRatio < desiredRatio) desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio); topLeftCornerSize = topLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100; topRightCornerSize = topRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100; bottomLeftCornerSize = bottomLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100; bottomRightCornerSize = bottomRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100; float cropX = (float)((sourceWidth - desiredWidth) / 2); float cropY = (float)((sourceHeight - desiredHeight) / 2); UIGraphics.BeginImageContextWithOptions(new CGSize(desiredWidth, desiredHeight), false, (nfloat)0.0); try { using (var context = UIGraphics.GetCurrentContext()) { context.BeginPath(); using (var path = new UIBezierPath()) { // TopLeft if (cornersTransformType.HasFlag(CornerTransformType.TopLeftCut)) { path.MoveTo(new CGPoint(0, topLeftCornerSize)); path.AddLineTo(new CGPoint(topLeftCornerSize, 0)); } else if (cornersTransformType.HasFlag(CornerTransformType.TopLeftRounded)) { path.MoveTo(new CGPoint(0, topLeftCornerSize)); path.AddQuadCurveToPoint(new CGPoint(topLeftCornerSize, 0), new CGPoint(0, 0)); } else { path.MoveTo(new CGPoint(0, 0)); } // TopRight if (cornersTransformType.HasFlag(CornerTransformType.TopRightCut)) { path.AddLineTo(new CGPoint(desiredWidth - topRightCornerSize, 0)); path.AddLineTo(new CGPoint(desiredWidth, topRightCornerSize)); } else if (cornersTransformType.HasFlag(CornerTransformType.TopRightRounded)) { path.AddLineTo(new CGPoint(desiredWidth - topRightCornerSize, 0)); path.AddQuadCurveToPoint(new CGPoint(desiredWidth, topRightCornerSize), new CGPoint(desiredWidth, 0)); } else { path.AddLineTo(new CGPoint(desiredWidth ,0)); } // BottomRight if (cornersTransformType.HasFlag(CornerTransformType.BottomRightCut)) { path.AddLineTo(new CGPoint(desiredWidth, desiredHeight - bottomRightCornerSize)); path.AddLineTo(new CGPoint(desiredWidth - bottomRightCornerSize, desiredHeight)); } else if (cornersTransformType.HasFlag(CornerTransformType.BottomRightRounded)) { path.AddLineTo(new CGPoint(desiredWidth, desiredHeight - bottomRightCornerSize)); path.AddQuadCurveToPoint(new CGPoint(desiredWidth - bottomRightCornerSize, desiredHeight), new CGPoint(desiredWidth, desiredHeight)); } else { path.AddLineTo(new CGPoint(desiredWidth, desiredHeight)); } // BottomLeft if (cornersTransformType.HasFlag(CornerTransformType.BottomLeftCut)) { path.AddLineTo(new CGPoint(bottomLeftCornerSize, desiredHeight)); path.AddLineTo(new CGPoint(0, desiredHeight - bottomLeftCornerSize)); } else if (cornersTransformType.HasFlag(CornerTransformType.BottomLeftRounded)) { path.AddLineTo(new CGPoint(bottomLeftCornerSize, desiredHeight)); path.AddQuadCurveToPoint(new CGPoint(0, desiredHeight - bottomLeftCornerSize), new CGPoint(0, desiredHeight)); } else { path.AddLineTo(new CGPoint(0, desiredHeight)); } path.ClosePath(); context.AddPath(path.CGPath); context.Clip(); } var drawRect = new CGRect(-cropX, -cropY, sourceWidth, sourceHeight); source.Draw(drawRect); var modifiedImage = UIGraphics.GetImageFromCurrentImageContext(); return modifiedImage; } } finally { UIGraphics.EndImageContext(); } }
public static UIImage ToCropped(UIImage source, double zoomFactor, double xOffset, double yOffset, double cropWidthRatio, double cropHeightRatio) { double sourceWidth = source.Size.Width; double sourceHeight = source.Size.Height; double desiredWidth = sourceWidth; double desiredHeight = sourceHeight; double desiredRatio = cropWidthRatio / cropHeightRatio; double currentRatio = sourceWidth / sourceHeight; if (currentRatio > desiredRatio) desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio); else if (currentRatio < desiredRatio) desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio); xOffset = xOffset * desiredWidth; yOffset = yOffset * desiredHeight; desiredWidth = desiredWidth / zoomFactor; desiredHeight = desiredHeight / zoomFactor; float cropX = (float)(((sourceWidth - desiredWidth) / 2) + xOffset); float cropY = (float)(((sourceHeight - desiredHeight) / 2) + yOffset); if (cropX < 0) cropX = 0; if (cropY < 0) cropY = 0; if (cropX + desiredWidth > sourceWidth) cropX = (float)(sourceWidth - desiredWidth); if (cropY + desiredHeight > sourceHeight) cropY = (float)(sourceHeight - desiredHeight); UIGraphics.BeginImageContextWithOptions(new CGSize(desiredWidth, desiredHeight), false, (nfloat)0.0); try { using (var context = UIGraphics.GetCurrentContext()) { var clippedRect = new CGRect(0, 0, desiredWidth, desiredHeight); context.BeginPath(); using (var path = UIBezierPath.FromRect(clippedRect)) { context.AddPath(path.CGPath); context.Clip(); } var drawRect = new CGRect(-cropX, -cropY, sourceWidth, sourceHeight); source.Draw(drawRect); var modifiedImage = UIGraphics.GetImageFromCurrentImageContext(); return modifiedImage; } } finally { UIGraphics.EndImageContext(); } }
void DrawRecipeImage (UIImage image, CGRect rect) { // Create a new rect based on the size of the header area CGRect imageRect = CGRect.Empty; // Scale the image to fit in the infoRect float maxImageDimension = RecipeInfoHeight - Padding * 2; // HACK: Change float to nfloat nfloat largestImageDimension = (nfloat)Math.Max (image.Size.Width, image.Size.Height); nfloat scale = maxImageDimension / largestImageDimension; imageRect.Size = new CGSize (image.Size.Width * scale, image.Size.Height * scale); // Place the image rect at the x,y defined by the argument rect imageRect.Location = new CGPoint (rect.Left + Padding, rect.Top + Padding); // Ask the image to draw in the image rect image.Draw (imageRect); }
// crop the image, without resizing private UIImage CropImage(UIImage sourceImage, float crop_x, float crop_y, float width, float height) { var imgSize = sourceImage.Size; UIGraphics.BeginImageContext(new SizeF(width, height)); var context = UIGraphics.GetCurrentContext(); var clippedRect = new RectangleF(0, 0, width, height); context.ClipToRect(clippedRect); var drawRect = new RectangleF(-crop_x, -crop_y, (float)imgSize.Width, (float)imgSize.Height); sourceImage.Draw(drawRect); var modifiedImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return modifiedImage; }
public static UIImage scaledToWidth (UIImage sourceImage, nfloat width) { nfloat oldWidth = sourceImage.Size.Width; nfloat scaleFactor = width / oldWidth; nfloat newHeight = sourceImage.Size.Height * scaleFactor; nfloat newWidth = oldWidth * scaleFactor; UIGraphics.BeginImageContext(new CGSize (newWidth, newHeight)); sourceImage.Draw(new CGRect (0, 0, newWidth, newHeight)); UIImage newImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return newImage; }
public static UIImage ToRounded(UIImage source, nfloat rad, double cropWidthRatio, double cropHeightRatio, double borderSize, string borderHexColor) { double sourceWidth = source.Size.Width; double sourceHeight = source.Size.Height; double desiredWidth = sourceWidth; double desiredHeight = sourceHeight; double desiredRatio = cropWidthRatio / cropHeightRatio; double currentRatio = sourceWidth / sourceHeight; if (currentRatio > desiredRatio) desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio); else if (currentRatio < desiredRatio) desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio); float cropX = (float)((sourceWidth - desiredWidth) / 2); float cropY = (float)((sourceHeight - desiredHeight) / 2); if (rad == 0) rad = (nfloat)(Math.Min(desiredWidth, desiredHeight) / 2); else rad = (nfloat)(rad * (desiredWidth + desiredHeight) / 2 / 500); UIGraphics.BeginImageContextWithOptions(new CGSize(desiredWidth, desiredHeight), false, (nfloat)0.0); try { using (var context = UIGraphics.GetCurrentContext()) { var clippedRect = new CGRect(0d, 0d, desiredWidth, desiredHeight); context.BeginPath(); using (var path = UIBezierPath.FromRoundedRect(clippedRect, rad)) { context.AddPath(path.CGPath); context.Clip(); } var drawRect = new CGRect(-cropX, -cropY, sourceWidth, sourceHeight); source.Draw(drawRect); if (borderSize > 0d) { borderSize = (borderSize * (desiredWidth + desiredHeight) / 2d / 1000d); UIColor borderColor = UIColor.Clear; try { borderColor = UIColor.Clear.FromHexString(borderHexColor); } catch(Exception) { } var borderRect = new CGRect((0d + borderSize/2d), (0d + borderSize/2d), (desiredWidth - borderSize), (desiredHeight - borderSize)); context.BeginPath(); using (var path = UIBezierPath.FromRoundedRect(borderRect, rad)) { context.SetStrokeColor(borderColor.CGColor); context.SetLineWidth((nfloat)borderSize); context.AddPath(path.CGPath); context.StrokePath(); } } var modifiedImage = UIGraphics.GetImageFromCurrentImageContext(); return modifiedImage; } } finally { UIGraphics.EndImageContext(); } }
public static UIImage ResizeImage(UIImage theImage,nfloat width, nfloat height, bool keepRatio) { if(keepRatio) { var ratio = theImage.Size.Height / theImage.Size.Width; if(height >0) width = height * ratio; else height = width * ratio; } UIGraphics.BeginImageContext (new CGSize (width,height)); var c = UIGraphics.GetCurrentContext (); theImage.Draw (new CGRect (0, 0, width, height)); var converted = UIGraphics.GetImageFromCurrentImageContext (); UIGraphics.EndImageContext (); return converted; }
public override void ViewDidLoad () { base.ViewDidLoad (); this.flexGrid.AutoGenerateColumns = false; this.flexGrid.ColumnHeaderFont = UIFont.BoldSystemFontOfSize (this.flexGrid.Font.PointSize); this.flexGrid.IsReadOnly = true; this.flexGrid.HeadersVisibility = GridHeadersVisibility.Column; GridColumn[] columns = new[] {new GridColumn { Binding = "FirstName", Header = "First Name", Width = 1, WidthType = GridColumnWidth.Star }, new GridColumn { Binding = "LastName", Header = "Last Name", Width = 1, WidthType = GridColumnWidth.Star }, new GridColumn { Binding = "OrderTotal", Header = "Total", Width = 1, WidthType = GridColumnWidth.Star, HeaderHorizontalAlignment = UITextAlignment.Center } }; Array.ForEach (columns, (c) => this.flexGrid.Columns.Add (c)); this.flexGrid.FormatItem += (FlexGrid sender, GridPanel panel, GridCellRange range, CoreGraphics.CGContext context) => { GridColumn c = sender.Columns[range.Col]; if(c.Binding.Equals("OrderTotal") && panel.CellType == GridCellType.Cell) { XuniRadialGauge rgauge = new XuniRadialGauge() { BackgroundColor = UIColor.Clear, ShowText = ShowText.None, Thickness = 0.6, Min = 0, Max = 100, LoadAnimation = null, Value = double.Parse(panel.GetCellData(range.Row, range.Col, false).ToString())*(100.0/10000.0), ShowRanges = false }; XuniGaugeRange[] ranges = new [] { new XuniGaugeRange() { Min = 0, Max = 40, Color = new UIColor(0.133f, 0.694f, 0.298f, 1.0f) }, new XuniGaugeRange() { Min = 40, Max = 80, Color = new UIColor(1.0f, 0.502f, 0.502f, 1.0f) }, new XuniGaugeRange() { Min = 80, Max = 100, Color = new UIColor(0.0f, 0.635f, 0.91f, 1.0f) } }; rgauge.Ranges.AddRange(ranges); CoreGraphics.CGRect rect = panel.GetCellRect(range.Row, range.Col); rgauge.Frame = rect; UIImage img = new UIImage(rgauge.GetImage()); img.Draw(rect); return true; } return false; }; this.flexGrid.ItemsSource = Customer.GetCustomerList (100); }
// // Centers image, scales and removes borders // internal static UIImage PrepareForProfileView(UIImage image) { const int size = 73; if (image == null) { Console.WriteLine("throwing error"); throw new ArgumentNullException ("image"); } UIGraphics.BeginImageContext (new CGSize (73, 73)); var c = UIGraphics.GetCurrentContext (); c.AddPath (largePath); c.Clip (); // Twitter not always returns squared images, adjust for that. var cg = image.CGImage; float width = cg.Width; float height = cg.Height; if (width != height){ float x = 0, y = 0; if (width > height){ x = (width-height)/2; width = height; } else { y = (height-width)/2; height = width; } c.ScaleCTM (1, -1); using (var copy = cg.WithImageInRect (new CGRect (x, y, width, height))){ c.DrawImage (new CGRect (0, 0, size, -size), copy); } } else image.Draw (new CGRect (0, 0, size, size)); var converted = UIGraphics.GetImageFromCurrentImageContext (); UIGraphics.EndImageContext (); return converted; }
public UIImage MaxResizeImage(UIImage sourceImage, nfloat maxWidth, nfloat maxHeight) { var sourceSize = sourceImage.Size; nfloat maxResizeFactor = (nfloat)Math.Max(maxWidth / sourceSize.Width, maxHeight / sourceSize.Height); if (maxResizeFactor > 1) return sourceImage; nfloat width = maxResizeFactor * sourceSize.Width; nfloat height = maxResizeFactor * sourceSize.Height; UIGraphics.BeginImageContextWithOptions(new CGSize(width, height),false, UIScreen.MainScreen.Scale); sourceImage.Draw(new CGRect(0, 0, width, height)); var resultImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return resultImage; }
// Child proof the image by rounding the edges of the image internal static UIImage RemoveSharpEdges(UIImage image) { if (image == null) { Console.WriteLine("throwing error at remove sharp edges"); throw new ArgumentNullException ("image"); } UIGraphics.BeginImageContext (new CGSize (48, 48)); var c = UIGraphics.GetCurrentContext (); c.AddPath (smallPath); c.Clip (); image.Draw (new CGRect (0, 0, 48, 48)); var converted = UIGraphics.GetImageFromCurrentImageContext (); UIGraphics.EndImageContext (); return converted; }
/// <summary> /// Resize the image to be contained within a maximum width and height, keeping aspect ratio /// </summary> /// <param name="sourceImage"></param> /// <param name="maxWidth"></param> /// <param name="maxHeight"></param> /// <returns></returns> private UIImage ResizeImage(UIImage sourceImage, float maxWidth, float maxHeight) { var sourceSize = sourceImage.Size; maxResizeFactor = Math.Min(maxWidth / sourceSize.Width, maxHeight / sourceSize.Height); SetCropperMinSize(); if (maxResizeFactor > 1) return sourceImage; float width = (float)maxResizeFactor * (float)sourceSize.Width; float height = (float)maxResizeFactor * (float)sourceSize.Height; UIGraphics.BeginImageContext(new SizeF(width, height)); sourceImage.Draw(new RectangleF(0, 0, width, height)); var resultImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return resultImage; }
// resize the image (without trying to maintain aspect ratio) public static UIImage ResizeImage (UIImage sourceImage, float width, float height) { UIGraphics.BeginImageContext (new CGSize (width, height)); sourceImage.Draw (new CGRect (0, 0, width, height)); var resultImage = UIGraphics.GetImageFromCurrentImageContext (); UIGraphics.EndImageContext (); return resultImage; }