积极答复者
BitmapDecoder与BitmapEncoder问题

问题
-
利用以下代码保存的图片始终不能打开,BitmapDecoder与BitmapEncoder的使用有问题么,请指教
private async void btnClick_Click_1(object sender, RoutedEventArgs e)
{
StorageFolder installedLocation = KnownFolders.PicturesLibrary;
StorageFile file = await installedLocation.GetFileAsync("Image.jpg");
ImageProperties props = await file.Properties.GetImagePropertiesAsync();
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(await file.OpenAsync(FileAccessMode.Read));
BitmapTransform transform = new BitmapTransform();
transform.Bounds = new BitmapBounds() { Width = props.Width, Height = props.Height };
PixelDataProvider pixelData = await decoder.GetPixelDataAsync(BitmapPixelFormat.Rgba8, decoder.BitmapAlphaMode, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage);
byte[] pixels = pixelData.DetachPixelData();StorageFile imageFile = await installedLocation.CreateFileAsync("Image00.jpg", CreationCollisionOption.ReplaceExisting);
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(Windows.Graphics.Imaging.BitmapEncoder.JpegEncoderId, await imageFile.OpenAsync(FileAccessMode.ReadWrite));
//BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(await imageFile.OpenAsync(FileAccessMode.ReadWrite), decoder);
encoder.SetPixelData(Windows.Graphics.Imaging.BitmapPixelFormat.Rgba8,Windows.Graphics.Imaging.BitmapAlphaMode.Straight,1280,1024,96,96,pixels);
await encoder.FlushAsync();}
注:只有程序停止运行后才能打开图片文件,否则则提示该图片在另外程序中打开,不知道有没办法解决。
问题: transform.Bounds = new BitmapBounds() { Width = props.Width, Height = props.Height };只是将图片截断为相应高度和宽度,有没方法可以不将图片切割而可以将图片保存为相应宽高分辨率的图片。
- 已编辑 Leo06053308 2012年5月22日 6:40
答案
-
用using包 await imageFile.OpenAsync(FileAccessMode.ReadWrite)
...... StorageFile imageFile = await installedLocation.CreateFileAsync("Image00.jpg", CreationCollisionOption.ReplaceExisting); using (IRandomAccessStream stream = await imageFile.OpenAsync(FileAccessMode.ReadWrite)) { BitmapEncoder encoder = await BitmapEncoder.CreateAsync(Windows.Graphics.Imaging.BitmapEncoder.JpegEncoderId, stream); //BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(await imageFile.OpenAsync(FileAccessMode.ReadWrite), decoder); encoder.SetPixelData(Windows.Graphics.Imaging.BitmapPixelFormat.Rgba8, Windows.Graphics.Imaging.BitmapAlphaMode.Straight, 128, 100, 96, 96, pixels); await encoder.FlushAsync(); }
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
- 已标记为答案 Leo06053308 2012年5月24日 7:51
全部回复
-
用using包 await imageFile.OpenAsync(FileAccessMode.ReadWrite)
...... StorageFile imageFile = await installedLocation.CreateFileAsync("Image00.jpg", CreationCollisionOption.ReplaceExisting); using (IRandomAccessStream stream = await imageFile.OpenAsync(FileAccessMode.ReadWrite)) { BitmapEncoder encoder = await BitmapEncoder.CreateAsync(Windows.Graphics.Imaging.BitmapEncoder.JpegEncoderId, stream); //BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(await imageFile.OpenAsync(FileAccessMode.ReadWrite), decoder); encoder.SetPixelData(Windows.Graphics.Imaging.BitmapPixelFormat.Rgba8, Windows.Graphics.Imaging.BitmapAlphaMode.Straight, 128, 100, 96, 96, pixels); await encoder.FlushAsync(); }
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
- 已标记为答案 Leo06053308 2012年5月24日 7:51
-
- 已编辑 Jie BaoModerator 2012年5月24日 6:18