none
BitmapDecoder与BitmapEncoder问题 RRS feed

  • 问题

  • 利用以下代码保存的图片始终不能打开,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 };只是将图片截断为相应高度和宽度,有没方法可以不将图片切割而可以将图片保存为相应宽高分辨率的图片。

    2012年5月21日 10:20

答案

  • 用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

    2012年5月24日 4:05
    版主

全部回复

  • 除非程序关闭,打开文件都会这样

    但是如果后续有对该图片文件进行操作,比如

    StorageFile LockScreenFile = await installedLocation.GetFileAsync("Image.jpg");都会提示文件没有找到的错误

    2012年5月22日 9:52
  • 用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

    2012年5月24日 4:05
    版主
  • 我试过用using问题还是没解决,最后我把IrandomAccessStream通过DataReader转换为bytes再通过DataWriter保存
    2012年5月24日 6:12
  • 奇怪了,我试了using是好的。 不过多一次DataReader转bytes 也行。

    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us


    2012年5月24日 6:18
    版主
  • 我又重新试了下可以了!
    2012年5月24日 7:50