public async Task <byte[]> ResizeImageAsync(byte[] imageData, float width, float height) { await Task.Run(() => { UIImage originalImage = ImageFromByteArray(imageData); UIImageOrientation orientation = originalImage.Orientation; //create a 24bit RGB image using (CGBitmapContext context = new CGBitmapContext(IntPtr.Zero, (int)width, (int)height, 8, 4 * (int)width, CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedFirst)) { RectangleF imageRect = new RectangleF(0, 0, width, height); // draw the image context.DrawImage(imageRect, originalImage.CGImage); resizedImage = UIKit.UIImage.FromImage(context.ToImage(), 0, orientation); } }); // save the image as a jpeg return(resizedImage.AsPNG().ToArray()); }
public byte[] ResizeImage(byte[] imageData, float widthScale, float heightScale) { UIImage originalImage = ImageFromByteArray(imageData); UIImageOrientation orientation = originalImage.Orientation; var width = (int)(originalImage.Size.Width * widthScale); var height = (int)(originalImage.Size.Height * heightScale); //create a 24bit RGB image using (CGBitmapContext context = new CGBitmapContext(IntPtr.Zero, (int)width, (int)height, 8, 4 * (int)width, CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedFirst)) { RectangleF imageRect = new RectangleF(0, 0, width, height); // draw the image context.DrawImage(imageRect, originalImage.CGImage); UIKit.UIImage resizedImage = UIKit.UIImage.FromImage(context.ToImage(), 0, orientation); // pngとしたときのバイト配列を返す return(resizedImage.AsPNG().ToArray()); } }
public System.IO.Stream RenderToStream (byte[] documentData, int pageIndex) { using (MemoryStream ms = new MemoryStream (documentData)) { // open document using (Document doc = new Document (ms)) { // prepare for rendering int width = (int)doc.Pages [pageIndex].Width; int height = (int)doc.Pages [pageIndex].Height; // render the page to a raw bitmap data represented by byte array byte[] imageData = ConvertBGRAtoRGBA(doc.Pages [pageIndex].RenderAsBytes (width,height, new RenderingSettings (), null)); // create CGDataProvider which will serve CGImage creation CGDataProvider dataProvider = new CGDataProvider (imageData, 0, imageData.Length); // create core graphics image using data provider created above, note that // we use CGImageAlphaInfo.Last(ARGB) pixel format CGImage cgImage = new CGImage(width,height,8,32,width*4,CGColorSpace.CreateDeviceRGB(),CGImageAlphaInfo.Last,dataProvider,null,false, CGColorRenderingIntent.Default); // create UIImage and save it to gallery UIImage finalImage = new UIImage (cgImage); return finalImage.AsPNG ().AsStream(); } } }
public async Task <bool> SaveImage(string directory, string filename, ImageSource img) { bool success = false; NSData imgData = null; var renderer = new Xamarin.Forms.Platform.iOS.StreamImagesourceHandler(); UIKit.UIImage photo = await renderer.LoadImageAsync(img); var savedImageFilename = System.IO.Path.Combine(directory, filename); NSFileManager.DefaultManager.CreateDirectory(directory, true, null); if (System.IO.Path.GetExtension(filename).ToLower() == ".png") { imgData = photo.AsPNG(); } else { imgData = photo.AsJPEG(100); } NSError err = null; success = imgData.Save(savedImageFilename, NSDataWritingOptions.Atomic, out err); return(success); }
public override void DidCropToImage(TOCropViewController cropViewController, UIImage image, CoreGraphics.CGRect cropRect, nint angle) { var imageBytes = image.AsPNG().ToArray(); _taskCompletionSource.SetResult(imageBytes); cropViewController.DismissViewController(true, null); }
public void FinishedPickingImage(UIKit.UIImagePickerController picker, UIKit.UIImage image, Foundation.NSDictionary editingInfo) { this.ImagenEmpresa.Image = image; NSData imageData = image.AsPNG(); InfoPerifl.Empresa_Actual.Empresa_Logotipo_Perfil = new Byte[imageData.Length]; EventosVistaTrabajoDelegate.InfoEmpresa(InfoPerifl); picker.DismissViewController(true, null); }
public void FinishedPickingImage(UIKit.UIImagePickerController picker, UIKit.UIImage image, Foundation.NSDictionary editingInfo) { if (this.TouchedBack) { image = ImageHelper.ReescalProfileBackImage(image); this.btnFondoImagen.SetBackgroundImage(image, UIControlState.Normal); NewInfoPerfil.Usuario_Fotografia_FondoPerfil = image?.AsPNG().ToArray(); } else if (this.TouchedProfile) { image = ImageHelper.ReescalProfileImage(image); this.btnImagen.SetBackgroundImage(image, UIControlState.Normal); NewInfoPerfil.Usuario_Fotografia_Perfil = image?.AsPNG().ToArray(); } this.TouchedBack = false; this.TouchedProfile = false; picker.DismissViewController(true, null); }
public void SaveTrasnformedImage(UIImage source) { byte[] myByteArray; using (var imageData = source.AsPNG()) { myByteArray = new byte[imageData.Length]; System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, myByteArray, 0, Convert.ToInt32(imageData.Length)); } SaveImageLocally(myByteArray, 101); }
public async void FinishedPickingImage(UIKit.UIImagePickerController picker, UIKit.UIImage image, Foundation.NSDictionary editingInfo) { if (this.TouchedBack) { BTProgressHUD.Show("Guardando Imagen"); await Task.Run(() => { image = ImageHelper.ReescalProfileBackImage(image); Miembro.Usuario_Fotografia_FondoPerfil = image?.AsPNG().ToArray(); if (this.GuardarInfo()) { BTProgressHUD.Dismiss(); BTProgressHUD.Show("Información guardada con éxito"); //this.PublicarPost(Miembro.Usuario_Nombre + " " + Miembro.Usuario_Apellidos + " actualizó su foto de fondo", Miembro.Usuario_Fotografia_FondoPerfil, 2); } }); BTProgressHUD.Dismiss(); this.btnImageBackGround.SetBackgroundImage(image, UIControlState.Normal); } else if (this.TouchedProfile) { BTProgressHUD.Show("Guardando Imagen"); await Task.Run(() => { image = ImageHelper.ReescalProfileImage(image); Miembro.Usuario_Fotografia_Perfil = image?.AsPNG().ToArray(); if (this.GuardarInfo()) { //this.PublicarPost(Miembro.Usuario_Nombre + " " + Miembro.Usuario_Apellidos + " actualizó su foto de perfil", Miembro.Usuario_Fotografia_Perfil, 1); } }); BTProgressHUD.Dismiss(); this.btnProfileImage.SetBackgroundImage(image, UIControlState.Normal); } this.TouchedBack = false; this.TouchedProfile = false; picker.DismissViewController(true, null); }
private static NSData GetData(UIImage image, ImageType type, int quality) { var floatQuality = quality/100f; switch(type) { case ImageType.Jpg: return image.AsJPEG(floatQuality); case ImageType.Png: return image.AsPNG(); default: return image.AsJPEG(floatQuality); } }
async Task <byte[]> GetImageAsByteAsync(bool usePNG, int quality, int desiredWidth, int desiredHeight) { PImage image = null; await ImageService.Instance.Config.MainThreadDispatcher.PostAsync(() => { if (Control != null) { image = Control.Image; } }).ConfigureAwait(false); if (image == null) { return(null); } if (desiredWidth != 0 || desiredHeight != 0) { image = image.ResizeUIImage((double)desiredWidth, (double)desiredHeight, InterpolationMode.Default); } #if __IOS__ NSData imageData = usePNG ? image.AsPNG() : image.AsJPEG((nfloat)quality / 100f); if (imageData == null || imageData.Length == 0) { return(null); } var encoded = imageData.ToArray(); imageData.TryDispose(); return(encoded); #elif __MACOS__ byte[] encoded; using (MemoryStream ms = new MemoryStream()) using (var stream = usePNG ? image.AsPngStream() : image.AsJpegStream(quality)) { stream.CopyTo(ms); encoded = ms.ToArray(); } if (desiredWidth != 0 || desiredHeight != 0) { image.TryDispose(); } return(encoded); #endif }
public async Task <bool> SaveHandheldImageAsync(string filename, ImageSource imageSource) { NSData imgData = null; var renderer = new Xamarin.Forms.Platform.iOS.StreamImagesourceHandler(); UIKit.UIImage photo = await renderer.LoadImageAsync(imageSource); var savedImageFilename = GetHandheldImage(filename); var directory = Path.GetDirectoryName(savedImageFilename); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); NSFileManager.SetSkipBackupAttribute(directory, true); } //var isDirectory = true; //if(!NSFileManager.DefaultManager.FileExists(directory, ref isDirectory)) //{ // if (!isDirectory) // { // NSFileManager.DefaultManager.CreateDirectory(directory, true, null); // NSFileManager.SetSkipBackupAttribute(directory, true); // } //} if (Path.GetExtension(filename).ToLower() == ".png") { imgData = photo.AsPNG(); } else { imgData = photo.AsJPEG(100); } bool success = false; NSError err = null; success = await Task.Run(() => imgData.Save(savedImageFilename, NSDataWritingOptions.Atomic, out err)); return(success); }
//縮圖方法 回傳byte[]Array public static byte[] ResizeImageIOS(UIImage originalImage, float width, float height) { UIImageOrientation orientation = originalImage.Orientation; //create a 24bit RGB image using (CGBitmapContext context = new CGBitmapContext(IntPtr.Zero, (int)width, (int)height, 8, (int)(4 * width), CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedFirst)) { RectangleF imageRect = new RectangleF(0, 0, width, height); // draw the image context.DrawImage(imageRect, originalImage.CGImage); UIKit.UIImage resizedImage = UIKit.UIImage.FromImage(context.ToImage(), 0, orientation); // save the image as a jpeg return(resizedImage.AsPNG().ToArray()); } }
public ImageData (UIImage image, string filename) { if (image == null) { throw new ArgumentNullException ("image"); } if (string.IsNullOrEmpty (filename)) { throw new ArgumentException ("filename"); } Image = image; Filename = filename; MimeType = (filename.ToLowerInvariant ().EndsWith (".png")) ? "image/png" : "image/jpeg"; if (MimeType == "image/png") { Data = new NSDataStream (image.AsPNG ()); } else { Data = new NSDataStream (image.AsJPEG ()); } }
public byte[] ResizeImage(byte[] imageData, ImageSize imageSize) { UIImage originalImage = ImageFromByteArray(imageData); UIImageOrientation orientation = originalImage.Orientation; //create a 24bit RGB image using (CGBitmapContext context = new CGBitmapContext(IntPtr.Zero, (int)imageSize.Width, (int)imageSize.Height, 8, 4 * (int)imageSize.Width, CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedFirst)) { RectangleF imageRect = new RectangleF(0, 0, imageSize.Width, imageSize.Height); // draw the image context.DrawImage(imageRect, originalImage.CGImage); UIKit.UIImage resizedImage = UIKit.UIImage.FromImage(context.ToImage(), 0, orientation); // save the image as a jpeg return(resizedImage.AsPNG().ToArray()); } }
public override void DidCropToImage (TOCropViewController cropViewController, UIImage image, CoreGraphics.CGRect cropRect, nint angle) { DidCrop = true; try { if (image != null) App.CroppedImage = image.AsPNG().ToArray(); } catch (Exception ex) { Debug.WriteLine (ex.Message); } finally { if (image != null) { image.Dispose (); image = null; } } parent.DismissViewController (true, App.PopModal); }
private static Stream GetImageStream(UIImage image, ImageFormatType formatType) { if (formatType == ImageFormatType.Jpg) return image.AsJPEG().AsStream(); return image.AsPNG().AsStream(); }
public override void DidOutputImage(BlinkID blinkid, UIImage image, string name) { MessagingCenter.Send (new Messages.BlinkIDImage { Image = ImageSource.FromStream (() => image.AsPNG ().AsStream ()) }, Messages.BlinkIDImageMessage); }
public async Task SetGeneratedImageAsync (string key, UIImage uiImage, bool saveToDisk = true) { if (uiImage == null) { RemoveImage (key); return; } // // Does it already exist in memory? // SetMemoryImage (key, uiImage, DateTime.UtcNow); // // If we added it to memory, then save it to disk // if (saveToDisk) { await Task.Run (() => { FileSystemManager.EnsureDirectoryExists (cacheDirectory); var cachePath = GetCachePath (key); NSError err; uiImage.AsPNG ().Save (cachePath, false, out err); if (err != null) { Debug.WriteLine (err); } }); } }
// Parse UIImage to Byte[] public static byte[] UIImageToByteArray (UIImage _image) { Byte[] byteArray; using (NSData nsImageData = _image.AsPNG ()) { byteArray = new Byte[nsImageData.Length]; System.Runtime.InteropServices.Marshal.Copy (nsImageData.Bytes, byteArray, 0, Convert.ToInt32 (nsImageData.Length)); } return byteArray; }