Comment télécharger des images après le recadrage de Xamarin

0

La question

Je suis à l'aide de ImageCropper et MediaPlugin pour Télécharger des images. Cependant j'ai un problème obtenir l'image après le recadrage de l'image.

string imagefile;
protected void OnClickedRectangle(object sender, EventArgs e)
{
    new ImageCropper()
    {
        Success = (imageFile) =>
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                view_imageavatar.Source = ImageSource.FromFile(imageFile);

            });
        }
    }.Show(this);
}

async void edit_avatar_Tapped(object sender, EventArgs e)
{
    try
    {
        await CrossMedia.Current.Initialize();
        new ImageCropper()
        {
            PageTitle = "Title",
            AspectRatioX = 1,
            AspectRatioY = 1,
            CropShape = ImageCropper.CropShapeType.Rectangle,
            SelectSourceTitle = "Img",
            TakePhotoTitle = "Take Camera",
            PhotoLibraryTitle = "Img Gallery",
            Success = (imageFile) =>
            {
                Device.BeginInvokeOnMainThread(async() =>
                {
                    view_imageavatar.Source = ImageSource.FromFile(imageFile);
                    imagefile = imageFile;
                    //API Get Images Upload
                    var content = new MultipartFormDataContent();
                    content.Add(new StreamContent(imageFile), "files", imagefile);
                    var httpClient = new HttpClient();
                    var responses = await httpClient.PostAsync("https://xxxxx/api/Upload", content);
                });
            }
        }.Show(this);
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine("GalleryException:>" + ex);
    }
}

enter image description here

Cependant comment puis-je obtenir de l'Image à télécharger. notez que view_imageavatar affiche toujours l'image après le recadrage. Tks!

Mise à jour...

async void edit_avatar_Tapped(object sender, EventArgs e)
{
    try
    {
        await CrossMedia.Current.Initialize();
        new ImageCropper()
        {
            PageTitle = "Title",
            AspectRatioX = 1,
            AspectRatioY = 1,
            CropShape = ImageCropper.CropShapeType.Rectangle,
            SelectSourceTitle = "Img",
            TakePhotoTitle = "Take Camera",
            PhotoLibraryTitle = "Img Gallery",
            Success = (imageFile) =>
            {
                Device.BeginInvokeOnMainThread(async() =>
                {
                    view_imageavatar.Source = ImageSource.FromFile(imageFile);
                    imagefile = imageFile;
                    //API Get Images Upload
                    
                    var fileStream = File.OpenRead(imageFile);
                    var fileContent = new StreamContent(fileStream);

                    var content = new MultipartFormDataContent();
                    content.Add(fileContent, "files", imageFile);
                    var httpClient = new HttpClient();    
                    
                    var responses = await httpClient.PostAsync("https://xxxxxx/api/UploadAvatarUs", content);   
                });
            }
        }.Show(this);
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine("GalleryException:>" + ex);
    }
}

Cela ne fonctionne toujours pas?

Mise à jour 2

async void edit_avatar_Tapped(object sender, EventArgs e)
{
    try
    {
        await CrossMedia.Current.Initialize();
        new ImageCropper()
        {
            PageTitle = "Title",
            AspectRatioX = 1,
            AspectRatioY = 1,
            CropShape = ImageCropper.CropShapeType.Rectangle,
            SelectSourceTitle = "Img",
            TakePhotoTitle = "Take Camera",
            PhotoLibraryTitle = "Img Gallery",
            Success = (imageFile) =>
            {
                Device.BeginInvokeOnMainThread(async() =>
                {
                    view_imageavatar.Source = ImageSource.FromFile(imageFile);
                    imagefile = imageFile;
                    //API Get Images Upload

                    var upfilebytes = File.ReadAllBytes(imageFile);
                    var ms = new MemoryStream(upfilebytes);
                    var content = new MultipartFormDataContent();
                    content.Add(new StreamContent(ms), "files", imageFile);


                    var httpClient = new HttpClient();    

                    var responses = await httpClient.PostAsync("https://xxxxxx/api/UploadAvatarUs", content);   
                });
            }
        }.Show(this);
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine("GalleryException:>" + ex);
    }
}

-> Il ne peut toujours pas télécharger des photos via l'API?

Cependant j'essaie de ne pas utiliser ImageCropper. Je les télécharger directement.

async void edit_avatar_Tapped(object sender, EventArgs e)
{
    var file = await MediaPicker.PickPhotoAsync();
    var content = new MultipartFormDataContent();
    content.Add(new StreamContent(await file.OpenReadAsync()), "files", file.FileName);

    var httpClient = new HttpClient();

    var responses = await httpClient.PostAsync("https://xxxxxx/api/UploadAvatarUs", content);
    string a = responses.StatusCode.ToString();
}

--> Puis il fonctionne très bien, l'image est téléchargée par le biais de l'API

Le chargement de l'image de content.Add(new StreamContent(ms), "files", imageFile); il ne fonctionne pas avec l'API? À la recherche de solutions à partir de tout le monde.

crop image-upload xamarin
2021-11-24 06:45:00
1

La meilleure réponse

0

Avez-vous réellement vérifié ce que StreamContent prend comme arguments?

Il faut un Stream pas un chemin d'accès à un fichier.

Vous devez ouvrir le fichier comme suit:

using var fileStream = File.Open(imageFile);
using var fileContent = new StreamContent(fileStream);

Avez-vous essayé quelque chose comme ça?

2021-11-24 07:52:53

Je vous remercie. Cependant File.Open(imageFile); -> Open ne fonctionne pas. Je passe File.OpenRead(imageFile). Est-ce bien? J'ai mis à jour ci-dessus.
Chim Di Tru

Dans d'autres langues

Cette page est dans d'autres langues

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................