/// <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 Stream GetRoundedFromSquareImage(Stream stream) { Stream result = null; using (UIImage image = UIImage.LoadFromData(NSData.FromStream(stream))) { UIGraphics.BeginImageContext(new SizeF(image.CGImage.Width, image.CGImage.Height)); CGRect rect = new CGRect(0, 0, image.CGImage.Width, image.CGImage.Height); UIBezierPath.FromRoundedRect(rect, Math.Max(image.CGImage.Width, image.CGImage.Height) / 2).AddClip(); image.Draw(new Rectangle(0, 0, (int)image.CGImage.Width, (int)image.CGImage.Height)); var resultImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); NSData data = resultImage.AsPNG(); result = data.AsStream(); } 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 UIImage GetDrawingImage() { UIImage returnImg = null; UIGraphics.BeginImageContext(this.Bounds.Size); using (CGContext context = UIGraphics.GetCurrentContext()) { context.SetStrokeColor(UIColor.Black.CGColor); context.SetLineWidth(5f); context.SetLineJoin(CGLineJoin.Round); context.SetLineCap(CGLineCap.Round); context.AddPath(this.drawPath); context.DrawPath(CGPathDrawingMode.Stroke); returnImg = UIGraphics.GetImageFromCurrentImageContext(); } UIGraphics.EndImageContext(); return(returnImg); }
// resize the image to be contained within a maximum width and height, keeping aspect ratio private 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 = (float)(maxResizeFactor * sourceSize.Width); var height = (float)(maxResizeFactor * sourceSize.Height); UIGraphics.BeginImageContextWithOptions(new SizeF(width, height), false, 2.0f); sourceImage.Draw(new RectangleF(0, 0, width, height)); var resultImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return(resultImage); }
private void DrawLineFrom(CGPoint fromPoint, CGPoint toPoint) { UIGraphics.BeginImageContext(Frame.Size); var context = UIGraphics.GetCurrentContext(); canvasTempImg.Image?.DrawAsPatternInRect(new CGRect(0, 0, Frame.Size.Width, Frame.Size.Height)); context.MoveTo(fromPoint.X, fromPoint.Y); context.AddLineToPoint(toPoint.X, toPoint.Y); context.SetLineCap(CGLineCap.Round); context.SetLineWidth(brushWidth); context.SetStrokeColor(colour); context.SetBlendMode(CGBlendMode.Normal); context.StrokePath(); canvasTempImg.Image = UIGraphics.GetImageFromCurrentImageContext(); canvasTempImg.Alpha = opacity; UIGraphics.EndImageContext(); }
public static UIImage GetImageWithOverlayColor(this UIImage self, UIColor color) { RectangleF rect = new RectangleF(0.0f, 0.0f, self.Size.Width, self.Size.Height); UIGraphics.BeginImageContextWithOptions(self.Size, false, self.CurrentScale); self.DrawAsPatternInRect(rect); CGContext context = UIGraphics.GetCurrentContext(); context.SetBlendMode(CGBlendMode.SourceIn); context.SetFillColor(color.CGColor); context.FillRect(rect); UIImage image = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return(image); }
/// <summary> /// Gets the image from view. /// </summary> /// <returns>UIImage.</returns> public UIImage GetImageFromView() { var rect = (RectangleF)Frame; UIGraphics.BeginImageContextWithOptions(rect.Size, false, 2.0f); CGContext context = UIGraphics.GetCurrentContext(); if (_image != null) { context.DrawImage(Frame, _image.CGImage); } Layer.RenderInContext(context); UIImage image = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return(image); }
public UIImage ImageWithColor(CGSize size) { CGRect rect = new CGRect(0, 0, size.Width, size.Height); UIGraphics.BeginImageContext(size); using (CGContext context = UIGraphics.GetCurrentContext()) { var baseTabbedPage = (BaseTabbedPage)Element; var selectedTabFillColor = baseTabbedPage.SelectedTabFillColor.ToCGColor(); context.SetFillColor(selectedTabFillColor); context.FillRect(rect); } UIImage image = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return(image); }
public static UIImage Tint(this UIImage img, UIColor tint, CGBlendMode blendMode) { UIGraphics.BeginImageContextWithOptions(img.Size, false, 0f); tint.SetFill(); var bounds = new RectangleF(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); }
public byte[] ScaleImage(byte[] arr) { float height = 1024; float width = 2048; try { NSData data = NSData.FromArray(arr); UIImage image = UIImage.LoadFromData(data); CGSize scaleSize = new CGSize(width, height); UIGraphics.BeginImageContextWithOptions(scaleSize, false, 0); image.Draw(new CGRect(0, 0, scaleSize.Width, scaleSize.Height)); UIImage resizedImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return(resizedImage.AsJPEG().ToArray()); } catch (Exception) { return(null); } }
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 MaxResizeImage(this 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); }
// resize the image to be contained within a maximum width and height, keeping aspect ratio static UIImage MaxResize(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 result = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return(result); }
public static UIImage ResizeImage(UIImage sourceImage, float maxWidth = 1080, float maxHeight = 1080) { var sourceSize = sourceImage.Size; var maxResizeFactor = Math.Min(maxWidth / (sourceSize.Width == 0 ? maxWidth : sourceSize.Width), maxHeight / (sourceSize.Height == 0 ? maxHeight : sourceSize.Height)); if (maxResizeFactor >= 1) { return(sourceImage); } var width = maxResizeFactor * sourceSize.Width; var height = maxResizeFactor * sourceSize.Height; UIGraphics.BeginImageContext(new CGSize((nfloat)width, (nfloat)height)); sourceImage.Draw(new CGRect(0, 0, (nfloat)width, (nfloat)height)); var resultImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return(resultImage); }
/// <summary> /// Scales the image to th specified width, preserving aspect ratio. /// </summary> /// <returns>A new image at the requested width.</returns> /// <param name="image">The image to resize.</param> /// <param name="width">The target width.</param> public static UIImage ScaleImageToWidth(UIImage image, int width) { UIImage scaledImage = null; image.InvokeOnMainThread(() => { var size = new CGSize(width, width / image.Size.Width * image.Size.Height); UIGraphics.BeginImageContextWithOptions(size, false, UIScreen.MainScreen.Scale); var context = UIGraphics.GetCurrentContext(); context.TranslateCTM(0, size.Height); context.ScaleCTM(1.0f, -1.0f); context.DrawImage(new CGRect(CGPoint.Empty, size), image.CGImage); scaledImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); }); return(scaledImage); }
public static UIImage Resize(this UIImage image, CGSize newSize) { UIGraphics.BeginImageContextWithOptions(newSize, false, 0); UIImage result = null; try { image.Draw(new CGRect(0, 0, newSize.Width, newSize.Height)); result = UIGraphics.GetImageFromCurrentImageContext(); } catch (Exception ex) { throw new Exception("UIImageColors.ResizeForUIImageColors failed: UIGraphics.GetImageFromCurrentImageContext returned null"); } finally { UIGraphics.EndImageContext(); } return(result); }
UIImage MaxResizeSwipeItemIconImage(UIImage sourceImage, nfloat maxWidth, nfloat maxHeight) { 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.BeginImageContextWithOptions(new CGSize((nfloat)width, (nfloat)height), false, 0); sourceImage.Draw(new CGRect(0, 0, (nfloat)width, (nfloat)height)); var resultImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return(resultImage); }
// crop the image, without resizing public static UIImage CropSquareImage(UIImage sourceImage, float dimension) { var resizedImage = MaxResizeImage(sourceImage, dimension, dimension); var imgSize = resizedImage.Size; var largerDimension = imgSize.Width > imgSize.Height ? imgSize.Width : imgSize.Height; var crop_y = largerDimension == imgSize.Width ? 0 : (largerDimension - imgSize.Width) / 2; var crop_x = largerDimension == imgSize.Height ? 0 : (largerDimension - imgSize.Height) / 2; UIGraphics.BeginImageContext(new CGSize(dimension, dimension)); var context = UIGraphics.GetCurrentContext(); var clippedRect = new CGRect(0, 0, dimension, dimension); context.ClipToRect(clippedRect); var drawRect = new CGRect(-crop_x, -crop_y, (float)imgSize.Width, (float)imgSize.Height); resizedImage.Draw(drawRect); var modifiedImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return(modifiedImage); }
public static UIImage ChangeColor(this UIImage img, UIColor color) { if (color == null) { throw new ArgumentNullException(nameof(color)); } UIGraphics.BeginImageContextWithOptions(img.Size, false, img.CurrentScale); var ctx = UIGraphics.GetCurrentContext(); color.SetFill(); ctx.TranslateCTM(0, img.Size.Height); ctx.ScaleCTM(1f, -1f); var rect = new CGRect(CGPoint.Empty, img.Size); ctx.ClipToMask(rect, img.CGImage); ctx.FillRect(rect); var coloredImg = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return(coloredImg); }
public override void ViewWillAppear(bool animated) { base.ViewWillAppear(animated); var control = (NavigationPageGradientHeader)this.Element; var gradientLayer = new CAGradientLayer(); gradientLayer.Bounds = NavigationBar.Bounds; gradientLayer.Colors = new CGColor[] { control.RightColor.ToCGColor(), control.LeftColor.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(); NavigationBar.SetBackgroundImage(image, UIBarMetrics.Default); }
protected void SaveAsImage(object sender, System.EventArgs e) { NSError err; UIGraphics.BeginImageContext(this.View.Frame.Size); using (var context = UIGraphics.GetCurrentContext()) { if (context != null) { //TODO: Save just the background and the user's drawing this.View.Layer.RenderInContext(context); UIImage fuckingImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); string location = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); fuckingImage.AsPNG().Save(Path.Combine(location, Title + ".png"), true, out err); Console.WriteLine("saved image to {0}", location); } } }
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(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 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); }
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(); } } } }
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); }
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(); }