public async void SetSource(ImageSource source) { try { if (!string.IsNullOrWhiteSpace(_cacheKey)) { await ImageService.Instance.InvalidateCacheEntryAsync(_cacheKey, FFImageLoading.Cache.CacheType.Memory, true); } if (!string.IsNullOrWhiteSpace(_refinedCacheKey)) { await ImageService.Instance.InvalidateCacheEntryAsync(_cacheKey, FFImageLoading.Cache.CacheType.Memory, true); } _imageGuid = Guid.NewGuid(); _cacheKey = _imageGuid.ToString(); _refinedCacheKey = $"{_imageGuid.ToString()}-Refined"; TaskParameter task = null; TaskParameter taskRefined = null; var fileSource = source as FileImageSource; if (fileSource != null) { task = ImageService.Instance.LoadFile(fileSource.File); taskRefined = ImageService.Instance.LoadFile(fileSource.File); } var urlSource = source as UriImageSource; if (urlSource != null) { task = ImageService.Instance.LoadUrl(urlSource.Uri?.OriginalString); taskRefined = ImageService.Instance.LoadUrl(urlSource.Uri?.OriginalString); } var streamSource = source as StreamImageSource; if (streamSource != null) { task = ImageService.Instance.LoadStream(streamSource.Stream); taskRefined = ImageService.Instance.LoadStream(streamSource.Stream); } using (var stream = await task.DownSample(PreviewResolution, PreviewResolution).AsJPGStreamAsync(90)) { byte[] bytes = StreamToByteArray(stream); _source = ImageSource.FromStream(() => new MemoryStream(bytes)); } using (var streamRefined = await taskRefined.DownSample(RefinedResolution, RefinedResolution).AsJPGStreamAsync(90)) { byte[] bytes = StreamToByteArray(streamRefined); _refinedSource = ImageSource.FromStream(() => new MemoryStream(bytes)); } LoadRefinedImage(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); } }
private async void TakePhotoMethod() { var file = await CameraHelper.TakePhoto(); if (file != null) { Image = ImageSource.FromStream(() => file.GetStream()); } }
async void TakePicture(object sender, EventArgs e) { await CrossMedia.Current.Initialize(); if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported) { await DisplayAlert("No Camera", "No Persmission to use Camera", "OK"); return; } var file = await CrossMedia.Current.TakePhotoAsync( new StoreCameraMediaOptions { SaveToAlbum = false, PhotoSize = PhotoSize.Small }); var img = ImageSource.FromStream(() => { using (var memoryStream = new MemoryStream()) { file.GetStream().CopyTo(memoryStream); vm.Bytes = memoryStream.ToArray(); } var stream = file.GetStream(); file.Dispose(); return(stream); }); vm.UsedBytes = null; TakenPicture.Source = img; vm.Picture = img; EditPictureButton.IsEnabled = true; SavePictureButton.IsEnabled = true; if (file == null) { return; } }
async Task LoadData(int page) { ObservableCollection <messages> tempOpenData; if (page == 1) { tempOpenData = new ObservableCollection <messages>(); } else { tempOpenData = new ObservableCollection <messages>(MessageList); } using (HttpClient hc = new HttpClient()) { try { IsBusy = true; bool IsBookMarkSelect = false; if (App.FrameImage != null) { ImageUrl = App.FrameImage; isImageVisible = true; } if (App.FileName != null) { FileName = App.FileName; isDocsVisible = true; } var jsonString = await hc.GetStringAsync(Config.MESSAGE_THREAD_API + threadId + "/" + page); if (jsonString != "") { var obj = JsonConvert.DeserializeObject <DealMessageThreadList>(jsonString); if (obj != null) { foreach (var user in obj.messages) { ThreadList.Clear(); if (string.IsNullOrEmpty(user.plainContent)) { user.plainContent = ""; } if (user.plainContent.Contains("<p><br/></p>")) { string value = user.plainContent.Replace("<p><br/></p>", null); user.plainContent = value; } if (user.plainContent.Contains("<li>")) { string value = user.plainContent.Replace("?", ""); user.plainContent = value; } if (user.plainContent.Contains("data:image")) { string[] delim1 = { "<img src=\"data:image/png;base64," }; var MessageValue = user.plainContent.Split(delim1, StringSplitOptions.None)[0]; var image = user.plainContent.Split(delim1, StringSplitOptions.None)[1]; var image1 = image.Split('"')[0]; //Debug.Write(image1); string[] delim = { "alt=\"\">" }; var stringMsg = image.Split(delim, StringSplitOptions.None); var Base64Stream = Convert.FromBase64String(image1); user.MsgImageUrl = ImageSource.FromStream(() => new MemoryStream(Base64Stream)); user.IsImageVisible = true; int index = 2; if (index == stringMsg.Length) { if (stringMsg[1] != null) { user.plainContent = MessageValue + stringMsg[1]; } } else { string message = Regex.Replace(MessageValue, HTML_TAG_PATTERN, string.Empty); if (!string.IsNullOrEmpty(message)) { user.plainContent = MessageValue; } else { user.plainContent = string.Empty; } } } string authorIdStr = user.authorId; if (user.messageUsers != null) { foreach (var thread in user.messageUsers) { if (thread.userid_10 == userId) { if (thread.followUp == true) { IsBookMarkSelect = true; } else { IsBookMarkSelect = false; } } ThreadList.Add(thread); } if (IsBookMarkSelect == true) { user.BookMarkImg = "orange_bookmark.png"; } else { user.BookMarkImg = "bookmark.png"; } } if (authorIdStr == null) { user.HorizontalOption = LayoutOptions.StartAndExpand; user.IsSenderBookMarkVisible = false; user.IsSenderProfileVisible = true; user.IsProfileVisible = false; user.IsBookMarkVisible = true; } else if (authorIdStr.Equals(userId)) { user.HorizontalOption = LayoutOptions.EndAndExpand; user.IsSenderBookMarkVisible = true; user.IsSenderProfileVisible = false; user.IsProfileVisible = true; user.IsBookMarkVisible = false; } else { user.HorizontalOption = LayoutOptions.StartAndExpand; user.IsSenderBookMarkVisible = false; user.IsSenderProfileVisible = true; user.IsProfileVisible = false; user.IsBookMarkVisible = true; } if (user.plainContent == null) { user.IsStopVisible = false; user.HeightRequest = 0; } else if (user.plainContent.Count() < 150) { user.IsStopVisible = false; user.HeightRequest = 0; } else if (user.plainContent.Count() > 150) { user.IsStopVisible = true; user.moreBtnText = "more"; user.HeightRequest = 35; user.MaxLines = 3; } tempOpenData.Add(user); } TotalRecords = obj.totalRecords; _LastPage = Convert.ToInt32(obj.totalPages); ObservableCollection <messages> OrderbyIdDesc = new ObservableCollection <messages>(tempOpenData.OrderByDescending(x => x.createdDate.Date)); MessageList = new ObservableCollection <messages>(OrderbyIdDesc); } } } catch (Exception ex) { Debug.WriteLine("Exception:>" + ex); } finally { IsBusy = false; } } }
public async void SetSource(ImageSource source) { try { await _lock.WaitAsync(); if (!string.IsNullOrWhiteSpace(_cacheKey)) { await ImageService.Instance.InvalidateCacheEntryAsync(_cacheKey, FFImageLoading.Cache.CacheType.Memory, true); } if (!string.IsNullOrWhiteSpace(_refinedCacheKey)) { await ImageService.Instance.InvalidateCacheEntryAsync(_cacheKey, FFImageLoading.Cache.CacheType.Memory, true); } if (source == null) { _cacheKey = null; _refinedCacheKey = null; _source = null; _refinedSource = null; _originalSource = null; Source = null; return; } var imageGuid = Guid.NewGuid(); _cacheKey = imageGuid.ToString(); _refinedCacheKey = $"{imageGuid.ToString()}-Refined"; _originalSource = source; TaskParameter task = null; TaskParameter taskRefined = null; var fileSource = source as FileImageSource; if (fileSource != null) { task = ImageService.Instance.LoadFile(fileSource.File); taskRefined = ImageService.Instance.LoadFile(fileSource.File); Stream = null; Path = fileSource.File; SourceType = FFImageLoading.Work.ImageSource.Filepath; } var urlSource = source as UriImageSource; if (urlSource != null) { task = ImageService.Instance.LoadUrl(urlSource.Uri?.OriginalString); taskRefined = ImageService.Instance.LoadUrl(urlSource.Uri?.OriginalString); Stream = null; Path = urlSource.Uri?.OriginalString; SourceType = FFImageLoading.Work.ImageSource.Url; } var streamSource = source as StreamImageSource; if (streamSource != null) { task = ImageService.Instance.LoadStream(streamSource.Stream); taskRefined = ImageService.Instance.LoadStream(streamSource.Stream); Stream = streamSource.Stream; Path = null; SourceType = FFImageLoading.Work.ImageSource.Stream; } if (ImageRotation != 0) { var rotateTransformation = new RotateTransformation(Math.Abs(ImageRotation), ImageRotation < 0) { Resize = true }; task.Transform(rotateTransformation); taskRefined.Transform(rotateTransformation); } using (var stream = await task.DownSample(PreviewResolution, PreviewResolution).AsJPGStreamAsync(90)) { byte[] bytes = StreamToByteArray(stream); _source = ImageSource.FromStream(() => new MemoryStream(bytes)); } using (var streamRefined = await taskRefined.DownSample(RefinedResolution, RefinedResolution).AsJPGStreamAsync(90)) { byte[] bytes = StreamToByteArray(streamRefined); _refinedSource = ImageSource.FromStream(() => new MemoryStream(bytes)); } LoadRefinedImage(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); } finally { _lock.Release(); } }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return((!(value is byte[] file)) ? null : ImageSource.FromStream(() => new System.IO.MemoryStream(file))); }