Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have taken a button and selected an image using PhotoChooserTask ..then i displayed its file name on a button .. now i need to convert that image into base64 and display that base64 value in a textbox.. i dont know how to convert .. Any help will be appreciated

C#
void photoChooserTask_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                //Initialise the result 
                photoStream = new MemoryStream();
                // saving (copy to stream)
                e.ChosenPhoto.CopyTo(photoStream);
                //saving original file name 
                filename = e.OriginalFileName;
                //TxtBlock.Text = e.OriginalFileName;
                var bitmapImage = new BitmapImage();
                bitmapImage.SetSource(photoStream);
                //ImageViewer.Source = bitmapImage;
                btnupload.Content = e.OriginalFileName;
            }
       }
Posted
Updated 5-Mar-15 19:01pm
v2
Comments
Richard MacCutchan 27-Feb-15 4:03am    
You just need to convert the MemoryStream to a byte array and then convert that to base64.
Aditya_Goud 2-Mar-15 6:37am    
cn u plz guide me ..
Richard MacCutchan 2-Mar-15 6:53am    
guide you in what? You can look at the documentation for MemoryStream to see how to convert it to a byte array. And you can do a Google search for how to convert bytes to base64.
Aditya_Goud 6-Mar-15 1:06am    
Hey Thanks for the guidance .. here i have done the task(cn see in answer below).. but this works only for the small size images .. what changes are to be made to make it compatible for large size images ..
Richard MacCutchan 6-Mar-15 3:03am    
Sorry, no idea. You need to investigate what is the maximum image size that it works for, and then try and find what errors cause it to fail.

1 solution

Heyy .. Found it my self.. Posting the sol here for further developers to know.. Here is the complete solution for selecting image from gallery using Photochoosertask ,converting that to byte and then byte to base64 string


C#
public void photoChooserTask_Completed(object sender, PhotoResult e)
 {
     if (e.TaskResult == TaskResult.OK)
     {
         //Initialise the result
         photoStream = new MemoryStream();
         // saving (copy to stream)
         e.ChosenPhoto.CopyTo(photoStream);
         //saving original file name
         filename = e.OriginalFileName;
         //Image Filename to textbox text

         btnUpload.IsEnabled = true;

         //Convert Memory Stream To byte Array
         byte[] imgArray = new byte[e.ChosenPhoto.Length];
         e.ChosenPhoto.Read(imgArray, 0, imgArray.Length);
         // Creating Bitmap Image
         BitmapImage bitmap = new BitmapImage();
         bitmap.SetSource(e.ChosenPhoto);
         WriteableBitmap wb = new WriteableBitmap(bitmap);
         MemoryStream ms = new MemoryStream();
         wb.SaveJpeg(ms, bitmap.PixelWidth, bitmap.PixelHeight, 0, 100);
         byte[] imageBytes = ms.ToArray();
         string strimageInBase64;
         strimageInBase64 = System.Convert.ToBase64String(imageBytes);

         //print byte Array
         //StreamTextbox.Text = imgArray.ToString();

         //Just Print Byte64 to know output
         StreamTextbox.Text = strimageInBase64;
     }
 }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900