public static UIImage Tint(this UIImage img, UIColor tint, CGBlendMode blendMode) { UIGraphics.BeginImageContextWithOptions(img.Size, false, 0f); tint.SetFill(); var bounds = new CGRect(0, 0, img.Size.Width, img.Size.Height); UIGraphics.RectFill(bounds); img.Draw(bounds, blendMode, 1f); if (blendMode != CGBlendMode.DestinationIn) { img.Draw(bounds, CGBlendMode.DestinationIn, 1f); } var tintedImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return(tintedImage); }
/// <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); }
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); }
private void SetGradientBackground() { if (NavigationController != null) { Color startColor = Color.FromHex("#19769f"); Color endColor = Color.FromHex("#35d8a6"); var gradientLayer = new CAGradientLayer(); gradientLayer.Bounds = NavigationController.NavigationBar.Bounds; gradientLayer.Colors = new CGColor[] { startColor.ToCGColor(), endColor.ToCGColor() }; gradientLayer.StartPoint = new CGPoint(0.0, 0.5); gradientLayer.EndPoint = new CGPoint(1.0, 0.5); UIGraphics.BeginImageContext(gradientLayer.Bounds.Size); gradientLayer.RenderInContext(UIGraphics.GetCurrentContext()); UIImage image = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); NavigationController.NavigationBar.SetBackgroundImage(image, UIBarMetrics.Default); } }
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); } var width = maxResizeFactor * sourceSize.Width; var height = maxResizeFactor * sourceSize.Height; UIGraphics.BeginImageContext(new SizeF((float)width, (float)height)); sourceImage.Draw(new RectangleF(0, 0, (float)width, (float)height)); var resultImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return(resultImage); }
public byte[] ResizeImage(string filename, float width, float height) { if (File.Exists(filename)) { byte[] imageData = File.ReadAllBytes(filename); UIImage originalImage = ImageFromByteArray(imageData); var originalHeight = originalImage.Size.Height; var originalWidth = originalImage.Size.Width; nfloat newHeight = 0; nfloat newWidth = 0; if (originalHeight > originalWidth) { newHeight = height; nfloat ratio = originalHeight / height; newWidth = originalWidth / ratio; } else { newWidth = width; nfloat ratio = originalWidth / width; newHeight = originalHeight / ratio; } width = (float)newWidth; height = (float)newHeight; UIGraphics.BeginImageContext(new SizeF(width, height)); originalImage.Draw(new RectangleF(0, 0, width, height)); var resizedImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); var bytesImagen = resizedImage.AsJPEG().ToArray(); resizedImage.Dispose(); return(bytesImagen); } return(null); }
void Crop() { UIGraphics.BeginImageContextWithOptions(new CGSize(App.ScreenWidth, App.ScreenHeight), true, 1.0f); startImage.Draw(new CGRect(imageViewToMove.ImgX, imageViewToMove.ImgY + center, imageViewToMove.ImgW, height)); resultImage = UIGraphics.GetImageFromCurrentImageContext(); var inputCGImage = resultImage.CGImage; var image = inputCGImage.WithImageInRect(cropperView.CropRect); using (var croppedImage = UIImage.FromImage(image)) { //scretching the size of the cropped View if (croppedImage.Size.Height < 968) { var testh = croppedImage.Size.Height; var testw = croppedImage.Size.Width; UIGraphics.BeginImageContextWithOptions(new CGSize(968, 968), true, 1.0f); croppedImage.Draw(new CGRect(0, 0, 968, 968)); resultImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); var testh1 = resultImage.Size.Height; var testw1 = resultImage.Size.Width; using (NSData imageData = resultImage.AsPNG()) { Byte[] myByteArray = new Byte[imageData.Length]; System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, myByteArray, 0, Convert.ToInt32(imageData.Length)); new ImageDoneCropping(myByteArray); } } //EndScretching else { using (NSData imageData = croppedImage.AsPNG()) { Byte[] myByteArray = new Byte[imageData.Length]; System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, myByteArray, 0, Convert.ToInt32(imageData.Length)); new ImageDoneCropping(myByteArray); } } } }
// #if __IOS__ public static byte[] ResizeImageIOS(byte[] imageData, float width, float height) { // Load the bitmap UIImage originalImage = ImageFromByteArray(imageData); // var Hoehe = originalImage.Size.Height; var Breite = originalImage.Size.Width; // nfloat ZielHoehe = 0; nfloat ZielBreite = 0; // if (Hoehe > Breite) // Höhe (71 für Avatar) ist Master { ZielHoehe = height; nfloat teiler = Hoehe / height; ZielBreite = Breite / teiler; } else // Breite (61 for Avatar) ist Master { ZielBreite = width; nfloat teiler = Breite / width; ZielHoehe = Hoehe / teiler; } // width = (float)ZielBreite; height = (float)ZielHoehe; // UIGraphics.BeginImageContext(new SizeF(width, height)); originalImage.Draw(new RectangleF(0, 0, width, height)); var resizedImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); // var bytesImagen = resizedImage.AsJPEG().ToArray(); resizedImage.Dispose(); return(bytesImagen); }
public UIImage GetPDFImageForPage() { CGPDFPage pdfPg = _pdf.GetPage(PageNumber); nfloat scale; PDFpageRect = pdfPg.GetBoxRect(CGPDFBox.Media); if (PDFpageRect.Height > PDFpageRect.Width) { scale = (this.View.Frame.Width - 80.0f) / PDFpageRect.Width; } else { scale = this.View.Frame.Height / PDFpageRect.Height; } PDFpageRect.Size = new CGSize(PDFpageRect.Width * scale, PDFpageRect.Height * scale); UIGraphics.BeginImageContext(PDFpageRect.Size); CGContext context = UIGraphics.GetCurrentContext(); context.SetFillColor((nfloat)1.0, (nfloat)1.0, (nfloat)1.0, (nfloat)1.0); context.FillRect(PDFpageRect); context.SaveState(); context.TranslateCTM(0, PDFpageRect.Size.Height); context.ScaleCTM(1, -1); context.ConcatCTM(CGAffineTransform.MakeScale(scale, scale)); context.DrawPDFPage(pdfPg); context.RestoreState(); UIImage thm = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return(thm); }
public byte[] ResizeImage(byte[] imageData, float width, float height) { UIImage originalImage = ImageFromByteArray(imageData); var originalImageHeight = originalImage.Size.Height; var originalImageWidth = originalImage.Size.Width; nfloat newHeight = 0; nfloat newWidth = 0; if (originalImageHeight > originalImageWidth) { newHeight = height; nfloat scale = originalImageHeight / height; newWidth = originalImageWidth / scale; } else { newWidth = width; nfloat scale = originalImageWidth / width; newHeight = originalImageHeight / scale; } width = (float)newWidth; height = (float)newHeight; UIGraphics.BeginImageContext(new SizeF(width, height)); originalImage.Draw(new RectangleF(0, 0, width, height)); var resizedImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); var bytesImagen = resizedImage.AsJPEG().ToArray(); resizedImage.Dispose(); return(bytesImagen); }
public static UIImage ImageFromFont(string text, UIColor iconColor, CGSize iconSize, string fontName = "ionicons") { UIGraphics.BeginImageContextWithOptions(iconSize, false, 0); var textRect = new CGRect(CGPoint.Empty, iconSize); var path = UIBezierPath.FromRect(textRect); UIColor.Clear.SetFill(); path.Fill(); var font = UIFont.FromName(fontName, iconSize.Width); using (var label = new UILabel() { Text = text, Font = font }) { GetFontSize(label, iconSize, 500, 5); font = label.Font; } iconColor.SetFill(); using (var nativeString = new NSString(text)) { nativeString.DrawString(textRect, new UIStringAttributes { Font = font, ForegroundColor = iconColor, BackgroundColor = UIColor.Clear, ParagraphStyle = new NSMutableParagraphStyle { Alignment = UITextAlignment.Center } }); } var image = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return(image); }
/// <summary> /// Gets the scaled image while maintaining correct aspect ration and not blurring. /// /// See iOS code version at https://gist.github.com/tomasbasham/10533743 /// </summary> /// <returns>The scaled image.</returns> /// <param name="originalImage">Original image.</param> UIImage GetScaledImage(UIImage originalImage) { CGSize newSize = new CGSize(originalImage.Size.Width / SCALING_FACTOR, originalImage.Size.Height / SCALING_FACTOR); CGRect scaledImageRect = CGRect.Empty; double aspectWidth = newSize.Width / originalImage.Size.Width; double aspectHeight = newSize.Height / originalImage.Size.Height; double aspectRatio = Math.Min(aspectWidth, aspectHeight); scaledImageRect.Size = new CGSize(originalImage.Size.Width * aspectRatio, originalImage.Size.Height * aspectRatio); scaledImageRect.X = (newSize.Width - scaledImageRect.Size.Width) / 2.0f; scaledImageRect.Y = (newSize.Height - scaledImageRect.Size.Height) / 2.0f; UIGraphics.BeginImageContextWithOptions(newSize, false, 0); originalImage.Draw(scaledImageRect); UIImage scaledImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return(scaledImage); }
public override UIImage GenerateBackgroundImage() { UIImage resultImage; var center = new CGPoint(BackBounds.GetMidX(), BackBounds.GetMidY()); UIGraphics.BeginImageContextWithOptions(BackBounds.Size, false, UIScreen.MainScreen.Scale); using (var context = UIGraphics.GetCurrentContext()) using (var borderBagelPath = BezierPathGenerator.Bagel(center, startRadius - borderPadding, endRadius + borderPadding, 0f, FullCircleAngle)) { context.SaveState(); context.SetFillColor(BackCircleBackgroundColor); borderBagelPath.Fill(); context.RestoreState(); resultImage = UIGraphics.GetImageFromCurrentImageContext(); } return(resultImage); }
private byte[] Resize(UIImage sourceImage, float percent, float quality) { UIImage resultImage; var sourceSize = sourceImage.Size; var width = percent * sourceSize.Width; var height = percent * sourceSize.Height; UIGraphics.BeginImageContext(new CGSize(width, height)); sourceImage.Draw(new CGRect(0, 0, width, height)); resultImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); using (var imageData = resultImage.AsJPEG(quality)) { var res = new byte[imageData.Length]; Marshal.Copy(imageData.Bytes, res, 0, (int)imageData.Length); return(res); } }
public byte[] Capture(View formsView) { if (formsView.Bounds.Width <= 0) { return(new byte[0]); } var nativeView = ConvertFormsToNative(formsView, formsView.Bounds); UIGraphics.BeginImageContext(nativeView.Frame.Size); nativeView.DrawViewHierarchy(nativeView.Frame, true); var image = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); using (var imageData = image.AsPNG()) { var bytes = new byte[imageData.Length]; System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, bytes, 0, Convert.ToInt32(imageData.Length)); return(bytes); } }
private void SetupIcon() { if (_withIcon) { using (var image = UIImage.FromFile(_materialButton.Image.File)) { UIGraphics.BeginImageContextWithOptions(new CGSize(18, 18), false, 0f); image.Draw(new CGRect(0, 0, 18, 18)); using (var newImage = UIGraphics.GetImageFromCurrentImageContext()) { UIGraphics.EndImageContext(); this.Control.SetImage(newImage, UIControlState.Normal); this.Control.SetImage(newImage, UIControlState.Disabled); this.Control.TitleEdgeInsets = new UIEdgeInsets(0f, 0f, 0f, 0f); this.Control.ImageEdgeInsets = new UIEdgeInsets(0f, -6f, 0f, 0f); this.Control.TintColor = _materialButton.TextColor.ToUIColor(); } } } }
void DrawLine(CGPoint pt1, CGPoint pt2, UIColor color) { UIGraphics.BeginImageContext(imgDraw.Frame.Size); using (var g = UIGraphics.GetCurrentContext()) { imgDraw.Layer.RenderInContext(g); using (var path = new CGPath()) { path.AddLines(new CGPoint [] { pt1, pt2 }); g.SetLineWidth(3); color.SetStroke(); g.AddPath(path); g.DrawPath(CGPathDrawingMode.Stroke); } imgDraw.Image?.Dispose(); imgDraw.Image = UIGraphics.GetImageFromCurrentImageContext(); } UIGraphics.EndImageContext(); }
public UIImage GetImageFromView() { RectangleF rect; rect = (RectangleF)Frame; UIGraphics.BeginImageContext(rect.Size); CGContext context = UIGraphics.GetCurrentContext(); if (_image != null) { context.DrawImage(Frame, _image.CGImage); } this.Layer.RenderInContext(context); UIImage image = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return(image); }
void DrawLine(CGPoint fromPoint, CGPoint toPoint) { UIGraphics.BeginImageContextWithOptions(ImgSample.Frame.Size, false, 0); var context = UIGraphics.GetCurrentContext(); context.MoveTo(fromPoint.X, fromPoint.Y); context.AddLineToPoint(toPoint.X, toPoint.Y); context.SetLineCap(CGLineCap.Round); context.SetBlendMode(CGBlendMode.Normal); context.SetLineWidth(10); context.SetStrokeColor(UIColor.Red.CGColor); ImgSample.Image.Draw(new CGRect(0, 0, ImgSample.Frame.Size.Width, ImgSample.Frame.Size.Height)); context.StrokePath(); var newImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); ImgSample.Image = newImage; }
//Crops an image to even width and height private static UIImage CenterCrop(UIImage originalImage) { // Use smallest side length as crop square length double squareLength = Math.Min(originalImage.Size.Width, originalImage.Size.Height); nfloat x, y; x = ( nfloat )((originalImage.Size.Width - squareLength) / 2.0); y = ( nfloat )((originalImage.Size.Height - squareLength) / 2.0); //This Rect defines the coordinates to be used for the crop CGRect croppedRect = CGRect.FromLTRB(x, y, x + ( nfloat )squareLength, y + ( nfloat )squareLength); // Center-Crop the image UIGraphics.BeginImageContextWithOptions(croppedRect.Size, false, originalImage.CurrentScale); originalImage.Draw(new CGPoint(-croppedRect.X, -croppedRect.Y)); UIImage croppedImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return(croppedImage); }
protected override void OnElementChanged(VisualElementChangedEventArgs e) { base.OnElementChanged(e); if (e.OldElement != null || Element == null) { return; } ContentPage page = (ContentPage)Element; // define SafeArea page.On <Xamarin.Forms.PlatformConfiguration.iOS>().SetUseSafeArea(true); if (!string.IsNullOrEmpty(page.BackgroundImage)) { UIImage ImageBackground = UIImage.FromFile(page.BackgroundImage); page.BackgroundImage = null; CGRect scaledImageRect = CGRect.Empty; double aspectWidth = this.View.Frame.Size.Width / ImageBackground.Size.Width; double aspectHeight = this.View.Frame.Size.Height / ImageBackground.Size.Height; double aspectRatio = Math.Max(aspectWidth, aspectHeight); scaledImageRect.Size = new CGSize(ImageBackground.Size.Width * aspectRatio, ImageBackground.Size.Height * aspectRatio); scaledImageRect.X = (this.View.Frame.Size.Width - scaledImageRect.Size.Width) / 2.0f; scaledImageRect.Y = (this.View.Frame.Size.Height - scaledImageRect.Size.Height) / 2.0f; UIGraphics.BeginImageContextWithOptions(this.View.Frame.Size, false, 0); ImageBackground.Draw(scaledImageRect); UIImage scaledImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); this.View.BackgroundColor = UIColor.FromPatternImage(scaledImage); } }
public static UIImage RemoveSharpEdges(this UIImage image, int radius) { var width = image.Size.Width; UIGraphics.BeginImageContextWithOptions(new SizeF(width, width), false, 0f); var context = UIGraphics.GetCurrentContext(); context.BeginPath(); context.MoveTo(width, width / 2); context.AddArcToPoint(width, width, width / 2, width, radius); context.AddArcToPoint(0, width, 0, width / 2, radius); context.AddArcToPoint(0, 0, width / 2, 0, radius); context.AddArcToPoint(width, 0, width, width / 2, radius); context.ClosePath(); context.Clip(); image.Draw(new PointF(0, 0)); var converted = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return(converted); }
// Mashes up the images. private UIImage ImageFromImages(List <UIImage> images, CGSize size) { if (images.Count <= 1) { return(images.FirstOrDefault()); } UIGraphics.BeginImageContextWithOptions(size, true, 0); if (images.Count == 2 || images.Count == 3) { // Draw left half. images[0].Draw(new CGRect(-size.Width / 4, 0, size.Width, size.Height)); } if (images.Count == 2) { // Draw right half. UIImage halfOfImage = HalfOfImage(images[1]); halfOfImage.Draw(new CGRect(size.Width / 2, 0, size.Width, size.Height)); } else { // Draw top right quadrant. images[1].Draw(new CGRect(size.Width / 2, 0, size.Width, size.Height)); // Draw bottom right quadrant. images[2].Draw(new CGRect(size.Width / 2, size.Height / 2, size.Width / 2, size.Height / 2)); } if (images.Count >= 4) { // Draw top left quadrant. images[0].Draw(new CGRect(0, 0, size.Width / 2, size.Height / 2)); // Draw bottom left quadrant. images[3].Draw(new CGRect(0, size.Height / 2, size.Width / 2, size.Height / 2)); } UIImage newImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return(newImage); }
UIImage CreatePreviewTabBackgroundImage() { // The preview tab should be flat on the bottom, and have rounded corners on top. var size = new SizeF(PreviewTabMinimumWidth, PreviewTabHeight); UIGraphics.BeginImageContextWithOptions(size, false, UIScreen.MainScreen.Scale); var rect = new RectangleF(0f, 0f, PreviewTabMinimumWidth, PreviewTabHeight); var radii = new SizeF(PreviewTabCornerRadius, PreviewTabCornerRadius); var roundedTopCornersRect = UIBezierPath.FromRoundedRect(rect, UIRectCorner.TopLeft | UIRectCorner.TopRight, radii); StyleUtilities.ForegroundColor.SetColor(); roundedTopCornersRect.Fill(); UIImage previewTabBackgroundImage = UIGraphics.GetImageFromCurrentImageContext(); var caps = new UIEdgeInsets(0f, PreviewTabCornerRadius, 0f, PreviewTabCornerRadius); previewTabBackgroundImage = previewTabBackgroundImage.CreateResizableImage(caps); UIGraphics.EndImageContext(); return(previewTabBackgroundImage); }
public UIImage MaxResizeImage(UIImage sourceImage, float maxWidth, float maxHeight) { Console.WriteLine("Re Size original image"); var sourceSize = sourceImage.Size; var maxResizeFactor = Math.Min(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); }
protected virtual UIImage GenerateFullProgressImage() { UIImage resultImage; UIGraphics.BeginImageContextWithOptions(Bounds.Size, false, UIScreen.MainScreen.Scale); using (var context = UIGraphics.GetCurrentContext()) using (var path = BezierPathGenerator.Bagel(CenterPoint, startRadius, endRadius, 0f, FullCircleAngle)) { context.SaveState(); context.SetFillColor(Color.CGColor); context.AddPath(path.CGPath); context.FillPath(); context.RestoreState(); resultImage = UIGraphics.GetImageFromCurrentImageContext(); } return(resultImage); }
private void DrawLine(CGPoint point1, CGPoint point2) { var size = this.Frame.Size; UIGraphics.BeginImageContext(size); this.Control.Image.Draw(new CGRect(0, 0, size.Width, size.Height)); var context = UIGraphics.GetCurrentContext(); context.SetLineCap(CGLineCap.Round); context.SetLineWidth((nfloat)_formsView.LineWidth); context.SetStrokeColor(_formsView.PaintColor.ToCGColor()); context.BeginPath(); context.MoveTo(point1.X - 0.5f, point1.Y - 0.5f); context.AddLineToPoint(point2.X - 0.5f, point2.Y - 0.5f); context.StrokePath(); this.Control.Image = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); }
UIImage CreateGradientBackground(Color startColor, Color endColor, CustomNavigationPage.GradientDirection direction) { var gradientLayer = new CAGradientLayer(); gradientLayer.Bounds = NavigationController.NavigationBar.Bounds; gradientLayer.Colors = new CGColor[] { startColor.ToCGColor(), endColor.ToCGColor() }; switch (direction) { case CustomNavigationPage.GradientDirection.LeftToRight: gradientLayer.StartPoint = new CGPoint(0.0, 0.5); gradientLayer.EndPoint = new CGPoint(1.0, 0.5); break; case CustomNavigationPage.GradientDirection.RightToLeft: gradientLayer.StartPoint = new CGPoint(1.0, 0.5); gradientLayer.EndPoint = new CGPoint(0.0, 0.5); break; case CustomNavigationPage.GradientDirection.BottomToTop: gradientLayer.StartPoint = new CGPoint(1.0, 1.0); gradientLayer.EndPoint = new CGPoint(0.0, 0.0); break; default: gradientLayer.StartPoint = new CGPoint(1.0, 0.0); gradientLayer.EndPoint = new CGPoint(0.0, 1.0); break; } UIGraphics.BeginImageContext(gradientLayer.Bounds.Size); gradientLayer.RenderInContext(UIGraphics.GetCurrentContext()); UIImage image = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return(image); }
public static UIImage ScaleImage(this UIImage owner, CGSize size, float screenScale = 0f) { if (null == owner) { throw new NullReferenceException("Image object is null!"); } //end if UIImage toReturn = null; UIGraphics.BeginImageContextWithOptions(size, false, screenScale); using (var context = UIGraphics.GetCurrentContext()) { context.TranslateCTM(0f, size.Height); context.ScaleCTM(1f, -1f); context.DrawImage(new CGRect(0f, 0f, size.Width, size.Height), owner.CGImage); toReturn = UIGraphics.GetImageFromCurrentImageContext(); } //end using context UIGraphics.EndImageContext(); return(toReturn); } //end static UIImage ScaleImage
public static UIImage OverlayWithColor(this UIImage image, UIColor color, float alpha = 1) { var size = image.Size; UIGraphics.BeginImageContextWithOptions(size, false, 2); try { using (var context = UIGraphics.GetCurrentContext()) { image.Draw(CGPoint.Empty, CGBlendMode.Normal, 1); context.SetFillColor(color.CGColor); context.SetBlendMode(CGBlendMode.Overlay); context.SetAlpha(alpha); context.FillRect(new CGRect(0, 0, size.Width, size.Height)); return(UIGraphics.GetImageFromCurrentImageContext()); } } finally { UIGraphics.EndImageContext(); } }