从字节数据保存图像时出现系统绘图错误

问题描述 投票:0回答:2

在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
2个回答
0
投票

您将需要使用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;
        }
    }

0
投票

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

StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(@"D:\");
StorageFile file = await folder.CreateFileAsync("img.jpg", CreationCollisionOption.ReplaceExisting);
await Windows.Storage.FileIO.WriteBytesAsync(file, data);
© www.soinside.com 2019 - 2024. All rights reserved.