public async Task UpdateImageSourceAsync() { if (PlatformView != null) { var token = this.SourceManager.BeginLoad(); var imageSource = _imageSourcePart(); if (imageSource != null) { #if IOS || ANDROID || WINDOWS var result = await imageSource.UpdateSourceAsync(PlatformView, ImageSourceServiceProvider, SetImage !, token) .ConfigureAwait(false); SourceManager.CompleteLoad(result); #elif TIZEN PlatformImage image = (PlatformView as PlatformImage) ?? new PlatformImage(PlatformView); var result = await imageSource.UpdateSourceAsync(image, ImageSourceServiceProvider, SetImage !, token) .ConfigureAwait(false); SourceManager.CompleteLoad(result); #else await Task.CompletedTask; #endif } else { SetImage?.Invoke(null); SourceManager.CompleteLoad(null); } } }
public async Task <ImageDataResult> GetImageDetails(byte[] image) { UIKit.UIImage uiImage = await image.ToImageAsync(); ImageDataResult data = new ImageDataResult { Width = (int)uiImage.Size.Width, Heigth = (int)uiImage.Size.Height }; return(data); }
public string getResult() { string path = NSBundle.MainBundle.PathForResource("qr", "PNG"); UIKit.UIImage image = new UIKit.UIImage(path); results = reader.DecodeImage(image, "", out error); if (results != null && results.Length > 0) { return(results[0].BarcodeText); } return("Barcode not found."); }
public async Task <string[]> GetImageDetails(byte[] image) { UIKit.UIImage uiImage = await image.ToImageAsync(); string[] data = new string[3] { uiImage.Size.Width.ToString(), uiImage.Size.Height.ToString(), image.Length.ToString() }; return(data); }
public static UIKit.UIImage ToImage(this byte[] data) { UIKit.UIImage image; try { image = new UIKit.UIImage(Foundation.NSData.FromArray(data)); } catch { return(null); } return(image); }
public static UIKit.UIImage ImageFromByteArray(byte[] data) { if (data == null) { return null; } UIKit.UIImage image; try { image = new UIKit.UIImage(Foundation.NSData.FromArray(data)); } catch (Exception e) { Console.WriteLine ("Image load failed: " + e.Message); return null; } return image; }
public IosImageSourceImpl(string name, Func <Stream> streamSource, int quality) { Name = name; _streamSource = streamSource; _quality = quality; using (var stream = streamSource.Invoke()) { image = UIKit.UIImage.LoadFromData(NSData.FromStream(stream)); var size = image?.Size ?? new CoreGraphics.CGSize(0, 0); Width = (int)size.Width; Height = (int)size.Height; Orientation = Orientation.Normal; } }
public static UIKit.UIImage ImageFromByteArray(byte[] data) { if (data == null) { return(null); } UIKit.UIImage image; try { image = new UIKit.UIImage(Foundation.NSData.FromArray(data)); } catch (Exception e) { Console.WriteLine("Image load failed: " + e.Message); return(null); } return(image); }
protected async override System.Threading.Tasks.Task <Tuple <UIKit.UIImage, UIKit.UIImage> > GetIcon(Page page) { string resourcePath = string.Empty; // Load the icon as a SVG vector if (TabbedPageIcons.TabbedPageIconsDictionary.TryGetValue(page.AutomationId, out resourcePath)) { UIKit.UIImage img = await ImageService.Instance .LoadEmbeddedResource(resourcePath) .WithCustomDataResolver(new SvgDataResolver(26, 0, true)) .AsUIImageAsync(); return(new Tuple <UIKit.UIImage, UIKit.UIImage>(img, img)); } return(await base.GetIcon(page)); }
public static Task <IImageSourceServiceResult <PlatformImage>?> GetPlatformImageAsync(this IImageSourceService imageSourceService, IImageSource?imageSource, IMauiContext mauiContext) { if (imageSource == null) { return(Task.FromResult <IImageSourceServiceResult <PlatformImage>?>(null)); } #if IOS || MACCATALYST return(imageSourceService.GetImageAsync(imageSource)); #elif ANDROID return(imageSourceService.GetDrawableAsync(imageSource, mauiContext.Context !)); #elif WINDOWS return(imageSourceService.GetImageSourceAsync(imageSource)); #elif TIZEN var platformImage = new PlatformImage(mauiContext.GetNativeParent()); return(imageSourceService.GetImageAsync(imageSource, platformImage)); #else throw new NotImplementedException(); #endif }
public static byte[] ResizeImageIOS(byte[] imageData, float width, float height, int quality) { UIImage originalImage = ImageFromByteArray(imageData); 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()); // save the image as a jpeg return(resizedImage.AsJPEG((float)quality).ToArray()); } }
//https://forums.xamarin.com/discussion/67531/how-to-resize-image-file-on-xamarin-forms-writeablebitmap-package-cant-be-added public byte[] GetThumbnailBytes(byte[] imageData, float width, float height) { UIKit.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; UIKit.UIGraphics.BeginImageContext(new System.Drawing.SizeF(width, height)); originalImage.Draw(new System.Drawing.RectangleF(0, 0, width, height)); var resizedImage = UIKit.UIGraphics.GetImageFromCurrentImageContext(); UIKit.UIGraphics.EndImageContext(); var bytesImagen = resizedImage.AsJPEG().ToArray(); resizedImage.Dispose(); return(bytesImagen); }
internal static _UIImage AsMonochrome(this _UIImage image, Color foreground) { var width = (int)image.Size.Width; var height = (int)image.Size.Height; var imageRect = new CGRect(0, 0, image.Size.Width, image.Size.Height); var rawData = new byte[width * height * 4]; var outputData = new byte[width * height * 4]; var handle = GCHandle.Alloc(rawData); try { using (var colorSpace = CGColorSpace.CreateDeviceRGB()) { using (var context = new CGBitmapContext( data: rawData, width: (nint)imageRect.Width, height: (nint)imageRect.Height, bitsPerComponent: 8, bytesPerRow: (nint)imageRect.Width * 4, colorSpace: colorSpace, bitmapInfo: CGImageAlphaInfo.PremultipliedLast ) ) { context.DrawImage(imageRect, image.CGImage); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { var index = x * 4 + y * height * 4; var sourceAlpha = rawData[index + 3]; outputData[index + 0] = foreground.R; outputData[index + 1] = foreground.G; outputData[index + 2] = foreground.B; outputData[index + 3] = sourceAlpha; } } } } } finally { handle.Free(); } using (var dataProvider = new CGDataProvider(outputData, 0, outputData.Length)) { using (var colorSpace = CGColorSpace.CreateDeviceRGB()) { var bitsPerComponent = 8; var bytesPerPixel = 4; using (var cgImage = new CGImage( width, height, bitsPerComponent, bitsPerComponent * bytesPerPixel, bytesPerPixel * width, colorSpace, CGImageAlphaInfo.Last, dataProvider, null, false, CGColorRenderingIntent.Default )) { return(FromCGImage(cgImage)); } } } }
public void SetImage(_UIImage image) => Image = image;
// on select of Gallery private async Task galleryFunc() { #if __IOS__ var picker = new MediaPicker(); try { if (!picker.PhotosSupported) { UserDialogs.Instance.HideLoading(); UserDialogs.Instance.Alert("Photo Gallery is unavailable"); } else { var result = await picker.PickPhotoAsync(); if (result == null) { return; } else { var memoryStream = new MemoryStream(); UserDialogs.Instance.ShowLoading("Uploading Image...", MaskType.Black); result.GetStream().CopyTo(memoryStream); imageByteArray_BlaffrViewModel = memoryStream.ToArray(); } Device.BeginInvokeOnMainThread(() => { ProfilePic = ImageSource.FromStream(() => result.GetStream()); }); UIKit.UIImage image = new UIKit.UIImage(result.Path); UIKit.UIImage highres = image; Foundation.NSData d = highres.AsJPEG(0.7f); Byte[] myByteArray = new Byte[d.Length]; System.Runtime.InteropServices.Marshal.Copy(d.Bytes, myByteArray, 0, Convert.ToInt32(d.Length)); imageByteArray_BlaffrViewModel = myByteArray; var p = ImageSource.FromStream(() => result.GetStream()); await this.OnPhotoReceived(result.Path, ""); } } catch (Exception ex) { string x = ex.Message; } #endif #if __ANDROID__ var picker = new CrossMedia(); try { //if (!picker.IsLocalVoiceInteractionSupported) //{ // UserDialogs.Instance.HideLoading(); // UserDialogs.Instance.Alert("Photo Gallery is unavailable"); //} //else //{ // var result = await picker(); // if (result == null) // return; // else // { // var memoryStream = new MemoryStream(); // UserDialogs.Instance.ShowLoading("Uploading Image...", MaskType.Black); // result.GetStream().CopyTo(memoryStream); // imageByteArray_ViewModel = memoryStream.ToArray(); // } // Device.BeginInvokeOnMainThread(() => // { // imageSource = ImageSource.FromStream(() => result.GetStream()); // }); // //UIKit.UIImage image = new UIKit.UIImage(result.Path); // //var highres = image; // //var d = highres.AsJPEG(0.7f); // //Byte[] myByteArray = new Byte[d.Length]; // //System.Runtime.InteropServices.Marshal.Copy(d.Bytes, myByteArray, 0, Convert.ToInt32(d.Length)); // //imageByteArray_ViewModel = myByteArray; // //var p = ImageSource.FromStream(() => result.GetStream()); // await this.OnPhotoReceived(result.Path, ""); //} } catch (Exception ex) { string x = ex.Message; } #endif }
public async Task <byte[]> GetImage() { UIKit.UIImage image = UIKit.UIImage.FromBundle("ImageJPG.jpg"); return(image.AsJPEG().ToArray()); }
protected virtual PImage Transform(PImage sourceBitmap, string path, ImageSource source, bool isPlaceholder, string key) { return(sourceBitmap); }
private Image(UIKit.UIImage image) { NativeImage = image; Width = (int)image.Size.Width; Height = (int)image.Size.Height; }
public RemoteImage(Uri uri, PlatformImage img) : this(uri) { IsLoaded = true; Bitmap = img; }
public EntryBase(float y, NSUIImage icon, object data) : this(y) { Icon = icon; Data = data; }
public virtual void Dispose(bool disposing) { NativeImage = null; }
/// <summary> /// 文件另存为 /// 过程和打开文件相同,先检查本地 /// </summary> public async void SaveAs() { if (State != FileItemState.None || string.IsNullOrEmpty(ID)) { return; } string fileName = Path.Combine(Kit.CachePath, GetFileName()); if (!File.Exists(fileName)) { // 先下载 bool suc = await Download(); if (!suc) { return; } } #if UWP FileSavePicker picker = new FileSavePicker(); picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary; picker.FileTypeChoices.Add(GetSaveDesc(), new List <string>() { GetExtName() }); picker.SuggestedFileName = _itemInfo.FileName; StorageFile file = await picker.PickSaveFileAsync(); if (file != null) { var folder = await StorageFolder.GetFolderFromPathAsync(Kit.CachePath); var temp = await folder.TryGetItemAsync(GetFileName()) as StorageFile; if (temp != null) { await temp.CopyAndReplaceAsync(file); Kit.Msg("文件另存成功!"); return; } } #elif ANDROID //string savePath; //string msg; //if (FileType == FileItemType.Image) //{ // savePath = IOUtil.GetPicturesPath(); // msg = "照片"; //} //else if (FileType == FileItemType.Video) //{ // savePath = IOUtil.GetMoviesPath(); // msg = "电影"; //} //else if (FileType == FileItemType.Sound) //{ // savePath = IOUtil.GetMusicPath(); // msg = "音乐"; //} //else //{ // savePath = IOUtil.GetDownloadsPath(); // msg = "下载"; //} try { // 复制本地文件 // android 11.0 因放在其他公共目录时被过滤显示,统一放在下载 File.Copy(fileName, Path.Combine(IOUtil.GetDownloadsPath(), GetFileName()), true); Kit.Msg("已保存到下载目录!"); } catch { Kit.Warn("文件保存失败!"); } #elif IOS if (FileType == FileItemType.Image || FileType == FileItemType.Video) { var status = await Permissions.CheckStatusAsync <Permissions.Photos>(); if (status != PermissionStatus.Granted) { status = await Permissions.RequestAsync <Permissions.Photos>(); } if (status == PermissionStatus.Granted) { if (FileType == FileItemType.Image) { var imageData = System.IO.File.ReadAllBytes(fileName); var myImage = new UIKit.UIImage(Foundation.NSData.FromArray(imageData)); myImage.SaveToPhotosAlbum((image, error) => { if (error == null) { Kit.Msg("已保存到照片!"); } }); } else { UIKit.UIVideo.SaveToPhotosAlbum(fileName, (image, error) => { if (error == null) { Kit.Msg("已保存到照片!"); } }); } } } else { // 普通文件以共享方式保存 await ShareFile(); } #endif }
protected override UIKit.UIImage Transform(UIKit.UIImage source) { RGBAWMatrix = FFColorMatrix.ColorToTintMatrix(R, G, B, A); return(base.Transform(source)); }
public EntryBase(float y, NSUIImage icon) : this(y) { Icon = icon; }