如何解决如何在 Xamarin 中设置新保存文件的名称?
重点是我想命名我要保存的图片,但我不知道如何以及然后我需要获取该保存图像的路径。你有什么想法如何做到这一点吗? 我现在有这个:
using System.IO.Path;
namespace Spotter.Views
{
[XamlCompilation(XamlCompilationoptions.Compile)]
public partial class ImportPage : ContentPage
{
public ImportPage()
{
InitializeComponent();
NavigationPage.SetHasNavigationBar(this,false);
}
string _dbPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal),"myDB.db3");
private async void buttonImport_Clicked(object sender,EventArgs e)
{
var db = new sqliteConnection(_dbPath);
db.CreateTable<Airplane>();
try
{
if (liveryEntry == null)
liveryEntry.Text = "N/A";
if (registrationEntry == null)
registrationEntry.Text = "N/A";
if (airportEntry == null)
airportEntry.Text = "N/A";
if (dateEntry == null)
dateEntry.Text = "N/A";
if (planeEntry.Text != null && airlineEntry.Text != null && liveryEntry.Text != null && registrationEntry.Text != null && dateEntry.Text != null)
{
var url = PhotoPick();
var maxPK = db.Table<Airplane>().OrderByDescending(c => c.Id).FirstOrDefault();
Airplane airplane = new Airplane()
{
Id = (maxPK == null ? 1 : maxPK.Id + 1),Plane = planeEntry.Text,Airline = airlineEntry.Text,Livery = liveryEntry.Text,Registration = registrationEntry.Text,Airport = airportEntry.Text,Date = dateEntry.Text,Comment = commentEntry.Text,Url = await url
};
CreateThumbnail(await url);
db.Insert(airplane);
await displayAlert("Saved",planeEntry.Text + " of " + airlineEntry.Text + " was saved","OK");
planeEntry.Text = "";
airlineEntry.Text = "";
liveryEntry.Text = "";
registrationEntry.Text = "";
airportEntry.Text = "";
dateEntry.Text = "";
commentEntry.Text = "";
}
else
await displayAlert("Fill all needed fields","You have to fill all fields except livery and comment","OK");
}
catch
{
await displayAlert("Error","Select picture again","OK");
}
}
public async Task<string> PhotoPick()
{
var result = await MediaPicker.PickPhotoAsync(new MediaPickerOptions { Title = "Please pick photo!"});
await result.OpenReadAsync();
string url = result.FullPath;
return url;
}
public async void CreateThumbnail(string Path)
{
var bitmap = SKBitmap.Decode(Path);
int h = bitmap.Height;
int w = bitmap.Width;
if (h > 1920 || w > 1920)
{
int rectHeight = 1920;
int rectWidth = 1920;
//aspect ratio calculation
float aspect = w / h;
//new dimensions by aspect ratio
int newWidth = (int)(rectWidth * aspect);
int newHeight = (int)(newWidth / aspect);
//if one of the two dimensions exceed the Box dimensions
if (newWidth > rectWidth || newHeight > rectHeight)
{
//depending on which of the two exceeds the Box dimensions set it as the Box dimension and calculate the other one based on the aspect ratio
if (newWidth > newHeight)
{
newWidth = rectWidth;
newHeight = (int)(newWidth / aspect);
}
else
{
newHeight = rectHeight;
newWidth = (int)(newHeight * aspect);
}
}
var resizedImage = bitmap.Resize(new SKImageInfo(newWidth,newHeight),SKBitmapResizeMethod.lanczos3);
var image = resizedImage.Encode(SKEncodedImageFormat.Jpeg,80);
var path = System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var filepath = Path.Combine(path,"myfilename.ext");
using (var stream = File.OpenWrite(filepath))
{
image.Saveto(stream);
}
await displayAlert(null,"Height is: " + newHeight + " pixels and width is: " + newWidth + " pixels","OK");
}
else
await displayAlert(null,"Height is: " + h + " pixels and width is: " + w + " pixels","OK");
}
}
}
解决方法
将文件名附加到路径
var path = System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var filepath = Path.Combine(path,"myfilename.ext");
using (var stream = File.OpenWrite(filepath))
{
image.SaveTo(stream);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。