我使用ImageCropper。用于裁剪从相机和图库中选择的图像的表单。
我的代码:
async void OpenCamera()
{
try
{
await CrossMedia.Current.Initialize();
//I need to open camera only here, no need of a pop up again.
new ImageCropper()
{
PageTitle = "Test Title",
AspectRatioX = 1,
AspectRatioY = 1,
CropShape = ImageCropper.CropShapeType.Rectangle,
SelectSourceTitle = "Select source",
TakePhotoTitle = "Take Photo",
PhotoLibraryTitle = "Photo Library",
Success = (imageFile) =>
{
Device.BeginInvokeOnMainThread(() =>
{
profilephoto.Source = ImageSource.FromFile(imageFile);
});
}
}.Show(this);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("CameraException:>" + ex);
}
}
async void Opengallery()
{
try
{
await CrossMedia.Current.Initialize();
//I need to open gallery only here, no need of a pop up again.
new ImageCropper()
{
PageTitle = "Test Title",
AspectRatioX = 1,
AspectRatioY = 1,
CropShape = ImageCropper.CropShapeType.Rectangle,
SelectSourceTitle = "Select source",
TakePhotoTitle = "Take Photo",
PhotoLibraryTitle = "Photo Library",
Success = (imageFile) =>
{
Device.BeginInvokeOnMainThread(() =>
{
profilephoto.Source = ImageSource.FromFile(imageFile);
//var stream = ImageSource.FromFile(imageFile);
//imagefile = imageFile;
});
}
}.Show(this);
}
}
//Save image to gallery
private MediaFile _mediaFile;
public async void SaveProfile(object sender, EventArgs args)
{
var content = new MultipartFormDataContent();
content.Add(new StreamContent(_mediaFile.GetStream()), ""file"", $""{_mediaFile.Path}"");
HttpClient client = new HttpClient();
var response = client.PostAsync(new Uri("Service URL"), content).Result;
}
我需要将裁剪的图像保存到服务器,我在最初的实现中使用MediaPlugin,并使用该插件我将选中的图像作为MediaFile。使用MediaFile,我可以使用其流和路径将图片保存到服务器,如下所示。
但是在裁剪实现之后,我不知道如何获得最终裁剪图像的流值和路径。所以请建议一个解决这个问题的方法。SaveProfile
是OpenCamera()
和Opengallery()
之外的另一个函数
如文档中所示,使用Success
处理程序
new ImageCropper()
{
Success = (imageFile) =>
{
// image file is the string path, do whatever you
// need here
var stream = File.Open(imageFile);
}
}.Show(this);
其中imageFile
是包含文件路径的字符串
您可以使用FileUploaderPlugin插件。
Success = (imageFile) =>
{
CrossFileUploader.Current.UploadFileAsync("<URL HERE>", new FilePathItem("<REQUEST FIELD NAME HERE>","<FILE PATH HERE>"), new Dictionary<string, string>()
{
{"<HEADER KEY HERE>" , "<HEADER VALUE HERE>"}
}
);
}