本文介绍了[UWP] [C#] BitmapDecoder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 我正在寻找一种将字符串转换为BitmapDecoder的方法。 我用以下代码做得不好。 (Windows.Graphics.Imaging.BitmapDecoder.CreateAsync中发生异常。) 字符串文本="你好2017/01/25!"; byte [] bytes = System.Text.Encoding.GetEncoding(" UTF-8")。GetBytes(text); using(Windows.Storage.Streams.InMemoryRandomAccessStream mStream = new Windows.Storage.Streams.InMemoryRandomAccessStream()) using(Windows.Storage.Streams.DataWriter writer = new Windows.Storage。 Streams.DataWriter(mStream)) { writer.WriteBytes(bytes); await writer.StoreAsync(); mStream.Seek(0); var bimapDecoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(mStream); } 解决方案 Hello y-ske, 为什么你要尝试像这样使用bimapDecoder?这里的BimapDecoder需要图像流而不是普通的文本流。 例如,如果我改为使用这样的代码: protected async override void OnNavigatedTo( NavigationEventArgs e) { base.OnNavigatedTo(e); byte [] fileBytes; StorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation; StorageFile file = await folder.GetFileAsync(" 1.jpg"); using(IRandomAccessStreamWithContentType stream = await file.OpenReadAsync()) { fileBytes = new byte [stream.Size]; using(DataReader reader = new DataReader(stream)) { await reader.LoadAsync((uint)stream.Size); reader.ReadBytes(fileBytes); } } 使用(Windows.Storage.Streams.InMemoryRandomAccessStream mStream = new Windows.Storage.Streams.InMemoryRandomAccessStream()) using(Windows.Storage。 Streams.DataWriter writer = new Windows.Storage.Streams.DataWriter(mStream)) { writer.WriteBytes(fileBytes); await writer.StoreAsync(); mStream.Seek(0); var bimapDecoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(mStream); } } 它没有问题。但是如果我将源字节数组从图像更改为文本流,它将直接报告此类错误。我相信它与"BitmapDecoder.CreateAsync"有关。此方法可能会尝试从流中读取以搜索所需的图像标记。 (您可以在线搜索位图文件格式,你会发现更多信息) 祝你好运, Barry I'm looking for a way to convert a string to BitmapDecoder.I could not do it well with the code below.(An exception occurs in Windows.Graphics.Imaging.BitmapDecoder.CreateAsync.)string text = "Hello 2017/01/25 !";byte[] bytes = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(text);using (Windows.Storage.Streams.InMemoryRandomAccessStream mStream = new Windows.Storage.Streams.InMemoryRandomAccessStream())using (Windows.Storage.Streams.DataWriter writer = new Windows.Storage.Streams.DataWriter(mStream)){ writer.WriteBytes(bytes); await writer.StoreAsync(); mStream.Seek(0); var bimapDecoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(mStream);} 解决方案 Hello y-ske,Why you are trying to use bimapDecoder like this? The BimapDecoder here need a image stream not a common text stream.For example, if I change to use some code like this: protected async override void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); byte[] fileBytes; StorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation; StorageFile file=await folder.GetFileAsync("1.jpg"); using (IRandomAccessStreamWithContentType stream = await file.OpenReadAsync()) { fileBytes = new byte[stream.Size]; using (DataReader reader = new DataReader(stream)) { await reader.LoadAsync((uint)stream.Size); reader.ReadBytes(fileBytes); } } using (Windows.Storage.Streams.InMemoryRandomAccessStream mStream = new Windows.Storage.Streams.InMemoryRandomAccessStream()) using (Windows.Storage.Streams.DataWriter writer = new Windows.Storage.Streams.DataWriter(mStream)) { writer.WriteBytes(fileBytes); await writer.StoreAsync(); mStream.Seek(0); var bimapDecoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(mStream); }}It works without problem. But if I change the source byte array from the image to your text stream, it will directly report this kind of error. I believe it is related to "BitmapDecoder.CreateAsync" this method may trying to read from the stream to search for required image tags. (You may search bitmap file format online and you will find more info)Best regards,Barry 这篇关于[UWP] [C#] BitmapDecoder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-07 00:16