Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Convert Image to Base64 string

Xammy10Xammy10 Member ✭✭✭

I already did the conversion, but i set the image as static, I don't know how to make it dynamic like, I'll search in my gallery or take some pictures.

Here is my conversion:

Bitmap mBit = BitmapFactory.DecodeResource(Resources, Resource.Drawable.user);
MemoryStream stream = new MemoryStream();
mBit.Compress(Bitmap.CompressFormat.Png, 0, stream);
byte[] ba = stream.ToArray();
string bal = Convert.ToBase64String(ba, Base64FormattingOptions.None);
resultTxt.Text = bal.ToString();

**Here is how I take a picture:
**
private void ClickMeh_Click(object sender, EventArgs e)
{
Intent pfp = new Intent(MediaStore.ActionImageCapture);
StartActivityForResult(pfp, 0);
}

Here is how I display the picture I take in the Image Button:

base.OnActivityResult(requestCode, resultCode, data);
Bitmap bitmap = (Bitmap)data.Extras.Get("data");
clickMeh.SetImageBitmap(bitmap);

**My Question is:

How to combine all of those?**

Image - Base64string converting is still not clear on me. I just search things up and combine it, that's why i convert it. But the data is static, what I want to convert is the image I took from the camera of from the gallery.

Thanks in advance :)

Answers

  • sivabhavanisivabhavani USMember ✭✭

    Hi Enohara,

    static byte[] imgByteData { get; set; }

                         ``var stream = file.GetStream();
                         var memoryStream = new MemoryStream();
                         file.GetStream().CopyTo(memoryStream);
                         imgByteData = memoryStream.ToArray();
    

    Can you please try this once..

  • DimChrisDimChris USMember ✭✭✭✭

    I need to ask why you want to convert it on base64? Do you want to send it in a server? Base64 converts an image into a a 'byte string' for sending them through network or other proposes.

  • Xammy10Xammy10 Member ✭✭✭

    @DimChris I want it to save on database

  • Xammy10Xammy10 Member ✭✭✭
    edited March 2018

    @sivabhavani said:
    Hi Enohara,

    static byte[] imgByteData { get; set; }

                         ``var stream = file.GetStream();
                         var memoryStream = new MemoryStream();
                         file.GetStream().CopyTo(memoryStream);
                         imgByteData = memoryStream.ToArray();
    

    Can you please try this once..

    Can you make a sample that you will get an image from gallery, I still don't get the part when getting the picture, and where I'm gonna put my picture in the converter code.

    for example:

    Bitmap mBit = BitmapFactory.DecodeResource(Resources, Resource.Drawable.sampleQR);

    with this, i can convert the sampleQR.png to base64. I get the file from my drawable folder.

    But what if i get the file from the gallery and display it on the image view? I still don't get that part, It's still not clear to me

    BTW, thanks for your answer :)

  • sivabhavanisivabhavani USMember ✭✭

    Hi Enohara,

    In my app i am using a plguin called (http://https://www.nuget.org/packages/Xam.Plugin.Media/ " https://www.nuget.org/packages/Xam.Plugin.Media/")

    Here is my code,

        private async void pickPhoto_Clicked(object sender, EventArgs e)
                {
    
    
                    if (!CrossMedia.Current.IsPickPhotoSupported)
                    {
                        //DisplayAlert("Photos Not Supported", ":( Permission not granted to photos.", "OK");
                        return;
                    }
                    var file = await CrossMedia.Current.PickPhotoAsync();
    
    
                    if (file == null)
                        return;
    
                    App.fileName = Path.GetFileName(file.Path);
                    App.fileExt = Path.GetExtension(file.Path);
                    removeBtn.IsVisible = true;
    
    
                    image.Source = ImageSource.FromStream(() =>
                    {
                        var stream = file.GetStream();
    
                        var memoryStream = new MemoryStream();
                        file.GetStream().CopyTo(memoryStream);
                        App.imgByteData = memoryStream.ToArray();
    
                        file.Dispose();
    
                        return stream;
                    });
                }
    

    try this once..may it help you

  • Xammy10Xammy10 Member ✭✭✭

    It's ok now sir, I get how things work in Bitmap - String Conversion part :)

    Thanks for the effort sir :)

Sign In or Register to comment.