Ako nahrať obrázky po orezaní v Xamarin

0

Otázka

Ja používam ImageCropper a MediaPlugin ak chcete Nahrať obrázky. Som však majú problém dostať obrázka po orezaní obrazu.

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

Avšak ako môžem získať Obraz na odovzdanie. upozorňujeme, že view_imageavatar stále ukazuje obrázok po orezaní. Tks!

Aktualizácia...

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);
    }
}

Je stále nefunguje?

Aktualizácia 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);
    }
}

-> To stále nie je možné nahrávať fotografie cez API?

Som však snažte sa používať ImageCropper. Som nahrať priamo.

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();
}

--> Potom to funguje, obraz odovzdáva cez API

Má obrázok načítať z content.Add(new StreamContent(ms), "files", imageFile); nefunguje to s API? A hľadáme riešenia, od každého.

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

Najlepšiu odpoveď

0

Už ste vlastne kontrolovať, čo StreamContent berie ako argumenty?

To trvá Stream nie cestu k súboru.

Musíte otvoriť súbor, ako prvý tak:

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

Už ste vyskúšali niečo ako, že?

2021-11-24 07:52:53

Ďakujem. Avšak File.Open(imageFile); -> Open nefunguje. Som prejsť v File.OpenRead(imageFile). Je to dobré? Som aktualizoval vyššie.
Chim Di Tru

V iných jazykoch

Táto stránka je v iných jazykoch

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