ImageCropper.Forms: 如何获取最终裁剪后的图片的stream & path值?
ImageCropper.Forms: How to get the stream & path value of the final cropped image?
我正在使用 ImageCropper.Forms 裁剪从相机和图库中选择的图像。
我的代码:
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()
之外的另一个函数。
如 docs 所示,使用 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>"}
}
);
}
我正在使用 ImageCropper.Forms 裁剪从相机和图库中选择的图像。
我的代码:
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()
之外的另一个函数。
如 docs 所示,使用 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>"}
}
);
}