locked
save image file in windows 8 app RRS feed

  • Question

  • I want to save an image file in windows 8 app via a button,how can i do it?
    Tuesday, September 17, 2013 2:01 PM

Answers

  • It depends on your source. Where is this image file coming from?

    If you have the pixels for the image you can encode it with a BitmapEncoder. If you have the file already then you can save it directly without transformation.

    See Simple imaging sample (Windows 8) for an example of the former and File access sample (Windows 8) for the latter.

    --Rob

    Tuesday, September 17, 2013 8:51 PM
    Moderator
  • Use this to save WriteableBitmap to StorageFile

    private async Task<StorageFile> WriteableBitmapToStorageFile(WriteableBitmap writeableBitmap)
    {
        var picker = new FileSavePicker();
        picker.FileTypeChoices.Add("JPEG Image", new string[] { ".jpg" });
        StorageFile file = await picker.PickSaveFileAsync();
        if (file != null && writeableBitmap != null)
        {
            using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                BitmapEncoder encoder = await BitmapEncoder.CreateAsync(
                    BitmapEncoder.JpegEncoderId, stream);
                Stream pixelStream = writeableBitmap.PixelBuffer.AsStream();
                byte[] pixels = new byte[pixelStream.Length];
                await pixelStream.ReadAsync(pixels, 0, pixels.Length);
    
                encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore,
                    (uint)writeableBitmap.PixelWidth, (uint)writeableBitmap.PixelHeight, 96.0, 96.0, pixels);
                await encoder.FlushAsync();
            }
            return file;
        }
        else
        {
            return null;
        }
    }

    Wednesday, September 18, 2013 12:12 PM

All replies

  • It depends on your source. Where is this image file coming from?

    If you have the pixels for the image you can encode it with a BitmapEncoder. If you have the file already then you can save it directly without transformation.

    See Simple imaging sample (Windows 8) for an example of the former and File access sample (Windows 8) for the latter.

    --Rob

    Tuesday, September 17, 2013 8:51 PM
    Moderator
  • I have converted a color pic to grayscale and i want to save that gray pic how can i do that?
    Wednesday, September 18, 2013 4:27 AM
  • Where is that grayscale pic ? Is it source of an <Image /> control ?
    Wednesday, September 18, 2013 8:57 AM
  • I have taken two image tools one for source and one for converted image(grayscale) i wanted to save dat converted pic which is my gray scale pic.
    Wednesday, September 18, 2013 9:38 AM
  • In which form (object type) grayscale pic is ? BitmapImage ? WritableBitmap ? byte[] ? stream ? StorageFile ?
    Wednesday, September 18, 2013 9:45 AM
  • Writable bitmap
    Wednesday, September 18, 2013 10:15 AM
  • Use this to save WriteableBitmap to StorageFile

    private async Task<StorageFile> WriteableBitmapToStorageFile(WriteableBitmap writeableBitmap)
    {
        var picker = new FileSavePicker();
        picker.FileTypeChoices.Add("JPEG Image", new string[] { ".jpg" });
        StorageFile file = await picker.PickSaveFileAsync();
        if (file != null && writeableBitmap != null)
        {
            using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                BitmapEncoder encoder = await BitmapEncoder.CreateAsync(
                    BitmapEncoder.JpegEncoderId, stream);
                Stream pixelStream = writeableBitmap.PixelBuffer.AsStream();
                byte[] pixels = new byte[pixelStream.Length];
                await pixelStream.ReadAsync(pixels, 0, pixels.Length);
    
                encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore,
                    (uint)writeableBitmap.PixelWidth, (uint)writeableBitmap.PixelHeight, 96.0, 96.0, pixels);
                await encoder.FlushAsync();
            }
            return file;
        }
        else
        {
            return null;
        }
    }

    Wednesday, September 18, 2013 12:12 PM