none
How to use Windows.Graphics.Imaging to transcode an image file RRS feed

答案

  • I am not sure what is the image file type, please try to use my sample code below, transform the Thumbnail via WriteableBitmap, but we have to scale the size to one static value:

                FileOpenPicker fop = new FileOpenPicker();
                fop.FileTypeFilter.Add(".jpg");
                fop.FileTypeFilter.Add(".png");
                fop.FileTypeFilter.Add(".bmp");
                fop.SuggestedStartLocation = PickerLocationId.Desktop;
                fop.ViewMode = PickerViewMode.List;
                StorageFile jpgfile = await fop.PickSingleFileAsync();
    
                const uint size = 200; // static width for Thumbnail 
                using (StorageItemThumbnail itemThumbnail = await jpgfile.GetThumbnailAsync(ThumbnailMode.PicturesView, size, ThumbnailOptions.ResizeThumbnail))
                {
                    WriteableBitmap writeableBitmap = new WriteableBitmap((int)itemThumbnail.OriginalWidth, (int)itemThumbnail.OriginalHeight);
                    writeableBitmap.SetSource(itemThumbnail);
                    Stream readtream = writeableBitmap.PixelBuffer.AsStream();
    
                    byte[] pixelBuffer = new byte[itemThumbnail.Size];
                    await readtream.ReadAsync(pixelBuffer, 0, pixelBuffer.Length);
    
                    FileSavePicker fsp = new FileSavePicker();
                    fsp.SuggestedFileName = @"Test.jpg";
                    fsp.DefaultFileExtension = ".jpg";
                    fsp.FileTypeChoices.Add("jpg", new List<String>() { ".jpg" });
                    fsp.SuggestedStartLocation = PickerLocationId.Desktop;
                    StorageFile pngfile = await fsp.PickSaveFileAsync();
                    using (IRandomAccessStream pngstream = await pngfile.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, pngstream);
                        encoder.SetPixelData(BitmapPixelFormat.Rgba8,
                                             BitmapAlphaMode.Ignore,
                                             itemThumbnail.OriginalWidth,
                                             itemThumbnail.OriginalHeight,
                                             96,
                                             96,
                                             pixelBuffer);
                        await encoder.FlushAsync();
                    }
                }

    Note, the JPG will ignore the alpha channel.


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

    2012年8月1日 12:02
    版主
  • What type image do you want to transcode. Below is one sample to transcode from jpg to png:

                FileOpenPicker fop = new FileOpenPicker();
                fop.FileTypeFilter.Add(".jpg");
                fop.SuggestedStartLocation = PickerLocationId.Desktop;
                fop.ViewMode = PickerViewMode.List;
                StorageFile jpgfile = await fop.PickSingleFileAsync();
                using (IRandomAccessStream jpgstream = await jpgfile.OpenAsync(FileAccessMode.Read))
                {
                    BitmapDecoder decoder = await BitmapDecoder.CreateAsync(BitmapDecoder.JpegDecoderId, jpgstream);
                    PixelDataProvider dataProvider = await decoder.GetPixelDataAsync();
                    FileSavePicker fsp = new FileSavePicker();
                    fsp.SuggestedFileName = @"Test.png";
                    fsp.DefaultFileExtension = ".png";
                    fsp.FileTypeChoices.Add("png", new List<String>() { ".png" });
                    fsp.SuggestedStartLocation = PickerLocationId.Desktop;
                    StorageFile pngfile = await fsp.PickSaveFileAsync();
                    using (IRandomAccessStream pngstream = await pngfile.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, pngstream);
                        encoder.SetPixelData(decoder.BitmapPixelFormat,
                                             decoder.BitmapAlphaMode,
                                             decoder.PixelWidth,
                                             decoder.PixelHeight,
                                             decoder.DpiX,
                                             decoder.DpiY,
                                             dataProvider.DetachPixelData());
                        await encoder.FlushAsync();
                    }
                }



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


    2012年7月30日 7:04
    版主

全部回复

  • What type image do you want to transcode. Below is one sample to transcode from jpg to png:

                FileOpenPicker fop = new FileOpenPicker();
                fop.FileTypeFilter.Add(".jpg");
                fop.SuggestedStartLocation = PickerLocationId.Desktop;
                fop.ViewMode = PickerViewMode.List;
                StorageFile jpgfile = await fop.PickSingleFileAsync();
                using (IRandomAccessStream jpgstream = await jpgfile.OpenAsync(FileAccessMode.Read))
                {
                    BitmapDecoder decoder = await BitmapDecoder.CreateAsync(BitmapDecoder.JpegDecoderId, jpgstream);
                    PixelDataProvider dataProvider = await decoder.GetPixelDataAsync();
                    FileSavePicker fsp = new FileSavePicker();
                    fsp.SuggestedFileName = @"Test.png";
                    fsp.DefaultFileExtension = ".png";
                    fsp.FileTypeChoices.Add("png", new List<String>() { ".png" });
                    fsp.SuggestedStartLocation = PickerLocationId.Desktop;
                    StorageFile pngfile = await fsp.PickSaveFileAsync();
                    using (IRandomAccessStream pngstream = await pngfile.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, pngstream);
                        encoder.SetPixelData(decoder.BitmapPixelFormat,
                                             decoder.BitmapAlphaMode,
                                             decoder.PixelWidth,
                                             decoder.PixelHeight,
                                             decoder.DpiX,
                                             decoder.DpiY,
                                             dataProvider.DetachPixelData());
                        await encoder.FlushAsync();
                    }
                }



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


    2012年7月30日 7:04
    版主
  • all right.

    in fact i want to save a stream[ get frome StorageFile.GetThumbnailAsync() ]  as a jpg file.

    but always failed.

    later i found an program can save a thumbnail in app sample,i copy the codes and try them in my own app.

    but when i use some desktop softwares to scan the file , the detail shows that this file's format is "bitmap".

    how to transcode it into an jpg file?

    javascript.

    thanks

    2012年8月1日 5:54
  • "bitmap" or "bmp"?

    For saving the image into jpg via BitmapEncoder, we just need the image size, including the PixelWidth, PixelHeight, DPI and the pixel data array. So whatever you get from the StorageFile.GetThumbnailAsync method, you just could read the pixel data from the StorageItemThumbnail object, and transfer it to the encoder.SetPixelData method.


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

    2012年8月1日 6:11
    版主
  • can you give me a sample?i do am confused by this problem.

    format is "bitmap".

    checked by MediaInfo v0.7.58

    2012年8月1日 9:37
  • I am not sure what is the image file type, please try to use my sample code below, transform the Thumbnail via WriteableBitmap, but we have to scale the size to one static value:

                FileOpenPicker fop = new FileOpenPicker();
                fop.FileTypeFilter.Add(".jpg");
                fop.FileTypeFilter.Add(".png");
                fop.FileTypeFilter.Add(".bmp");
                fop.SuggestedStartLocation = PickerLocationId.Desktop;
                fop.ViewMode = PickerViewMode.List;
                StorageFile jpgfile = await fop.PickSingleFileAsync();
    
                const uint size = 200; // static width for Thumbnail 
                using (StorageItemThumbnail itemThumbnail = await jpgfile.GetThumbnailAsync(ThumbnailMode.PicturesView, size, ThumbnailOptions.ResizeThumbnail))
                {
                    WriteableBitmap writeableBitmap = new WriteableBitmap((int)itemThumbnail.OriginalWidth, (int)itemThumbnail.OriginalHeight);
                    writeableBitmap.SetSource(itemThumbnail);
                    Stream readtream = writeableBitmap.PixelBuffer.AsStream();
    
                    byte[] pixelBuffer = new byte[itemThumbnail.Size];
                    await readtream.ReadAsync(pixelBuffer, 0, pixelBuffer.Length);
    
                    FileSavePicker fsp = new FileSavePicker();
                    fsp.SuggestedFileName = @"Test.jpg";
                    fsp.DefaultFileExtension = ".jpg";
                    fsp.FileTypeChoices.Add("jpg", new List<String>() { ".jpg" });
                    fsp.SuggestedStartLocation = PickerLocationId.Desktop;
                    StorageFile pngfile = await fsp.PickSaveFileAsync();
                    using (IRandomAccessStream pngstream = await pngfile.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, pngstream);
                        encoder.SetPixelData(BitmapPixelFormat.Rgba8,
                                             BitmapAlphaMode.Ignore,
                                             itemThumbnail.OriginalWidth,
                                             itemThumbnail.OriginalHeight,
                                             96,
                                             96,
                                             pixelBuffer);
                        await encoder.FlushAsync();
                    }
                }

    Note, the JPG will ignore the alpha channel.


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

    2012年8月1日 12:02
    版主
  • JS中,没有WriteableBitmap 那我要怎么办呢。 我的图片是bmp 格式的, 我要怎么转换为jpg 呢???谢谢
    2012年9月3日 14:50
  • WriteableBitmap 是 WinRT的类型 ,包括我上面的代码用的 都是WinRT的类型 ,可以在JS中使用的 。

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

    2012年9月4日 3:25
    版主