如何将位图图像转换为存储文件?



在拖放图像时,我需要将BitmapImage转换为StorageFile才能上传。

DataPackageView dataPackageView = Clipboard.GetContent();
if (dataPackageView.Contains(StandardDataFormats.Bitmap))
{
RandomAccessStreamReference img = await dataPackageView.GetBitmapAsync();
var imgstream = await img.OpenReadAsync();
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(imgstream);
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(bitmap.UriSource.ToString()));
IUploadFileModel   _uploadModel = ObjFactory.Instance.CreateUploadFileModel();
_uploadModel.UploadFileAsync(file, user, OpenChatData.TeamID.ToString(), _isReadReceiptEnabled);
}

不必将从剪贴板获取的数据转换为BitmapImage,可以直接操作获取的RandomAccessStreamReference,保存到本地文件,上传文件

var data = Clipboard.GetContent();
if (data.Contains(StandardDataFormats.Bitmap))
{
var img = await data.GetBitmapAsync();
var imgstream = await img.OpenReadAsync();
string type = imgstream.ContentType;
string extension = type.Replace("image/", "");
var file = await ApplicationData.Current.LocalFolder.CreateFileAsync($"temp.{extension}", CreationCollisionOption.ReplaceExisting);
byte[] buffer = new byte[imgstream.Size];
await imgstream.ReadAsync(buffer.AsBuffer(), (uint)imgstream.Size, InputStreamOptions.None);
await FileIO.WriteBytesAsync(file, buffer);
// upload file
}

谢谢。

相关内容

  • 没有找到相关文章

最新更新