Metro style app
I. Image scale and crop operations
public class PhotoEdit { public async static Task<WriteableBitmap> ScaleAndCorpPhoto(IStorageFile file) { if (file == null) return null; // create a stream from the file and decode the image var fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read); BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream); BitmapTransform transform = new BitmapTransform(); BitmapBounds bounds = new BitmapBounds(); transform.ScaledWidth = 500; transform.ScaledHeight = 500; const int croppedHeight = 400; const int croppedWidth = 400; bounds.Height = croppedHeight; bounds.Width = croppedWidth; bounds.X = 0; bounds.Y = 0; transform.Bounds = bounds; // Get the pixels in Bgra8 to match what the WriteableBitmap will expect PixelDataProvider pixelData = await decoder.GetPixelDataAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.ColorManageToSRgb); byte[] pixels = pixelData.DetachPixelData(); // And stream them into a WriteableBitmap WriteableBitmap cropBmp = new WriteableBitmap(croppedHeight, croppedWidth); Stream pixStream = cropBmp.PixelBuffer.AsStream(); pixStream.Write(pixels, 0, pixels.Length); return cropBmp; } }
Call Method
Private async system. threading. tasks. task scaleandcorpphoto () {fileopenpicker = new fileopenpicker (); fileopenpicker. commitbuttontext = "open"; fileopenpicker. filetypefilter. insert (0 ,". jpg "); storagefile file = await fileopenpicker. picksinglefileasync (); If (file = NULL) return; image. source = await photoedit. scaleandcorpphoto (File );}
Ii. Opening and saving images
1. Open the image. Select the image to be displayed and display the image in the image control.
private static BitmapImage srcImage = new BitmapImage(); private static WriteableBitmap wbsrcImage; public async static void OpenImage(Image ImageOne) { FileOpenPicker imagePicker = new FileOpenPicker { ViewMode = PickerViewMode.Thumbnail, SuggestedStartLocation = PickerLocationId.PicturesLibrary, FileTypeFilter = { ".jpg", ".jpeg", ".png", ".bmp" } }; Guid decoderId; StorageFile imageFile = await imagePicker.PickSingleFileAsync(); if (imageFile != null) { srcImage = new BitmapImage(); using (IRandomAccessStream stream = await imageFile.OpenAsync(FileAccessMode.Read)) { srcImage.SetSource(stream); switch (imageFile.FileType.ToLower()) { case ".jpg": case ".jpeg": decoderId = Windows.Graphics.Imaging.BitmapDecoder.JpegDecoderId; break; case ".bmp": decoderId = Windows.Graphics.Imaging.BitmapDecoder.BmpDecoderId; break; case ".png": decoderId = Windows.Graphics.Imaging.BitmapDecoder.PngDecoderId; break; default: return; } Windows.Graphics.Imaging.BitmapDecoder decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(decoderId, stream); int width = (int)decoder.PixelWidth; int height = (int)decoder.PixelHeight; Windows.Graphics.Imaging.PixelDataProvider dataprovider = await decoder.GetPixelDataAsync(); byte[] pixels = dataprovider.DetachPixelData(); wbsrcImage = new WriteableBitmap(width, height); Stream pixelStream = wbsrcImage.PixelBuffer.AsStream(); //rgba in original //to display ,convert tobgra for (int i = 0; i < pixels.Length; i += 4) { byte temp = pixels[i]; pixels[i] = pixels[i + 2]; pixels[i + 2] = temp; } pixelStream.Write(pixels, 0, pixels.Length); pixelStream.Dispose(); stream.Dispose(); } ImageOne.Source = wbsrcImage; ImageOne.Width = wbsrcImage.PixelWidth; ImageOne.Height = wbsrcImage.PixelHeight; } }
2. Save the image. Save the obtained writeablebitmap. For example, you can save the scale and crop images.
public async static Task SaveImage(WriteableBitmap src) { FileSavePicker save = new FileSavePicker(); save.SuggestedStartLocation = PickerLocationId.PicturesLibrary; save.DefaultFileExtension = ".jpg"; save.SuggestedFileName = "newimage"; save.FileTypeChoices.Add(".bmp", new List<string>() { ".bmp" }); save.FileTypeChoices.Add(".png", new List<string>() { ".png" }); save.FileTypeChoices.Add(".jpg", new List<string>() { ".jpg", ".jpeg" }); StorageFile savedItem = await save.PickSaveFileAsync(); try { Guid encoderId; switch (savedItem.FileType.ToLower()) { case ".jpg": encoderId = BitmapEncoder.JpegEncoderId; break; case ".bmp": encoderId = BitmapEncoder.BmpEncoderId; break; case ".png": default: encoderId = BitmapEncoder.PngEncoderId; break; } IRandomAccessStream fileStream = await savedItem.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite); BitmapEncoder encoder = await BitmapEncoder.CreateAsync(encoderId, fileStream); Stream pixelStream = src.PixelBuffer.AsStream(); byte[] pixels = new byte[pixelStream.Length]; pixelStream.Read(pixels, 0, pixels.Length); //pixal format shouldconvert to rgba8 for (int i = 0; i < pixels.Length; i += 4) { byte temp = pixels[i]; pixels[i] = pixels[i + 2]; pixels[i + 2] = temp; } encoder.SetPixelData( BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, (uint)src.PixelWidth, (uint)src.PixelHeight, 96, // Horizontal DPI 96, // Vertical DPI pixels ); await encoder.FlushAsync(); } catch (Exception e) { throw e; } }