Error ReasonPhrase: 'Request-URI Too Long', Version: 1.1, cause image convertion to byte

lizavenitalizavenita USMember ✭✭

Halo Everyone,
I work on a xamarin forms app, and I need to convert my image to byte and then upload it when InsertMethod call. The conversion result will be one of the parameters in insert method. But when I try to upload the image I got this error:

StatusCode: 414, ReasonPhrase: 'Request-URI Too Long', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:

It cause of the conversion result, in byte format, is too long, Is there another way to convert them the image or to set the maximum URL length?
This is my code for pick the image and converts it. Thank you.
private async void PickPhoto_Clicked(object sender, EventArgs e)
{
try
{
await CrossMedia.Current.Initialize();

            if (!CrossMedia.Current.IsPickPhotoSupported)
            {
                await DisplayAlert("No PickPhoto", ":( No PickPhoto available.", "OK");
                return;
            }

            _mediaFile = await CrossMedia.Current.PickPhotoAsync();

            if (_mediaFile == null)
                return;

            LocalPathLabel.Text = _mediaFile.Path;

            FileImage.Source = ImageSource.FromStream(() =>
            {
                return _mediaFile.GetStream();
            });

            using (var memoryStream = new MemoryStream())
            {
                _mediaFile.GetStream().CopyTo(memoryStream);
                _mediaFile.Dispose();
                imageAsBytes = memoryStream.ToArray();

                var byteArray = memoryStream.ToArray();
                string base64String = Convert.ToBase64String(byteArray);
                fileBytee = base64String;
            }
        }
        catch (Exception a)
        {
            Debug.WriteLine(a.Message);
            Debug.WriteLine("eeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
        }
    }

Answers

  • ManojkumarMaliManojkumarMali USMember ✭✭✭

    Hi,

    Try below code to compress image while pick photo...

    var file = await CrossMedia.Current.PickPhotoAsync( new PickMediaOptions { CompressionQuality = 50, PhotoSize = PhotoSize.Medium });

    Also write your image conversion code out side of this pick method.

  • lizavenitalizavenita USMember ✭✭

    Halo @ManojkumarMali ,
    Thank you so much for your answer, it works!
    But when I checked the image in the service its blur and too small, and there are some imperfection in the image upload such:
    I only can upload image size >1 MB if I use PhotoSize = PhotoSize.Small, but it caused the image after conversion too small.
    If I use PhotoSize = PhotoSize.Large with the size >30 KB it will cause error, but under 30 is OK and the image after conversion is bigger.
    Am I did something wrong? Thanks in advance @ManojkumarMali :smile:

  • ManojkumarMaliManojkumarMali USMember ✭✭✭

    Hi @lizavenita ,

    Change the CompressionQuality value & check.

  • lizavenitalizavenita USMember ✭✭

    I already try in range 10 -100 CompressionQuality and PhotoSize from Small - Full/Large, but sometimes it does not work for bigger image(1 MB) and the conversion result is blur. Thanks @ManojkumarMali :smiley:

  • ManojkumarMaliManojkumarMali USMember ✭✭✭

    Ohhh ok.

    Then try to compress image after capture.
    While capture, capture high quality image & then write code to compress image.

    This might help you..
    c-sharpcorner.com/uploadfile/rahuldebray/compress-image-to-a-given-size/

Sign In or Register to comment.