首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将BitmapImage转换为StorageFile?

如何将BitmapImage转换为StorageFile?
EN

Stack Overflow用户
提问于 2020-06-30 03:48:25
回答 1查看 34关注 0票数 0

虽然拖放的图像,我需要转换为StorageFile的BitmapImage上传它。

代码语言:javascript
复制
       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);
           }
EN

回答 1

Stack Overflow用户

发布于 2020-06-30 10:23:57

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

代码语言:javascript
复制
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
}

谢谢。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62645676

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档