在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格式保存图像
您将需要使用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;
}
}
从@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);