public static async Task ResizeImageUniformToFillAsync(StorageFile sourceFile, StorageFile targetFile, int maxWidth = Int32.MaxValue, int maxHeight = Int32.MaxValue) { Size finalSize; byte[] pixels = null; using (var stream = await sourceFile.OpenReadAsync()) { var decoder = await BitmapDecoder.CreateAsync(stream); maxWidth = Math.Min(maxWidth, (int)decoder.OrientedPixelWidth); maxHeight = Math.Min(maxHeight, (int)decoder.OrientedPixelHeight); var imageSize = new Size(decoder.OrientedPixelWidth, decoder.OrientedPixelHeight); finalSize = imageSize.ToUniformToFill(new Size(maxWidth, maxHeight)); var bitmapTransform = new BitmapTransform() { ScaledWidth = (uint)finalSize.Width, ScaledHeight = (uint)finalSize.Height, InterpolationMode = BitmapInterpolationMode.Fant }; var pixelProvider = await decoder.GetPixelDataAsync(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Ignore, bitmapTransform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage); pixels = pixelProvider.DetachPixelData(); } using (var fileStream = await targetFile.OpenAsync(FileAccessMode.ReadWrite)) { var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, fileStream); encoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Ignore, (uint)finalSize.Width, (uint)finalSize.Height, 96, 96, pixels); await encoder.FlushAsync(); } }
private async Task <Image> createWorkableImage(StorageFile image) { var copyBitmapImage = await this.makeACopyOfTheFileToWorkOn(image); using (var fileStream = await image.OpenAsync(FileAccessMode.Read)) { var decoder = await BitmapDecoder.CreateAsync(fileStream); var transform = new BitmapTransform { ScaledWidth = Convert.ToUInt32(copyBitmapImage.PixelWidth), ScaledHeight = Convert.ToUInt32(copyBitmapImage.PixelHeight) }; var pixelData = await decoder.GetPixelDataAsync( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, transform, ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage ); var sourcePixels = pixelData.DetachPixelData(); var thumbnail = await this.makeACopyOfTheFileToWorkOn(image); return(new Image(sourcePixels, decoder, thumbnail)); } }
public async Task <byte[]> GetScaledPixelsAsync(int height, int width) { using (IRandomAccessStream fileStream = await m_file.OpenAsync(Windows.Storage.FileAccessMode.Read)) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream); // Scale image to appropriate size BitmapTransform transform = new BitmapTransform() { ScaledWidth = Convert.ToUInt32(width), ScaledHeight = Convert.ToUInt32(height) }; PixelDataProvider pixelData = await decoder.GetPixelDataAsync( BitmapPixelFormat.Bgra8, // WriteableBitmap uses BGRA format BitmapAlphaMode.Straight, transform, ExifOrientationMode.IgnoreExifOrientation, // This sample ignores Exif orientation ColorManagementMode.DoNotColorManage ); // An array containing the decoded image data, which could be modified before being displayed byte[] sourcePixels = pixelData.DetachPixelData(); return(sourcePixels); } }
private static async Task <WriteableBitmap> GetImageFile(Uri fileUri) { StorageFile imageFile = await StorageFile.GetFileFromApplicationUriAsync(fileUri); WriteableBitmap writeableBitmap = null; using (IRandomAccessStream imageStream = await imageFile.OpenReadAsync()) { BitmapDecoder bitmapDecoder = await BitmapDecoder.CreateAsync(imageStream); BitmapTransform dummyTransform = new BitmapTransform(); PixelDataProvider pixelDataProvider = await bitmapDecoder.GetPixelDataAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, dummyTransform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.ColorManageToSRgb); byte[] pixelData = pixelDataProvider.DetachPixelData(); writeableBitmap = new WriteableBitmap( (int)bitmapDecoder.OrientedPixelWidth, (int)bitmapDecoder.OrientedPixelHeight); using (Stream pixelStream = writeableBitmap.PixelBuffer.AsStream()) { await pixelStream.WriteAsync(pixelData, 0, pixelData.Length); } } return(writeableBitmap); }
private static async Task Resize(StorageFile source, StorageFile destination, uint height, uint width) { using (var _Source = await source.OpenAsync(FileAccessMode.Read)) { var _BitmapDecoder = await BitmapDecoder.CreateAsync(_Source); var _BitmapTransform = new BitmapTransform() { ScaledHeight = height, ScaledWidth = width }; var _PixelData = await _BitmapDecoder.GetPixelDataAsync( BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, _BitmapTransform, ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage); using (var _Target = await destination.OpenAsync(FileAccessMode.ReadWrite)) { var _BitmapEncoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, _Target); _BitmapEncoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Premultiplied, height, width, 96, 96, _PixelData.DetachPixelData()); await _BitmapEncoder.FlushAsync(); } } }
public async Task <IBitmap> Load(Stream sourceStream, float?desiredWidth, float?desiredHeight) { return(await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(async() => { using (var rwStream = new InMemoryRandomAccessStream()) { var writer = rwStream.AsStreamForWrite(); await sourceStream.CopyToAsync(writer); await writer.FlushAsync(); rwStream.Seek(0); var decoder = await BitmapDecoder.CreateAsync(rwStream); var transform = new BitmapTransform { ScaledWidth = (uint)(desiredWidth ?? decoder.OrientedPixelWidth), ScaledHeight = (uint)(desiredHeight ?? decoder.OrientedPixelHeight), InterpolationMode = BitmapInterpolationMode.Fant }; var pixelData = await decoder.GetPixelDataAsync(decoder.BitmapPixelFormat, BitmapAlphaMode.Premultiplied, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.ColorManageToSRgb); var pixels = pixelData.DetachPixelData(); var bmp = new WriteableBitmap((int)transform.ScaledWidth, (int)transform.ScaledHeight); using (var bmpStream = bmp.PixelBuffer.AsStream()) { bmpStream.Seek(0, SeekOrigin.Begin); bmpStream.Write(pixels, 0, (int)bmpStream.Length); return (IBitmap) new WriteableBitmapImageBitmap(bmp); } } })); }
private async Task <IRandomAccessStream> ResizeJpegStreamAsync(int maxPixelDimension, int percentQuality, IRandomAccessStream input) { var decoder = await BitmapDecoder.CreateAsync(input); int targetHeight; int targetWidth; MvxPictureDimensionHelper.TargetWidthAndHeight(maxPixelDimension, (int)decoder.PixelWidth, (int)decoder.PixelHeight, out targetWidth, out targetHeight); var transform = new BitmapTransform() { ScaledHeight = (uint)targetHeight, ScaledWidth = (uint)targetWidth }; var pixelData = await decoder.GetPixelDataAsync( BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage); var destinationStream = new InMemoryRandomAccessStream(); var bitmapPropertiesSet = new BitmapPropertySet(); bitmapPropertiesSet.Add("ImageQuality", new BitmapTypedValue(((double)percentQuality) / 100.0, PropertyType.Single)); var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, destinationStream, bitmapPropertiesSet); encoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Premultiplied, (uint)targetWidth, (uint)targetHeight, decoder.DpiX, decoder.DpiY, pixelData.DetachPixelData()); await encoder.FlushAsync(); destinationStream.Seek(0L); return(destinationStream); }
public async Task <byte[]> ReadPixels(StorageFile file) { var copyBitmapImage = await this.MakeACopyOfTheFileToWorkOn(file); using (var fileStream = await file.OpenAsync(FileAccessMode.Read)) { var decoder = await BitmapDecoder.CreateAsync(fileStream); var transform = new BitmapTransform { ScaledWidth = Convert.ToUInt32(copyBitmapImage.PixelWidth), ScaledHeight = Convert.ToUInt32(copyBitmapImage.PixelHeight) }; var pixelData = await decoder.GetPixelDataAsync( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, transform, ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage ); return(pixelData.DetachPixelData()); } }
private async Task <byte[]> ImageToBytes(IRandomAccessStream sourceStream) { byte[] imageArray; BitmapDecoder decoder = await BitmapDecoder.CreateAsync(sourceStream); var transform = new BitmapTransform { ScaledWidth = decoder.PixelWidth, ScaledHeight = decoder.PixelHeight }; PixelDataProvider pixelData = await decoder.GetPixelDataAsync( BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage); using (var destinationStream = new InMemoryRandomAccessStream()) { BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, destinationStream); encoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Premultiplied, decoder.PixelWidth, decoder.PixelHeight, 96, 96, pixelData.DetachPixelData()); await encoder.FlushAsync(); BitmapDecoder outputDecoder = await BitmapDecoder.CreateAsync(destinationStream); await destinationStream.FlushAsync(); imageArray = (await outputDecoder.GetPixelDataAsync()).DetachPixelData(); } return(imageArray); }
public static async Task <Image> Convert(StorageFile imageFile, Image originalImage) { var copyBitmapImage = await MakeACopyOfTheFileToWorkOn(imageFile); using (var fileStream = await imageFile.OpenAsync(FileAccessMode.Read)) { var decoder = await BitmapDecoder.CreateAsync(fileStream); var transform = new BitmapTransform { ScaledWidth = System.Convert.ToUInt32(copyBitmapImage.PixelWidth), ScaledHeight = System.Convert.ToUInt32(copyBitmapImage.PixelHeight) }; var pixelData = await decoder.GetPixelDataAsync( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, transform, ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage ); var sourcePixels = pixelData.DetachPixelData(); var bitmap = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight); var convertedImage = await Convert(sourcePixels, originalImage, bitmap); return(convertedImage); } }
/// <summary> /// Resizes the specified stream. /// </summary> /// <param name="sourceStream">The source stream to resize.</param> /// <param name="newWidth">The width of the resized image.</param> /// <param name="newHeight">The height of the resized image.</param> /// <returns>The resized image stream.</returns> public static async Task <InMemoryRandomAccessStream> Resize(IRandomAccessStream sourceStream, uint newWidth, uint newHeight) { var destinationStream = new InMemoryRandomAccessStream(); var decoder = await BitmapDecoder.CreateAsync(sourceStream); var transform = new BitmapTransform { ScaledWidth = newWidth, ScaledHeight = newHeight }; var pixelData = await decoder.GetPixelDataAsync( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage); var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, destinationStream); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, newWidth, newHeight, 96, 96, pixelData.DetachPixelData()); await encoder.FlushAsync(); return(destinationStream); }
private async Task <WriteableBitmap> ReadAsync() { // 파일로부터 IRandomAccessStream를 가져오고 using (IRandomAccessStream stream = await _file.OpenAsync(FileAccessMode.ReadWrite)) { //BitmapDecoder로 이미지를 가져온다 BitmapDecoder decoder = await BitmapDecoder.CreateAsync(BitmapDecoder.JpegDecoderId, stream); uint width = decoder.PixelWidth; uint height = decoder.PixelHeight; if (_angle % 180 != 0) { width = decoder.PixelHeight; height = decoder.PixelWidth; } // 그런 다음 사진을 조작하여 변형을 반영한다. BitmapTransform transform = new BitmapTransform { Rotation = rotation_angles[_angle] }; // PixelDataProvider를 이용하여 사진을 회전 PixelDataProvider data = await decoder.GetPixelDataAsync( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, transform, ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage); _bitmap = new WriteableBitmap((int)width, (int)height); // 이후 정보를 파일에 다시 기록하여 회전된 이미지를 생성 byte[] buffer = data.DetachPixelData(); using (Stream pixels = _bitmap.PixelBuffer.AsStream()) { pixels.Write(buffer, 0, (int)pixels.Length); } } return(_bitmap); }
private async Task ResizeToJpegPhoto(IRandomAccessStream inputStream, IRandomAccessStream outputStream, uint maxSize) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(inputStream); double scaleFactor = 1.0; uint pixelSize = decoder.PixelWidth > decoder.PixelHeight ? decoder.PixelWidth : decoder.PixelHeight; if (pixelSize > maxSize) { scaleFactor = (double)maxSize / pixelSize; } BitmapTransform transform = new BitmapTransform(); transform.ScaledWidth = (uint)(decoder.PixelWidth * scaleFactor); transform.ScaledHeight = (uint)(decoder.PixelHeight * scaleFactor); transform.InterpolationMode = BitmapInterpolationMode.Fant; BitmapPixelFormat pixelFormat = decoder.BitmapPixelFormat; BitmapAlphaMode alphaMode = decoder.BitmapAlphaMode; PixelDataProvider pixelDataProvider = await decoder.GetPixelDataAsync(pixelFormat, alphaMode, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage); var pixels = pixelDataProvider.DetachPixelData(); uint finalWidth = (uint)(decoder.OrientedPixelWidth * scaleFactor); uint finalHeight = (uint)(decoder.OrientedPixelHeight * scaleFactor); BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, outputStream); encoder.SetPixelData(pixelFormat, alphaMode, finalWidth, finalHeight, decoder.DpiX, decoder.DpiY, pixels); await encoder.FlushAsync(); }
public static async Task <Color> GetDominantColor(StorageFile file) { using (var stream = await file.OpenAsync(FileAccessMode.Read)) { //Create a decoder for the image var decoder = await BitmapDecoder.CreateAsync(stream); //Create a transform to get a 1x1 image var myTransform = new BitmapTransform { ScaledHeight = 1, ScaledWidth = 1 }; //Get the pixel provider var pixels = await decoder.GetPixelDataAsync( BitmapPixelFormat.Rgba8, BitmapAlphaMode.Ignore, myTransform, ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage); //Get the bytes of the 1x1 scaled image var bytes = pixels.DetachPixelData(); //read the color return(Color.FromArgb(255, bytes[0], bytes[1], bytes[2])); } }
public static async Task <SoftwareBitmapSource> ToSoftwareBitmapSourceAsync(StorageFile input) { using (IRandomAccessStream fileStream = await input.OpenAsync(FileAccessMode.Read)) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream); BitmapTransform transform = new BitmapTransform(); const float sourceImageHeightLimit = 1280; if (decoder.PixelHeight > sourceImageHeightLimit) { float scalingFactor = (float)sourceImageHeightLimit / (float)decoder.PixelHeight; transform.ScaledWidth = (uint)Math.Floor(decoder.PixelWidth * scalingFactor); transform.ScaledHeight = (uint)Math.Floor(decoder.PixelHeight * scalingFactor); } SoftwareBitmap sourceBitmap = await decoder.GetSoftwareBitmapAsync(decoder.BitmapPixelFormat, BitmapAlphaMode.Premultiplied, transform, ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage); SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource(); await bitmapSource.SetBitmapAsync(sourceBitmap); return(bitmapSource); } }
private async Task ResizeImage(StorageFile sourceFile, StorageFile destinationFile) { using (var sourceStream = await sourceFile.OpenAsync(FileAccessMode.Read)) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(sourceStream); var newWidth = Math.Min(800, decoder.PixelWidth); var newHeight = newWidth * decoder.PixelHeight / decoder.PixelWidth; BitmapTransform transform = new BitmapTransform() { ScaledHeight = newHeight, ScaledWidth = newWidth }; PixelDataProvider pixelData = await decoder.GetPixelDataAsync( BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage); using (var destinationStream = await destinationFile.OpenAsync(FileAccessMode.ReadWrite)) { BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, destinationStream); encoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Premultiplied, newWidth, newHeight, 96, 96, pixelData.DetachPixelData()); await encoder.FlushAsync(); } } }
/// <summary> /// Resize the file to a certain level /// </summary> /// <param name="name">Final Name Of the Files Generated </param> /// <param name="hsize">Height Of Image</param> /// <param name="wsize">Width Of Image</param> /// <param name="file">The File That Needs To Be Resized</param> /// <param name="folder">Location of the final file</param> public async static Task Resizer(String name, uint hsize, uint wsize, StorageFile file, StorageFolder folder) { StorageFile newFile = await folder.CreateFileAsync(name); using (var sourceStream = await file.OpenAsync(FileAccessMode.Read)) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(sourceStream); BitmapTransform transform = new BitmapTransform() { ScaledHeight = hsize, ScaledWidth = wsize }; PixelDataProvider pixelData = await decoder.GetPixelDataAsync( BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage); using (var destinationStream = await newFile.OpenAsync(FileAccessMode.ReadWrite)) { BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, destinationStream); encoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Premultiplied, wsize, hsize, 100, 100, pixelData.DetachPixelData()); await encoder.FlushAsync(); } } }
public async void LoadBitmap(string name) { StorageFile srcfile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(name)); using (IRandomAccessStream fileStream = await srcfile.OpenAsync(Windows.Storage.FileAccessMode.Read)) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream); BitmapTransform transform = new BitmapTransform() { ScaledWidth = 8, ScaledHeight = 8 }; PixelDataProvider pixelData = await decoder.GetPixelDataAsync( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, transform, ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage ); byte[] sourcePixels = pixelData.DetachPixelData(); byte[] hatPixels = new byte[192]; for (int i = 0; i < (sourcePixels.Length / 4) / 8; i++) { MapToHat(hatPixels, i * 8 * 3, sourcePixels, i * 8 * 4); } WriteLEDMatrix(hatPixels); } }
public static async Task <string> SaveStreamAsync(IRandomAccessStream streamToSave, uint width, uint height, string fileName) { FolderPicker folderPicker = new FolderPicker(); folderPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; folderPicker.ViewMode = PickerViewMode.List; folderPicker.FileTypeFilter.Add(".jpg"); folderPicker.FileTypeFilter.Add(".jpeg"); folderPicker.FileTypeFilter.Add(".png"); StorageFolder newFolder = await folderPicker.PickSingleFolderAsync(); StorageFile destination = await newFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting); BitmapTransform transform = new BitmapTransform(); BitmapDecoder bmpDecoder = await BitmapDecoder.CreateAsync(streamToSave); PixelDataProvider pixelData = await bmpDecoder.GetPixelDataAsync(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage); using (var destFileStream = await destination.OpenAsync(FileAccessMode.ReadWrite)) { BitmapEncoder bmpEncoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, destFileStream); bmpEncoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, width, height, 300, 300, pixelData.DetachPixelData()); await bmpEncoder.FlushAsync(); } return(destination.Path); }
public static async Task <SoftwareBitmap> ConvertToSoftwareBitmapAsync(InMemoryRandomAccessStream stream) { var decoder = await BitmapDecoder.CreateAsync(stream); var transform = new BitmapTransform(); const float sourceImageHeightLimit = 1280; if (decoder.PixelHeight > sourceImageHeightLimit) { var scalingFactor = (float)sourceImageHeightLimit / (float)decoder.PixelHeight; transform.ScaledWidth = (uint)Math.Floor(decoder.PixelWidth * scalingFactor); transform.ScaledHeight = (uint)Math.Floor(decoder.PixelHeight * scalingFactor); } var sourceBitmap = await decoder.GetSoftwareBitmapAsync(decoder.BitmapPixelFormat, BitmapAlphaMode.Premultiplied, transform, ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage); const BitmapPixelFormat faceDetectionPixelFormat = BitmapPixelFormat.Gray8; SoftwareBitmap convertedBitmap; if (sourceBitmap.BitmapPixelFormat != faceDetectionPixelFormat) { convertedBitmap = SoftwareBitmap.Convert(sourceBitmap, faceDetectionPixelFormat); } else { convertedBitmap = sourceBitmap; } return(convertedBitmap); }
/// <summary> /// Reads in an source image, makes a copy and transforms it /// to a writable image and returns a Bitmap object. /// Precondition: sourceImageFile != null /// Post-condition: None /// </summary> /// <returns> /// A Bitmap object with now a writable bitmap image /// </returns> public static async Task <Bitmap> ReadBitmap(StorageFile sourceImageFile) { if (sourceImageFile == null) { throw new ArgumentNullException(nameof(sourceImageFile)); } var bitmapImage = await sourceImageFile.ToBitmapImageAsync(); using (var fileStream = await sourceImageFile.OpenAsync(FileAccessMode.Read)) { var decoder = await BitmapDecoder.CreateAsync(fileStream); var transform = new BitmapTransform { ScaledWidth = Convert.ToUInt32(bitmapImage.PixelWidth), ScaledHeight = Convert.ToUInt32(bitmapImage.PixelHeight) }; var pixelData = await decoder.GetPixelDataAsync( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, transform, ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage ); var sourcePixels = pixelData.DetachPixelData(); return(new Bitmap(sourcePixels, transform.ScaledWidth, (uint)decoder.DpiX, (uint)decoder.DpiY)); } }
// https://docs.microsoft.com/ja-jp/windows/uwp/audio-video-camera/detect-and-track-faces-in-an-image public static async Task <SoftwareBitmap> ResizeAsync2(this SoftwareBitmap source, float newWidth, float newHeight) { if (source == null) { return(null); } using (var memory = new InMemoryRandomAccessStream()) { var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, memory); encoder.SetSoftwareBitmap(source); await encoder.FlushAsync(); var decoder = await BitmapDecoder.CreateAsync(memory); var transform = new BitmapTransform() { ScaledHeight = (uint)newHeight, ScaledWidth = (uint)newWidth, InterpolationMode = BitmapInterpolationMode.Fant, }; var dest = await decoder.GetSoftwareBitmapAsync( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, transform, ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage ); return(dest); } }
/// <summary> /// Use BitmapTransform to define the region to crop, and then get the pixel data in the region. /// If you want to get the pixel data of a scaled image, set the scaledWidth and scaledHeight /// of the scaled image. /// </summary> async static private Task <byte[]> GetPixelData(BitmapDecoder decoder, uint startPointX, uint startPointY, uint width, uint height, uint scaledWidth, uint scaledHeight) { BitmapTransform transform = new BitmapTransform(); BitmapBounds bounds = new BitmapBounds(); bounds.X = startPointX; bounds.Y = startPointY; bounds.Height = height; bounds.Width = width; transform.Bounds = bounds; transform.ScaledWidth = scaledWidth; transform.ScaledHeight = scaledHeight; // Get the cropped pixels within the bounds of transform. PixelDataProvider pix = await decoder.GetPixelDataAsync( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, transform, ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.ColorManageToSRgb); byte[] pixels = pix.DetachPixelData(); return(pixels); }
private static async Task <SoftwareBitmapSource> CropPhoto(StorageFile photo, FaceRectangle rectangle) { using (var imageStream = await photo.OpenReadAsync()) { var decoder = await BitmapDecoder.CreateAsync(imageStream); if (decoder.PixelWidth >= rectangle.Left + rectangle.Width || decoder.PixelHeight >= rectangle.Top + rectangle.Height) { var transform = new BitmapTransform { Bounds = new BitmapBounds { X = (uint)rectangle.Left, Y = (uint)rectangle.Top, Height = (uint)rectangle.Height, Width = (uint)rectangle.Width } }; var softwareBitmapBGR8 = await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, transform, ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage); SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource(); await bitmapSource.SetBitmapAsync(softwareBitmapBGR8); return(bitmapSource); } return(null); } }
public static async Task <BitmapSource> CreateScaledBitmapFromStreamAsync(WebView web, IRandomAccessStream source) { int height = Convert.ToInt32(web.ActualHeight); int width = Convert.ToInt32(web.ActualWidth); WriteableBitmap bitmap = new WriteableBitmap(width, height); BitmapDecoder decoder = await BitmapDecoder.CreateAsync(source); BitmapTransform transform = new BitmapTransform(); transform.ScaledHeight = (uint)height; transform.ScaledWidth = (uint)width; PixelDataProvider pixelData = await decoder.GetPixelDataAsync( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage); pixelData.DetachPixelData().CopyTo(bitmap.PixelBuffer); var savefile = await ApplicationData.Current.LocalFolder.CreateFileAsync("inkSample", CreationCollisionOption.ReplaceExisting); using (IRandomAccessStream stream = await savefile.OpenAsync(FileAccessMode.ReadWrite)) { BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream); Stream pixelStream = bitmap.PixelBuffer.AsStream(); byte[] pixels = new byte[pixelStream.Length]; await pixelStream.ReadAsync(pixels, 0, pixels.Length); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight, 96.0, 96.0, pixels); await encoder.FlushAsync(); } return(bitmap); }
public static async Task ToArray(this StorageFile file, IOutputArray result, ImreadModes modes = ImreadModes.AnyColor | ImreadModes.AnyDepth) { using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read)) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream); Size s = new Size((int)decoder.PixelWidth, (int)decoder.PixelHeight); BitmapTransform transform = new BitmapTransform(); PixelDataProvider pixelData = await decoder.GetPixelDataAsync( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, transform, ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage); byte[] sourcePixels = pixelData.DetachPixelData(); GCHandle handle = GCHandle.Alloc(sourcePixels, GCHandleType.Pinned); using (Image <Bgra, Byte> img = new Image <Bgra, byte>(s.Width, s.Height, s.Width * 4, handle.AddrOfPinnedObject())) { if (modes.HasFlag(ImreadModes.Grayscale)) { CvInvoke.CvtColor(img, result, ColorConversion.Bgra2Gray); } else { CvInvoke.CvtColor(img, result, ColorConversion.Bgra2Bgr); } handle.Free(); } } }
async private static Task <byte[]> GetCroppedPixelsAsync(IRandomAccessStream stream, FaceRectangle rectangle) { // Create a decoder from the stream. With the decoder, we can get // the properties of the image. BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); // Create cropping BitmapTransform and define the bounds. BitmapTransform transform = new BitmapTransform(); BitmapBounds bounds = new BitmapBounds(); bounds.X = (uint)rectangle.Left; bounds.Y = (uint)rectangle.Top; bounds.Height = (uint)rectangle.Height; bounds.Width = (uint)rectangle.Width; transform.Bounds = bounds; // Get the cropped pixels within the bounds of transform. PixelDataProvider pix = await decoder.GetPixelDataAsync( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, transform, ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.ColorManageToSRgb); return(pix.DetachPixelData()); }
/// <summary> /// 读取照片流 转为WriteableBitmap给二维码解码器 /// </summary> /// <param name="fileStream"></param> /// <param name="type"></param> /// <returns></returns> public async static Task <WriteableBitmap> ReadBitmap(IRandomAccessStream fileStream, string type) { WriteableBitmap bitmap = null; try { Guid decoderId = DecoderIDFromFileExtension(type); BitmapDecoder decoder = await BitmapDecoder.CreateAsync(decoderId, fileStream); BitmapTransform tf = new BitmapTransform(); uint width = decoder.OrientedPixelWidth; uint height = decoder.OrientedPixelHeight; double dScale = 1; if (decoder.OrientedPixelWidth > MaxSizeSupported.Width || decoder.OrientedPixelHeight > MaxSizeSupported.Height) { dScale = Math.Min(MaxSizeSupported.Width / decoder.OrientedPixelWidth, MaxSizeSupported.Height / decoder.OrientedPixelHeight); width = (uint)(decoder.OrientedPixelWidth * dScale); height = (uint)(decoder.OrientedPixelHeight * dScale); tf.ScaledWidth = (uint)(decoder.PixelWidth * dScale); tf.ScaledHeight = (uint)(decoder.PixelHeight * dScale); } bitmap = new WriteableBitmap((int)width, (int)height); PixelDataProvider dataprovider = await decoder.GetPixelDataAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, tf, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage); byte[] pixels = dataprovider.DetachPixelData(); Stream pixelStream2 = bitmap.PixelBuffer.AsStream(); pixelStream2.Write(pixels, 0, pixels.Length); //bitmap.SetSource(fileStream); } catch { } return(bitmap); }
/// <summary> /// 识别照片流中的二维码 /// </summary> /// <param name="fileStream"></param> /// <param name="type"></param> /// <returns>识别到的文本,未识别到时返回 null。</returns> public async Task <string> ScanBitmap(IRandomAccessStream fileStream, string type) { var decoderId = DecoderIDFromFileExtension(type); var decoder = await BitmapDecoder.CreateAsync(decoderId, fileStream); var tf = new BitmapTransform(); var width = decoder.OrientedPixelWidth; var height = decoder.OrientedPixelHeight; if (decoder.OrientedPixelWidth > MaxSizeSupported.Width || decoder.OrientedPixelHeight > MaxSizeSupported.Height) { var dScale = Math.Min(MaxSizeSupported.Width / decoder.OrientedPixelWidth, MaxSizeSupported.Height / decoder.OrientedPixelHeight); width = (uint)(decoder.OrientedPixelWidth * dScale); height = (uint)(decoder.OrientedPixelHeight * dScale); tf.ScaledWidth = (uint)(decoder.PixelWidth * dScale); tf.ScaledHeight = (uint)(decoder.PixelHeight * dScale); } var bitmap = new WriteableBitmap((int)width, (int)height); var dataprovider = await decoder.GetPixelDataAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, tf, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage); var pixels = dataprovider.DetachPixelData(); var pixelStream2 = bitmap.PixelBuffer.AsStream(); pixelStream2.Write(pixels, 0, pixels.Length); return(barcodeReader.Decode(bitmap)?.Text); }
public static async Task <Color> GetDominantColorAsync(IRandomAccessStream stream) { //modified code from: http://www.jonathanantoine.com/2013/07/16/winrt-how-to-easily-get-the-dominant-color-of-a-picture/ //Create a decoder for the image var decoder = await BitmapDecoder.CreateAsync(stream); //Create a transform to get a 1x1 image var myTransform = new BitmapTransform { ScaledHeight = 1, ScaledWidth = 1 }; //Get the pixel provider var pixels = await decoder.GetPixelDataAsync( BitmapPixelFormat.Rgba8, BitmapAlphaMode.Ignore, myTransform, ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage); //Get the bytes of the 1x1 scaled image var bytes = pixels.DetachPixelData(); //read the color var myDominantColor = Color.FromArgb(255, bytes[0], bytes[1], bytes[2]); return(myDominantColor); }