Odpovědět How to save image form imagesource

  • Thursday, April 12, 2012 11:11 AM
     
      Has Code

    I have Image control ,I want so save image to Local Data from Image Control source

            public static async Task SaveImages(BitmapSource images)
            {
    var folder = ApplicationData.Current.LocalFolder;
                try
                {
                    StorageFile file = await folder.CreateFileAsync("111.png", CreationCollisionOption.ReplaceExisting);
                    using (var readStream = await file.OpenAsync(FileAccessMode.ReadWrite))
                    { 
                        var outPutStream = readStream.GetOutputStreamAt(0);
                        WriteableBitmap wb = new WriteableBitmap(images.PixelWidth, images.PixelHeight);
                        DataWriter dw = new DataWriter(outPutStream);                    
                        dw.WriteBytes(wb.PixelBuffer.ToArray());
                        await dw.StoreAsync();
                        await outPutStream.FlushAsync();
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
    }


    that's code work but  I can't open this images file (111.png)  from windows picture manger

    var bytes=wb.PixelBuffer.ToArray();

    and then I debug found bytes  item value is 0



    • Edited by Trigged Friday, April 13, 2012 5:45 AM
    •  

All Replies

  • Friday, April 13, 2012 3:48 PM
    Moderator
     
     

    You are storing out raw, uninitialized bytes from your WriteableBitmap.

    You will need to use a WriteableBitmap which has data in it and then convert to PNG format with a BitmapEncoder.

    I believe the imaging sample demonstrates how to do this.

    --Rob

  • Monday, April 16, 2012 2:15 AM
     
     

    I see the imaging sample through FilePicker get the stream 

     I set  Uri to image control  ,So I use the HttpClient download the image get the stream and convert it ,But in this way its very complex

    Is there any simple solution convert BitmapSource to IRandomAccessStream tks


  • Friday, April 20, 2012 12:40 AM
    Moderator
     
     Answered

    There is no way to convert a BitmapSource to an IRandomAccessStream. You need to either keep the original source URL or store the bits in a WriteableBitmap.

    To convert the pixels from their raw format in the WriteableBitmap into a storage format such as PNG you would use a BitmapEncoder.

    --Rob

    • Marked As Answer by Trigged Friday, April 20, 2012 1:24 AM
    •