【问题标题】:System.Drawing error on Save image from byte dataSystem.Drawing 错误从字节数据保存图像
【发布时间】:2019-10-18 20:40:38
【问题描述】:

在 UWP 应用程序中将字节数据转换为图像时出错

试图从数据中获取流并将其转换为图像但错误!

错误是:

此平台不支持system.drawing

 videoParser.Initialize(delegate (byte[] data)
 {
       using (MemoryStream mStream = new MemoryStream(data))
       {
           System.Drawing.Image img = System.Drawing.Image.FromStream(mStream);
           img.Save(@"D:/img.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
       }
       return DJISDKManager.Instance.VideoFeeder.ParseAssitantDecodingInfo(0, data);
 });

我想把图片保存为jpeg格式

【问题讨论】:

标签: c# uwp-xaml


【解决方案1】:

您将需要使用 IFormFile

  private string SaveImage(Guid AdID, IFormFile Photo)
    {
        if (Photo != null)
        {
            string uploadFolder = Path.Combine(hostingEnvironment.WebRootPath, "AdsImages");
            string UniqueFileName = AdID.ToString() + "_" + Photo.FileName;
            string FilePath = Path.Combine(uploadFolder, UniqueFileName);
            Photo.CopyTo(new FileStream(FilePath, FileMode.Create));
            return UniqueFileName;
        }
        else
        {
            return null;
        }
    }

【讨论】:

  • 这里的字节[]数据在哪里使用?
【解决方案2】:

从@Amy 共享的链接看来,System.Drawing 似乎不适用于通用 Windows 应用程序。如果你只想从数据中保存图像,你可以先创建一个文件,然后在其中写入字节。

StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(@"D:\");
StorageFile file = await folder.CreateFileAsync("img.jpg", CreationCollisionOption.ReplaceExisting);
await Windows.Storage.FileIO.WriteBytesAsync(file, data);

【讨论】:

  • 我在 GetFolderFromPathAsync 上收到 System.UnauthorizedAccessException ..
  • 当您尝试通过路径访问文件夹时,您是否在设置>隐私>文件系统中添加了broadFileSystemAccess功能并配置了访问权限以允许您的应用访问文件系统?
  • 如果你想提交一个应用到商店,broadFileSystemAccess 确实不是一个好的选择,它会被严格审查。您可以使用 FileSavePicker 创建文件,它会打开一个类似于文件资源管理器的对话框,然后您可以从您想要的位置创建文件。或者您可以将文件保存在 localFolder(ApplicationData.Current.LocalFolder.CreateFileAsync("img.jpg", CreationCollisionOption.ReplaceExisting);) 中。
猜你喜欢
  • 2020-09-15
  • 2012-12-16
  • 1970-01-01
  • 2017-08-18
  • 2014-12-07
  • 1970-01-01
  • 1970-01-01
  • 2019-04-16
  • 2012-12-24
相关资源
最近更新 更多