Forum Xamarin.Android

Convert from Binary To image

hello guys
i used this code to insert into my sql server the image i loaded

            Stream st = ContentResolver.OpenInputStream(uri);
            Android.Graphics.Bitmap bitmap = BitmapFactory.DecodeStream(st);
            MemoryStream memoryStream = new MemoryStream();
            bitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Jpeg, 100, memoryStream);

            byte[] data = memoryStream.ToArray();
            image = Convert.ToBase64String(data);


            SqlCommand cm = new SqlCommand("update dbo.Members set Member_Name= '" + firs.Text + " " + second.Text + "',Image=CONVERT(Varbinary(MAX),'" + image + "'), Phone='" + phone.Text + "' where user_name ='" + mail.Text + "' ", cn);
            cm.CommandTimeout = 0;

and when i load my app from start i select the info and sent them with intent to the next actiity like that

                    emailadress = dt.Rows[0]["user_name"].ToString();
                    user_name = dt.Rows[0]["Member_Name"].ToString();
                    image = dt.Rows[0]["Image"].ToString();

well my question how to reback that to bitmap to use into that code

i used this code but not working

             image = Intent.GetStringExtra("Image") ?? "Data is not available";

             byte[] bytes = Encoding.ASCII.GetBytes(image);

            MemoryStream memoryStream = new MemoryStream();
            memoryStream.Write(bytes, 0, bytes.Length);

            bitmap = BitmapFactory.DecodeStream(memoryStream);

circleImageView.SetImageBitmap(bitmap);

thanks at advance

Tagged:

Best Answer

  • emadyounanemadyounan ✭✭
    Accepted Answer

    I found the solution xD
    I made this:-

    i changed the column type to varchar(MAX) first then
    at the main activity

             image = Intent.GetStringExtra("Image") ?? "Data is not available";
            byte[] decodedString = Base64.Decode(image, Base64.Default);
            Android.Graphics.Bitmap decodedByte = BitmapFactory.DecodeByteArray(decodedString, 0, decodedString.Length);
    

    and it worked

Answers

  • emadyounanemadyounan Member ✭✭
    Accepted Answer

    I found the solution xD
    I made this:-

    i changed the column type to varchar(MAX) first then
    at the main activity

             image = Intent.GetStringExtra("Image") ?? "Data is not available";
            byte[] decodedString = Base64.Decode(image, Base64.Default);
            Android.Graphics.Bitmap decodedByte = BitmapFactory.DecodeByteArray(decodedString, 0, decodedString.Length);
    

    and it worked

Sign In or Register to comment.