1 using System; 2 using System.Threading.Tasks; 3 using System.Runtime.InteropServices.WindowsRuntime; 4 using Windows.Graphics.Imaging; 5 using Windows.UI.Xaml.Media.Imaging; 6 using Windows.Storage; 7 using Windows.Storage.Pickers; 8 using Windows.Storage.Streams; 9 using System.IO; 10 using Windows.Foundation; 11 using System.Collections.Generic; 12 13 namespace Common 14 { 15 public class ImageHelper 16 { 17 public const int THumbLength = 400; 18 public static Size MinSizeSupported = new Size(200, 200); 19 public static Size MaxSizeSupported = new Size(4000, 3000); 20 21 static Guid EncoderIDFromFileExtension(string strExtension) 22 { 23 Guid encoderId; 24 switch (strExtension.ToLower()) 25 { 26 case ".jpg": 27 case ".jpeg": 28 encoderId = BitmapEncoder.JpegEncoderId; 29 break; 30 case ".bmp": 31 encoderId = BitmapEncoder.BmpEncoderId; 32 break; 33 case ".png": 34 default: 35 encoderId = BitmapEncoder.PngEncoderId; 36 break; 37 } 38 return encoderId; 39 } 40 static Guid DecoderIDFromFileExtension(string strExtension) 41 { 42 Guid encoderId; 43 switch (strExtension.ToLower()) 44 { 45 case ".jpg": 46 case ".jpeg": 47 encoderId = BitmapDecoder.JpegDecoderId; 48 break; 49 case ".bmp": 50 encoderId = BitmapDecoder.BmpDecoderId; 51 break; 52 case ".png": 53 default: 54 encoderId = BitmapDecoder.PngDecoderId; 55 break; 56 } 57 return encoderId; 58 } 59 60 public async static Task<WriteableBitmap> ReadBitmap(IRandomAccessStream fileStream, string type) 61 { 62 WriteableBitmap bitmap = null; 63 try 64 { 65 Guid decoderId = DecoderIDFromFileExtension(type); 66 67 BitmapDecoder decoder = await BitmapDecoder.CreateAsync(decoderId, fileStream); 68 BitmapTransform tf = new BitmapTransform(); 69 70 uint width = decoder.OrientedPixelWidth; 71 uint height = decoder.OrientedPixelHeight; 72 double dScale = 1; 73 74 if (decoder.OrientedPixelWidth > MaxSizeSupported.Width || decoder.OrientedPixelHeight > MaxSizeSupported.Height) 75 { 76 dScale = Math.Min(MaxSizeSupported.Width / decoder.OrientedPixelWidth, MaxSizeSupported.Height/decoder.OrientedPixelHeight ); 77 width =(uint)( decoder.OrientedPixelWidth * dScale); 78 height = (uint)(decoder.OrientedPixelHeight * dScale); 79 80 tf.ScaledWidth = (uint)(decoder.PixelWidth* dScale); 81 tf.ScaledHeight = (uint)(decoder.PixelHeight* dScale); 82 } 83 84 85 bitmap = new WriteableBitmap((int)width, (int)height); 86 87 88 89 PixelDataProvider dataprovider = await decoder.GetPixelDataAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, tf, 90 ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage); 91 byte[] pixels = dataprovider.DetachPixelData(); 92 93 Stream pixelStream2 = bitmap.PixelBuffer.AsStream(); 94 95 pixelStream2.Write(pixels, 0, pixels.Length); 96 //bitmap.SetSource(fileStream); 97 } 98 catch 99 { 100 } 101 102 return bitmap; 103 } 104 105 public async static Task<WriteableBitmap> ReadBitmap(StorageFile file) 106 { 107 IRandomAccessStream stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read); 108 WriteableBitmap bitmap = await ReadBitmap(stream, file.FileType.ToLower()); 109 110 return bitmap; 111 } 112 113 114 public async static Task<bool> SaveBitmap(IRandomAccessStream stream, Guid encoderId, WriteableBitmap bitmap, BitmapTransform bitmapTransform) 115 { 116 bool bSuccess = true; 117 try 118 { 119 Stream pixelStream = bitmap.PixelBuffer.AsStream(); 120 121 BitmapEncoder encoder = await BitmapEncoder.CreateAsync(encoderId, stream); 122 if (bitmapTransform != null) 123 { 124 encoder.BitmapTransform.ScaledWidth = bitmapTransform.ScaledWidth; 125 encoder.BitmapTransform.ScaledHeight = bitmapTransform.ScaledHeight; 126 encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Fant; 127 encoder.BitmapTransform.Bounds = bitmapTransform.Bounds; 128 } 129 130 byte[] pixels = new byte[pixelStream.Length]; 131 132 pixelStream.Read(pixels, 0, pixels.Length); 133 134 encoder.SetPixelData( 135 BitmapPixelFormat.Bgra8, 136 BitmapAlphaMode.Straight, 137 (uint)bitmap.PixelWidth, 138 (uint)bitmap.PixelHeight, 139 96, // Horizontal DPI 140 96, // Vertical DPI 141 pixels 142 ); 143 await encoder.FlushAsync(); 144 145 pixelStream.Dispose(); 146 } 147 catch 148 { 149 bSuccess = false; 150 } 151 152 return bSuccess; 153 } 154 155 async static Task<bool> SaveBitmap(StorageFile file, WriteableBitmap bitmap, BitmapTransform bitmapTransform) 156 { 157 bool bSuccess = true; 158 try 159 { 160 // Application now has read/write access to the saved file 161 Guid encoderId = EncoderIDFromFileExtension(file.FileType); 162 163 164 IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite); 165 166 bSuccess = await SaveBitmap(fileStream, encoderId,bitmap,bitmapTransform); 167 168 fileStream.Dispose(); 169 170 } 171 catch 172 { 173 bSuccess = false; 174 } 175 176 return bSuccess; 177 } 178 179 public async static Task<bool> SaveBitmap(StorageFile file, WriteableBitmap bitmap) 180 { 181 bool bSuccess = await SaveBitmap(file, bitmap, null); 182 return bSuccess; 183 } 184 185 public async static Task<bool> SaveBitmapWithFilePicker(WriteableBitmap bitmap) 186 { 187 FileSavePicker save = new FileSavePicker(); 188 save.SuggestedStartLocation = PickerLocationId.PicturesLibrary; 189 save.DefaultFileExtension = ".jpg"; 190 save.SuggestedFileName = "new image"; 191 save.FileTypeChoices.Add(".bmp", new List<string>() { ".bmp" }); 192 save.FileTypeChoices.Add(".png", new List<string>() { ".png" }); 193 save.FileTypeChoices.Add(".jpg", new List<string>() { ".jpg", ".jpeg" }); 194 195 StorageFile savedItem = await save.PickSaveFileAsync(); 196 if (savedItem == null) 197 { 198 return false; 199 } 200 201 return await SaveBitmap(savedItem, bitmap); 202 } 203 204 public async static Task<bool> SaveBitmapAsThumb(StorageFile file, WriteableBitmap bitmap, double width, double height) 205 { 206 BitmapTransform transform = new BitmapTransform(); 207 208 double ratio = Math.Min(width / bitmap.PixelWidth, height / bitmap.PixelHeight); 209 210 transform.ScaledWidth = (uint)(bitmap.PixelWidth * ratio); 211 transform.ScaledHeight = (uint)(bitmap.PixelHeight * ratio); 212 transform.InterpolationMode = BitmapInterpolationMode.Fant; 213 214 return await SaveBitmap(file, bitmap, transform); 215 } 216 public async static Task<bool> SaveBitmapAsThumb(StorageFile file, WriteableBitmap bitmap, Windows.Foundation.Rect cropRect) 217 { 218 BitmapTransform transform = new BitmapTransform(); 219 220 221 double ratio =THumbLength/ Math.Max(cropRect.Width, cropRect.Height ); 222 223 transform.ScaledWidth = (uint)(bitmap.PixelWidth * ratio); 224 transform.ScaledHeight = (uint)(bitmap.PixelHeight * ratio); 225 transform.Bounds = new BitmapBounds() 226 { 227 X = (uint)(cropRect.X * ratio), 228 Y = (uint)(cropRect.Y * ratio), 229 Width = (uint)(cropRect.Width * ratio), 230 Height = (uint)(cropRect.Height * ratio) 231 }; 232 transform.InterpolationMode = BitmapInterpolationMode.Fant; 233 234 return await SaveBitmap(file, bitmap, transform); 235 } 236 } 237 }
windows store app 读写图片,布布扣,bubuko.com
原文:http://www.cnblogs.com/Mengyl/p/3741732.html