private async Task AdvanceFrame() { if (_bitmapDecoder.FrameCount == 0) { return; } var frameIndex = _currentFrameIndex; var frameProperties = _frameProperties[frameIndex]; var disposeRequested = _disposeRequested; // Increment frame index and loop count _currentFrameIndex++; if (_currentFrameIndex >= _bitmapDecoder.FrameCount) { _completedLoops++; _currentFrameIndex = 0; } // Set flag to clear before next frame if necessary _disposeRequested = frameProperties.ShouldDispose; // Set up the timer to display the next frame if (_imageProperties.IsAnimated && (_imageProperties.LoopCount == 0 || _completedLoops < _imageProperties.LoopCount)) { _animationTimer.Interval = TimeSpan.FromMilliseconds(frameProperties.DelayMilliseconds); } else { _animationTimer.Stop(); } // Decode the frame var frame = await _bitmapDecoder.GetFrameAsync((uint)frameIndex); var pixelData = await frame.GetPixelDataAsync( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, new BitmapTransform(), ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage ); var pixels = pixelData.DetachPixelData(); var frameRectangle = frameProperties.Rect; var shouldClear = disposeRequested || frameIndex == 0; // Compose and display the frame try { PrepareFrame(pixels, frameRectangle, shouldClear); UpdateImageSource(frameRectangle); } catch (Exception e) when(_canvasImageSource.Device.IsDeviceLost(e.HResult)) { // XAML will also raise a SurfaceContentsLost event, and we use this to trigger // redrawing the surface. Therefore, ignore this error. } }
/// <summary> /// Get the bytes for next frame, update the imagebuffer and invalidate the canvas to trigger a redraw with the new imagebuffer. Finally we set trigger a change frame which we're observing. /// </summary> private async Task ChangeCurrentFrameAsync() { try { var time = Stopwatch.StartNew(); var frame = await _decoder.GetFrameAsync((uint)_currentFrameIndex); var pixelData = await frame.GetPixelDataAsync( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, new BitmapTransform(), ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage ); CreateActualPixels(pixelData.DetachPixelData()); var newDelay = _currentGifFrame.DelayMilliseconds - time.ElapsedMilliseconds; if (newDelay > 0 && time.ElapsedMilliseconds < 300) { await Task.Delay((int)newDelay); } time.Stop(); _canvasControl?.Invalidate(); SetNextFrame(); } catch (Exception) { // We could potentially crash by leaving the viewport in the middle of a sequence StopByCatch(); } }
private async void CropBtn_Click(object sender, RoutedEventArgs e) { BitmapDecoder decoder = null; BitmapImage bImage = original.Source as BitmapImage; if (storageFile == null && bImage.UriSource != null) { storageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx://" + bImage.UriSource.AbsolutePath)); } using (IRandomAccessStream streamWithContent = await storageFile.OpenAsync(FileAccessMode.Read)) { decoder = await BitmapDecoder.CreateAsync(streamWithContent); BitmapFrame bitmapFrame = await decoder.GetFrameAsync(0); WB_CapturedImage = new WriteableBitmap((int)bitmapFrame.PixelWidth, (int)bitmapFrame.PixelHeight); Size cropsize = new Size(Math.Abs(Point2.X - Point1.X), Math.Abs(Point2.Y - Point1.Y)); double originalImageWidth = WB_CapturedImage.PixelWidth; double originalImageHeight = WB_CapturedImage.PixelHeight; // Get the size of the image when it is displayed on the phone double displayedWidth = original.ActualWidth; double displayedHeight = original.ActualHeight; // Calculate the ratio of the original image to the displayed image double widthRatio = originalImageWidth / displayedWidth; double heightRatio = originalImageHeight / displayedHeight; WB_CroppedImage = await GetCroppedBitmapAsync(streamWithContent, Point1, cropsize, widthRatio, heightRatio); FinalCroppedImage.Source = WB_CroppedImage; } }
public async Task GetThumbnailFromMedia() { // Create a stream from the resource URI that we have var uri = new Uri("ms-appx:///silence with album art.mp3"); var file = await StorageFile.GetFileFromApplicationUriAsync(uri); var stream = await file.OpenAsync(FileAccessMode.Read); // CreateFFmpegInteropMSSFromUri should return null if uri is blank with default parameter FFmpegInteropMSS FFmpegMSS = await FFmpegInteropMSS.CreateFromStreamAsync(stream); Assert.IsNotNull(FFmpegMSS); var thumbnailData = FFmpegMSS.ExtractThumbnail(); Assert.IsNotNull(thumbnailData); // Verify that we have a valid bitmap using (IRandomAccessStream thumbnailstream = thumbnailData.Buffer.AsStream().AsRandomAccessStream()) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(thumbnailstream); var bitmap = await decoder.GetFrameAsync(0); Assert.IsNotNull(bitmap); } }
public static async Task <Uri> ResizeImage(Uri uri, uint size) { if (uri.IsFile) { return(uri); } var response = await HttpWebRequest.Create(uri).GetResponseAsync(); List <Byte> allBytes = new List <byte>(); using (Stream imageStream = response.GetResponseStream()) { InMemoryRandomAccessStream downloadedImageRas = new InMemoryRandomAccessStream(); await RandomAccessStream.CopyAndCloseAsync(imageStream.AsInputStream(), downloadedImageRas.GetOutputStreamAt(0)); BitmapDecoder bmpDecoder = await BitmapDecoder.CreateAsync(downloadedImageRas); BitmapFrame frame = await bmpDecoder.GetFrameAsync(0); BitmapTransform bmpTrans = new BitmapTransform(); bmpTrans.InterpolationMode = BitmapInterpolationMode.Cubic; /*BitmapBounds bounds = new BitmapBounds(); * bounds.X = 0; * bounds.Y = 0; * bounds.Height = 360; * bounds.Height = 360; * bmpTrans.Bounds = bounds;*/ bmpTrans.ScaledHeight = size; bmpTrans.ScaledWidth = size; PixelDataProvider pixelDataProvider = await frame.GetPixelDataAsync(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Ignore, bmpTrans, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.ColorManageToSRgb); byte[] pixelData = pixelDataProvider.DetachPixelData(); InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream(); BitmapEncoder enc = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, ras); // write the pixel data to our stream enc.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Ignore, size, size, bmpDecoder.DpiX, bmpDecoder.DpiY, pixelData); await enc.FlushAsync(); string fileName = Path.GetRandomFileName(); var file = await ApplicationData.Current.LocalFolder.CreateFileAsync( fileName + ".jpg", CreationCollisionOption.GenerateUniqueName); using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite)) { await RandomAccessStream.CopyAndCloseAsync(ras.GetInputStreamAt(0), fileStream.GetOutputStreamAt(0)); } return(new Uri("ms-appdata:///local/" + file.Name)); } }
public static async Task <BitmapFrame> LoadLogoBitmapAsync() { var uri = new Uri("ms-appx:///Assets/coffee-logo.png"); StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uri); using (var readStream = await file.OpenReadAsync()) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(readStream); return(await decoder.GetFrameAsync(0)); } }
/// <summary> /// GetPixelFrame /// </summary> /// <param name="bitmap"></param> /// <returns></returns> public async Task <List <byte[]> > GetPixelFrame(BitmapDecoder bitmap) { List <byte[]> frames = new List <byte[]>(); for (uint f = 0; f < FrameCount; f++) { BitmapFrame frame = await bitmap.GetFrameAsync(f).AsTask().ConfigureAwait(false); PixelDataProvider pixelData = await frame.GetPixelDataAsync().AsTask().ConfigureAwait(false); frames.Add(pixelData.DetachPixelData()); } return(frames); }
private async Task <byte[]> DecodePixelAsync(uint frameIndex) { var frame = await _bitmapDecoder.GetFrameAsync(frameIndex).AsTask().ConfigureAwait(false); var pixelData = await frame.GetPixelDataAsync( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, new BitmapTransform(), ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage ).AsTask().ConfigureAwait(false); var pixels = pixelData.DetachPixelData(); return(pixels); }
private static async Task <byte[]> GetRGBABytesAsync(Stream imageStream, CancellationToken cancellationToken) { byte[] result; bool overrideStream = !imageStream.CanSeek; Stream stream; if (overrideStream) { stream = new MemoryStream(); imageStream.CopyTo(stream); } else { stream = imageStream; } using (var ras = stream.AsRandomAccessStream()) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(ras).AsTask(cancellationToken).ConfigureAwait(false); BitmapFrame frame = await decoder.GetFrameAsync(0).AsTask(cancellationToken).ConfigureAwait(false); BitmapTransform transform = new BitmapTransform() { ScaledWidth = decoder.PixelWidth, ScaledHeight = decoder.PixelHeight }; PixelDataProvider pixelData = await frame.GetPixelDataAsync( BitmapPixelFormat.Rgba8, BitmapAlphaMode.Premultiplied, transform, ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage) .AsTask(cancellationToken).ConfigureAwait(false); result = pixelData.DetachPixelData(); } if (overrideStream) { stream.Dispose(); stream = null; } return(result); }
public async Task GetThumbnailFromMedia() { // Create a stream from the resource URI that we have var uri = new Uri("ms-appx:///TestFiles//silence with album art.mp3"); StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uri); IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read); // CreateFFmpegInteropMSSFromUri should return null if uri is blank with default parameter MediaStreamSource mss = CreateMSSFromStream(stream, null); // Verify that we have a valid bitmap using (IRandomAccessStream thumbnailStream = await mss.Thumbnail.OpenReadAsync()) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(thumbnailStream); BitmapFrame bitmap = await decoder.GetFrameAsync(0); } }
public async Task <WriteableBitmap> getPNG(StorageFile sFile) { try { IRandomAccessStream stream = await sFile.OpenReadAsync(); BitmapDecoder decoder = await BitmapDecoder.CreateAsync(BitmapDecoder.PngDecoderId, stream); // 获取第一帧 BitmapFrame frame = await decoder.GetFrameAsync(0); // 获取像素数据 PixelDataProvider pixprd = await frame.GetPixelDataAsync(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, new BitmapTransform(), ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage); byte[] rgbaBuff = pixprd.DetachPixelData(); byte[] res = new byte[rgbaBuff.Length]; for (int i = 0; i < rgbaBuff.Length; i += 4) { byte r = rgbaBuff[i]; byte g = rgbaBuff[i + 1]; byte b = rgbaBuff[i + 2]; byte a = rgbaBuff[i + 3]; if (r == 0 && g == 0 && b == 0) { a = 0; } // 反色就是用255分别减去R,G,B的值 res[i] = r; res[i + 1] = g; res[i + 2] = b; res[i + 3] = a; } // 创建新的位图对象 WriteableBitmap wb = new WriteableBitmap((int)frame.PixelWidth, (int)frame.PixelHeight); // 复制数据 res.CopyTo(wb.PixelBuffer); return(wb); } catch (Exception) { throw; } }
async Task DetectBitmapInBackgroundAsync(IBuffer proxyResult, double factor, int min, int size, ImageCropper parent) { // Decode BitmapDecoder bd = await BitmapDecoder.CreateAsync(proxyResult.AsStream().AsRandomAccessStream()); BitmapFrame bf = await bd.GetFrameAsync(0); // Detect AnimeFaceDetector c = new AnimeFaceDetector(); c.LoadCascade(); var s = await c.DetectBitmap(bf, factor, min, new Size(size, size)); await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { parent.Rects = s.ToList(); }); }
async Task <BitmapSource> LoadBitmapAsync(IRandomAccessStream stream) { WriteableBitmap bitmap = null; // Create a BitmapDecoder from the stream BitmapDecoder decoder = null; try { decoder = await BitmapDecoder.CreateAsync(stream); } catch { // Just skip ones that aren't valid return(null); } // Get the first frame BitmapFrame bitmapFrame = await decoder.GetFrameAsync(0); // Get the pixels PixelDataProvider dataProvider = await bitmapFrame.GetPixelDataAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, new BitmapTransform(), ExifOrientationMode.RespectExifOrientation, ColorManagementMode.ColorManageToSRgb); byte[] pixels = dataProvider.DetachPixelData(); // Create WriteableBitmap and set the pixels bitmap = new WriteableBitmap((int)bitmapFrame.PixelWidth, (int)bitmapFrame.PixelHeight); using (Stream pixelStream = bitmap.PixelBuffer.AsStream()) { pixelStream.Write(pixels, 0, pixels.Length); } bitmap.Invalidate(); return(bitmap); }
async void OnPasteAppBarButtonClick(object sender, RoutedEventArgs args) { // Temporarily disable the Paste button Button button = sender as Button; button.IsEnabled = false; // Get the Clipboard contents and check for a bitmap DataPackageView dataView = Clipboard.GetContent(); if (dataView.Contains(StandardDataFormats.Bitmap)) { // Get the stream reference and a stream RandomAccessStreamReference streamRef = await dataView.GetBitmapAsync(); IRandomAccessStreamWithContentType stream = await streamRef.OpenReadAsync(); // Create a BitmapDecoder for reading the bitmap BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); BitmapFrame bitmapFrame = await decoder.GetFrameAsync(0); PixelDataProvider pixelProvider = await bitmapFrame.GetPixelDataAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, new BitmapTransform(), ExifOrientationMode.RespectExifOrientation, ColorManagementMode.ColorManageToSRgb); // Save information sufficient for creating WriteableBitmap pastedPixelWidth = (int)bitmapFrame.PixelWidth; pastedPixelHeight = (int)bitmapFrame.PixelHeight; pastedPixels = pixelProvider.DetachPixelData(); // Check if it's OK to replace the current painting await CheckIfOkToTrashFile(FinishPasteBitmapAndPixelArray); } // Re-enable the button and close the app bar button.IsEnabled = true; this.BottomAppBar.IsOpen = false; }
public async Task <byte[]> DecodeAsync(BitmapDecoder bitmapDecoder) { BitmapFrame bitmapFrame = null; if (_bitmapFrame == null || !_bitmapFrame.TryGetTarget(out bitmapFrame)) { bitmapFrame = await bitmapDecoder.GetFrameAsync(FrameIndex).AsTask().ConfigureAwait(false); _bitmapFrame = new WeakReference <BitmapFrame>(bitmapFrame); } var pixelData = await bitmapFrame.GetPixelDataAsync( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, new BitmapTransform(), ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage ).AsTask().ConfigureAwait(false); return(pixelData.DetachPixelData()); }
public async Task <byte[]> DecodeAsync(BitmapDecoder bitmapDecoder) { if (_isDecode) { return(this.Pixels); } var frame = await bitmapDecoder.GetFrameAsync(FrameIndex).AsTask().ConfigureAwait(false); var pixelData = await frame.GetPixelDataAsync( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, new BitmapTransform(), ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage ).AsTask().ConfigureAwait(false); this.Pixels = pixelData.DetachPixelData(); _isDecode = true; return(Pixels); }
/// <summary> /// Get image basic information for further rendering usage /// </summary> /// <param name="imageFilePath">Path to the image file</param> /// <returns>Image width and height</returns> public async static Task <Tuple <uint, uint> > GetImageInfoForRendering(string imageFilePath) { try { var file = await StorageFile.GetFileFromPathAsync(imageFilePath); using (var s = await file.OpenReadAsync()) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(BitmapDecoder.JpegDecoderId, s); var frame = await decoder.GetFrameAsync(0); // Store image width and height for following rendering return(new Tuple <uint, uint>(frame.PixelWidth, frame.PixelHeight)); } } catch { return(new Tuple <uint, uint>(0, 0)); } }
private static async Task <DetechResult> DetechFromBufferAsync(IBuffer buffer, int minNeighbors = 5, Single minSize = 30) { // Detect the face(s) var c = new Imaging.AnimeFaceDetector(); BitmapDecoder bd = await BitmapDecoder.CreateAsync(buffer.AsStream().AsRandomAccessStream()); BitmapFrame bf = await bd.GetFrameAsync(0); List <Rect> rects = new List <Rect>(); await Task.Run(async() => { var s = await c.DetectBitmap(bf, 1.05, minNeighbors, new Size(minSize, minSize)); rects = s.ToList(); }); return(new DetechResult() { Faces = rects, FrameSize = new Size(bf.PixelWidth, bf.PixelHeight) }); }
async Task LoadBitmapFromFile(StorageFile storageFile) { using (IRandomAccessStreamWithContentType stream = await storageFile.OpenReadAsync()) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); BitmapFrame bitmapframe = await decoder.GetFrameAsync(0); PixelDataProvider dataProvider = await bitmapframe.GetPixelDataAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, new BitmapTransform(), ExifOrientationMode.RespectExifOrientation, ColorManagementMode.ColorManageToSRgb); pixels = dataProvider.DetachPixelData(); bitmap = new WriteableBitmap((int)bitmapframe.PixelWidth, (int)bitmapframe.PixelHeight); await InitializeBitmap(); } }
private async void ProxyImage_ImageOpened(object sender, RoutedEventArgs e) { // Download IBuffer proxyResult = null; HttpClient client = new HttpClient(); try { proxyResult = await client.GetBufferAsync(new Uri(ProxySource)); } catch (Exception ex) { return; } // Detect var factor = 1.05; var min = 3; var size = 25; AnimeFaceDetector c; c = new AnimeFaceDetector(); BitmapDecoder bd = await BitmapDecoder.CreateAsync(proxyResult.AsStream().AsRandomAccessStream()); BitmapFrame bf = await bd.GetFrameAsync(0); try { var s = await c.DetectBitmap(bf, factor, min, new Size(size, size)); Rects = s.ToList(); } catch (Exception ex) { } }
private async void ProxyImage_ImageOpened(object sender, RoutedEventArgs e) { // Download IBuffer proxyResult = null; HttpClient client = new HttpClient(); try { proxyResult = await client.GetBufferAsync(new Uri(ProxySource)); } catch (Exception ex) { return; } // Detect var factor = 1.05; var min = 3; var size = 25; BitmapDecoder bd = await BitmapDecoder.CreateAsync(proxyResult.AsStream().AsRandomAccessStream()); BitmapFrame bf = await bd.GetFrameAsync(0); //c.LoadCascadeFile(); try { //await DetectBitmapInBackgroundAsync(c, bf, factor, min, size); await Task.Run(async() => await DetectBitmapInBackgroundAsync(bf, factor, min, size, this)); } catch (Exception ex) { } }
public static async Task <IRandomAccessStream> ResizeImageFile(Uri uri, uint size) { StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uri); using (var fileStream = await file.OpenAsync(FileAccessMode.Read)) { BitmapDecoder bmpDecoder = await BitmapDecoder.CreateAsync(fileStream); BitmapFrame frame = await bmpDecoder.GetFrameAsync(0); BitmapTransform bmpTrans = new BitmapTransform(); bmpTrans.InterpolationMode = BitmapInterpolationMode.Cubic; /*BitmapBounds bounds = new BitmapBounds(); * bounds.X = 0; * bounds.Y = 0; * bounds.Height = 360; * bounds.Height = 360; * bmpTrans.Bounds = bounds;*/ bmpTrans.ScaledHeight = size; bmpTrans.ScaledWidth = size; PixelDataProvider pixelDataProvider = await frame.GetPixelDataAsync(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Ignore, bmpTrans, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.ColorManageToSRgb); byte[] pixelData = pixelDataProvider.DetachPixelData(); InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream(); BitmapEncoder enc = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, ras); // write the pixel data to our stream enc.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Ignore, size, size, bmpDecoder.DpiX, bmpDecoder.DpiY, pixelData); await enc.FlushAsync(); return(ras); } }
private async void Gif(IRandomAccessStream fileStream) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream); BitmapFrame frame = await decoder.GetFrameAsync(0); var frameProperties = await frame.BitmapProperties.GetPropertiesAsync(new List <string>()); var imageDescriptionProperties = await(frameProperties["/imgdesc"].Value as BitmapPropertiesView).GetPropertiesAsync(new List <string>() { "/Top", "/Left", "/Width", "/Height" }); int top = Int32.Parse(imageDescriptionProperties["/Top"].Value.ToString()); int left = Int32.Parse(imageDescriptionProperties["/Left"].Value.ToString()); int width = Int32.Parse(imageDescriptionProperties["/Width"].Value.ToString()); int height = Int32.Parse(imageDescriptionProperties["/Height"].Value.ToString()); var gifControlExtensionProperties = await(frameProperties["/grctlext"].Value as BitmapPropertiesView).GetPropertiesAsync(new List <string>() { "/Delay", "/UserInputFlag" }); TimeSpan delay = TimeSpan.FromSeconds(Double.Parse(gifControlExtensionProperties["/Delay"].Value.ToString()) / 100); // delay is in 1/100s of a second bool userInputFlag = bool.Parse(gifControlExtensionProperties["/UserInputFlag"].Value.ToString()); }
private async Task AdvanceFrame() { if (_bitmapDecoder.FrameCount == 0) { return; } var frameIndex = _currentFrameIndex; BitmapFrame frame = null; if (!_frameProperties[frameIndex].HasValue) { frame = await _bitmapDecoder.GetFrameAsync((uint)frameIndex); _frameProperties[frameIndex] = await RetrieveFramePropertiesAsync(frame); } FrameProperties frameProperties = _frameProperties[frameIndex].Value; // Increment frame index and loop count _currentFrameIndex++; if (_currentFrameIndex >= _bitmapDecoder.FrameCount) { _completedLoops++; _currentFrameIndex = 0; } // Set up the timer to display the next frame if (_imageProperties.IsAnimated && (_imageProperties.LoopCount == 0 || _completedLoops < _imageProperties.LoopCount)) { _animationTimer.Interval = TimeSpan.FromMilliseconds(frameProperties.DelayMilliseconds); } else { _animationTimer.Stop(); } // Decode the frame if (frame == null) { frame = await _bitmapDecoder.GetFrameAsync((uint)frameIndex); } var pixelData = await frame.GetPixelDataAsync( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, new BitmapTransform(), ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage ); var pixels = pixelData.DetachPixelData(); var frameRectangle = frameProperties.Rect; var disposeRectangle = Rect.Empty; if (frameIndex > 0) { var previousFrameProperties = _frameProperties[frameIndex - 1].GetValueOrDefault(); if (previousFrameProperties.ShouldDispose) { // Clear the pixels from the last frame disposeRectangle = previousFrameProperties.Rect; } } else { disposeRectangle = new Rect(0, 0, _imageProperties.PixelWidth, _imageProperties.PixelHeight); } // Compose and display the frame try { PrepareFrame(pixels, frameRectangle, disposeRectangle); UpdateImageSource(frameRectangle); } catch (Exception e) when(_canvasImageSource.Device.IsDeviceLost(e.HResult)) { // XAML will also raise a SurfaceContentsLost event, and we use this to trigger // redrawing the surface. Therefore, ignore this error. } }
/// <summary> /// 문자열 이미지 uri를 받아서 이미지를 로컬에 저장하고 BitmapImage로 반환한다. /// </summary> /// <param name="imageUri"></param> /// <returns></returns> public async Task <BitmapImage> UriImageSaveLocalAsync(string imageUri, bool retry = true) { if (string.IsNullOrEmpty(imageUri) == true) { return(null); } //폴더 초기화 될때까지 대기 while (files == null) { await TaskEx.Delay(500); } //Stream var iuri = new Uri(imageUri, UriKind.Absolute); string filename = System.IO.Path.GetFileName(iuri.LocalPath); //메모리 내용확인 var mbi = GetImage(filename); if (mbi != null) { return(mbi); } Stream imageStream = null; //기본 스트림 //IRandomAccessStream InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream(); //create bitmap BitmapImage bi = new BitmapImage(); //폴더에 파일 존재 확인 if (files.Any(p => p.Name == filename)) { var localFile = files.First(p => p.Name == filename); bi.UriSource = new Uri(Path.Combine(ApplicationData.Current.TemporaryFolder.Path, localFile.Name)); AddImage(filename, bi); //try //{ // imageStream = await localFile.OpenStreamForReadAsync(); //} //catch (Exception) //{ // //파일 열때 에러가 발생하면, 파일이 존재하지 않기 때문일 수 있는.. // UpdateFolder(); // bi.UriSource = new Uri(imageUri); // if (imageStream != null) imageStream.Dispose(); // if (ras != null) ras.Dispose(); // return bi; //} //await imageStream.CopyToAsync(ras.AsStreamForWrite()); //if (ras.Size > 0) //{ // ras.Seek(0); // await bi.SetSourceAsync(ras); // //메모리에 저장 // AddImage(filename, bi); //} //else //{ // //파일 이상인듯 // await localFile.DeleteAsync(); // UpdateFolder(); // //재귀호출 // if (retry == false) // { // if (imageStream != null) imageStream.Dispose(); // if (ras != null) ras.Dispose(); // return await UriImageSaveLocalAsync(imageUri, true); // } // else // { // bi.UriSource = new Uri(imageUri); // if (imageStream != null) imageStream.Dispose(); // if (ras != null) ras.Dispose(); // return bi; // } //} } else { using (HttpClient hc = new HttpClient()) { try { imageStream = await hc.GetStreamAsync(imageUri); } catch (Exception) { //네트워크 상태가 끊어졌을 때 bi.UriSource = new Uri(imageUri); if (imageStream != null) { imageStream.Dispose(); } if (ras != null) { ras.Dispose(); } return(bi); } //Stream -> IRandomAccessStream await imageStream.CopyToAsync(ras.AsStreamForWrite()); if (ras.Size > 0) { try { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(ras); BitmapFrame frame = await decoder.GetFrameAsync(0); //파일로 저장 var bitmap = new WriteableBitmap((int)frame.PixelWidth, (int)frame.PixelHeight); ras.Seek(0); await bitmap.SetSourceAsync(ras); var saveImage = await bitmap.SaveToFile(ApplicationData.Current.TemporaryFolder, filename, CreationCollisionOption.OpenIfExists); UpdateFolder(); //UriSource로 이미지 넣어주고 bi.UriSource = new Uri(Path.Combine(ApplicationData.Current.TemporaryFolder.Path, saveImage.Name)); //이미지로 변환 //ras.Seek(0); //await bi.SetSourceAsync(ras); //메모리에 저장 AddImage(filename, bi); } catch (Exception) { //이미지가 너무 커서 저장할 수 없을 경우 그냥 이미지 uri를 넣어서 던짐 bi.UriSource = new Uri(imageUri); if (imageStream != null) { imageStream.Dispose(); } if (ras != null) { ras.Dispose(); } return(bi); } } else { //재귀호출 if (retry == false) { if (imageStream != null) { imageStream.Dispose(); } if (ras != null) { ras.Dispose(); } return(await UriImageSaveLocalAsync(imageUri, true)); } else { bi.UriSource = new Uri(imageUri); if (imageStream != null) { imageStream.Dispose(); } if (ras != null) { ras.Dispose(); } return(bi); } } } } if (imageStream != null) { imageStream.Dispose(); } if (ras != null) { ras.Dispose(); } return(bi); }
public static async Task <AnimatedImage> DecodeAnimatedImage(StorageFile source, CanvasBitmapFileFormat format) { string[] props = new string[] { "/grctlext/Delay" }; List <ImageFrame> frames = new List <ImageFrame>(); BitmapDecoder decoder = await BitmapDecoder.CreateAsync( BitmapDecoder.GifDecoderId, await source.OpenAsync(FileAccessMode.Read)); CanvasDevice device = CanvasDevice.GetSharedDevice(); CanvasRenderTarget renderTarget = new CanvasRenderTarget( device, decoder.PixelWidth, decoder.PixelHeight, 96); CanvasBitmap buffer; var count = decoder.FrameCount; TimeSpan delay; TimeSpan startTime = new TimeSpan(); Rect rect = new Rect(0, 0, decoder.PixelWidth, decoder.PixelHeight); // Перебираем кадры. for (uint frameIdx = 0; frameIdx < decoder.FrameCount; frameIdx++) { var frame = await decoder.GetFrameAsync(frameIdx); // Получаем длительность кадра var propSet = await frame.BitmapProperties.GetPropertiesAsync(props); var prop = propSet.FirstOrDefault(); delay = TimeSpan.FromMilliseconds(10 * (UInt16)prop.Value.Value); if (delay.Ticks == 0) { delay = TimeSpan.FromMilliseconds(100); } var softwareBitmap = SoftwareBitmap.Convert( await frame.GetSoftwareBitmapAsync(), BitmapPixelFormat.Rgba16, BitmapAlphaMode.Premultiplied); // Создаем прадварительный буфер. buffer = CanvasBitmap.CreateFromSoftwareBitmap(device, softwareBitmap); using (var session = renderTarget.CreateDrawingSession()) { session.DrawImage(buffer, rect); } using (var stream = new InMemoryRandomAccessStream()) { await renderTarget.SaveAsync(stream, format); BitmapImage bitmap = new BitmapImage(); bitmap.SetSource(stream); ImageFrame decodedFrame = new ImageFrame() { Duration = delay, StartTime = startTime, ImageSource = SoftwareBitmap.Convert( await SoftwareBitmap.CreateCopyFromSurfaceAsync(renderTarget), BitmapPixelFormat.Rgba16, BitmapAlphaMode.Premultiplied) }; frames.Add(decodedFrame); startTime += delay; } } return(new AnimatedImage(frames, startTime) { Width = decoder.PixelWidth, Height = decoder.PixelHeight }); }
public async Task TestExtractVideoFrameAsync() { Uri uri = new Uri(Constants.DownloadUriSource); Assert.IsNotNull(uri); StorageFile file = await StorageFile.CreateStreamedFileFromUriAsync(Constants.DownloadStreamedFileName, uri, null); Assert.IsNotNull(file); IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read); Assert.IsNotNull(readStream); // CreateFromStreamAsync should return valid FFmpegInteropMSS object which generates valid MediaStreamSource object var frameGrabber = await FrameGrabber.CreateFromStreamAsync(readStream); var frame = await frameGrabber.ExtractVideoFrameAsync(TimeSpan.Zero); Assert.IsNotNull(frame); using (var stream = new InMemoryRandomAccessStream()) { // encode as jpeg await frame.EncodeAsJpegAsync(stream); stream.Seek(0); BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); var bitmap = await decoder.GetFrameAsync(0); Assert.IsNotNull(bitmap); // encode as png stream.Seek(0); stream.Size = 0; await frame.EncodeAsPngAsync(stream); stream.Seek(0); decoder = await BitmapDecoder.CreateAsync(stream); bitmap = await decoder.GetFrameAsync(0); Assert.IsNotNull(bitmap); // encode as bmp stream.Seek(0); stream.Size = 0; await frame.EncodeAsBmpAsync(stream); stream.Seek(0); decoder = await BitmapDecoder.CreateAsync(stream); bitmap = await decoder.GetFrameAsync(0); Assert.IsNotNull(bitmap); } }
async void OnOpenAppBarButtonClick(object sender, RoutedEventArgs args) { // Create FileOpenPicker FileOpenPicker picker = new FileOpenPicker(); picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; // Initialize with filename extensions IReadOnlyList <BitmapCodecInformation> codecInfos = BitmapDecoder.GetDecoderInformationEnumerator(); foreach (BitmapCodecInformation codecInfo in codecInfos) { foreach (string extension in codecInfo.FileExtensions) { picker.FileTypeFilter.Add(extension); } } // Get the selected file StorageFile storageFile = await picker.PickSingleFileAsync(); if (storageFile == null) { return; } // Open the stream and create a decoder BitmapDecoder decoder = null; using (IRandomAccessStreamWithContentType stream = await storageFile.OpenReadAsync()) { string exception = null; try { decoder = await BitmapDecoder.CreateAsync(stream); } catch (Exception exc) { exception = exc.Message; } if (exception != null) { MessageDialog msgdlg = new MessageDialog("That particular image file could not be loaded. " + "The system reports on error of: " + exception); await msgdlg.ShowAsync(); return; } // Get the first frame BitmapFrame bitmapFrame = await decoder.GetFrameAsync(0); // Set information title txtblk.Text = String.Format("{0}: {1} x {2} {3} {4} x {5} DPI", storageFile.Name, bitmapFrame.PixelWidth, bitmapFrame.PixelHeight, bitmapFrame.BitmapPixelFormat, bitmapFrame.DpiX, bitmapFrame.DpiY); // Save the resolution dpiX = bitmapFrame.DpiX; dpiY = bitmapFrame.DpiY; // Get the pixels PixelDataProvider dataProvider = await bitmapFrame.GetPixelDataAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, new BitmapTransform(), ExifOrientationMode.RespectExifOrientation, ColorManagementMode.ColorManageToSRgb); byte[] pixels = dataProvider.DetachPixelData(); // Create WriteableBitmap and set the pixels WriteableBitmap bitmap = new WriteableBitmap((int)bitmapFrame.PixelWidth, (int)bitmapFrame.PixelHeight); using (Stream pixelStream = bitmap.PixelBuffer.AsStream()) { await pixelStream.WriteAsync(pixels, 0, pixels.Length); } // Invalidate the WriteableBitmap and set as Image source bitmap.Invalidate(); image.Source = bitmap; } // Enable the other buttons saveAsButton.IsEnabled = true; rotateLeftButton.IsEnabled = true; rotateRightButton.IsEnabled = true; }
async Task CreateCollage(IReadOnlyList <StorageFile> files) { progressIndicator.Visibility = Windows.UI.Xaml.Visibility.Visible; var sampleDataGroups = files; if (sampleDataGroups.Count() == 0) { return; } try { // Do a square-root of the number of images to get the // number of images on x and y axis int number = (int)Math.Ceiling(Math.Sqrt((double)files.Count)); // Calculate the width of each small image in the collage int numberX = (int)(ImageCollage.ActualWidth / number); int numberY = (int)(ImageCollage.ActualHeight / number); // Initialize an empty WriteableBitmap. WriteableBitmap destination = new WriteableBitmap(numberX * number, numberY * number); int col = 0; // Current Column Position int row = 0; // Current Row Position destination.Clear(Colors.White); // Set the background color of the image to white WriteableBitmap bitmap; // Temporary bitmap into which the source // will be copied foreach (var file in files) { // Create RandomAccessStream reference from the current selected image RandomAccessStreamReference streamRef = RandomAccessStreamReference.CreateFromFile(file); int wid = 0; int hgt = 0; byte[] srcPixels; // Read the image file into a RandomAccessStream using (IRandomAccessStreamWithContentType fileStream = await streamRef.OpenReadAsync()) { // Now that you have the raw bytes, create a Image Decoder BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream); // Get the first frame from the decoder because we are picking an image BitmapFrame frame = await decoder.GetFrameAsync(0); // Convert the frame into pixels PixelDataProvider pixelProvider = await frame.GetPixelDataAsync(); // Convert pixels into byte array srcPixels = pixelProvider.DetachPixelData(); wid = (int)frame.PixelWidth; hgt = (int)frame.PixelHeight; // Create an in memory WriteableBitmap of the same size bitmap = new WriteableBitmap(wid, hgt); Stream pixelStream = bitmap.PixelBuffer.AsStream(); pixelStream.Seek(0, SeekOrigin.Begin); // Push the pixels from the original file into the in-memory bitmap pixelStream.Write(srcPixels, 0, (int)srcPixels.Length); bitmap.Invalidate(); if (row < number) { // Resize the in-memory bitmap and Blit (paste) it at the correct tile // position (row, col) destination.Blit(new Rect(col * numberX, row * numberY, numberX, numberY), bitmap.Resize(numberX, numberY, WriteableBitmapExtensions.Interpolation.Bilinear), new Rect(0, 0, numberX, numberY)); col++; if (col >= number) { row++; col = 0; } } } } ImageCollage.Source = destination; ((WriteableBitmap)ImageCollage.Source).Invalidate(); progressIndicator.Visibility = Windows.UI.Xaml.Visibility.Collapsed; } catch (Exception ex) { // TODO: Log Error, unable to render image throw; } }