locked
Create Jpeg/Png file from BitmapImage in c#?

    Question

  • Hi All,

    i Want to create a Jpeg/png file out of my Byte array.

    am getting byte array from service now i need to convert byte array to physical image and save it in app local folder.

    any suggestion would be of great help. 

    thanks

    arjun


    Arjun

    Thursday, April 3, 2014 5:35 PM

Answers

All replies

  • What data is in your byte array?

    Usually services will provide data already encoded in a bitmap format (such as jpg or png), so all you need to do is save it to a file. See Quickstart: Reading and writing files

    If you have decoded ARGB pixels then you can use a BitmapEncoder to encode to jpg or png and then save that out. See How to encode a new image

    --Rob

    Thursday, April 3, 2014 5:48 PM
    Moderator
  • Hi Bob,

    Thanks For Your Reply

    I tried the link Which You have replied.

    It is working for me but problem is because of await method it is not writing bytes in file.

    its is jumping to next line of code.

    Any suggestion wpuld be of great help

    below is my code

    public async void CreateFile(byte[] InputArray,string FileName)
           {
               try
               {
                   StorageFolder storageFolder = ApplicationData.Current.TemporaryFolder;
    
                   this.file = await storageFolder.CreateFileAsync(
                     FileName, CreationCollisionOption.ReplaceExisting);
                   using (IRandomAccessStream fileStream =
                            await file.OpenAsync(FileAccessMode.ReadWrite))
                   {
                       Stream stream = fileStream.AsStreamForWrite();
                       byte[] array = InputArray;
                       stream.SetLength(stream.Length + array.Length);
    
                       await stream.WriteAsync(array, 0, array.Length);
                       await stream.FlushAsync();
    
                       await fileStream.FlushAsync();
                   }
               }
               catch (Exception ex)
               {
    
                   throw ex;
               }
             
    
           }

    Thanks

    Arjun.M


    Arjun


    Tuesday, April 8, 2014 1:14 PM
  • You can make your function awaitable by returning a Task instead of void. See Asynchronous programming in .NET (Windows Runtime apps using C#/VB/C++ and XAML)
    Tuesday, April 8, 2014 3:24 PM
    Moderator