怎么上传图片后裁剪好了

0

的问题

我使用 ImageCropperMediaPlugin 上传的图像。 但是我有问题得到图像之后剪切图像。

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

但是我怎么能得到的图像上传。 注意, view_imageavatar 仍然显示图像之后种植。 Tks!

更新...

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

它仍然不能工作?

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

-> 它仍然不能上传照片过API?

然而,我尽量不要使用ImageCropper. 我上传。

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

--> 然后它工作得很好,图像已上载的通过API

不会像从 content.Add(new StreamContent(ms), "files", imageFile); 它不工作与API? 在寻找解决方案,从每一个人。

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

最好的答案

0

你有没有实际检查什么StreamContent需要作为论据?

它需要一个 Stream 没有一个路径的文件。

你需要打开文件第一,像这样:

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

你有没有尝试过类似的东西吗?

2021-11-24 07:52:53

谢谢你。 然而 File.Open(imageFile); -> Open 不会的工作。 我通过在 File.OpenRead(imageFile). 这是很好吗? 我已经更新了以上。
Chim Di Tru

其他语言

此页面有其他语言版本

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