我想设置十六进制字符串以0x
开头的<Image>
控制来源的以下格式。资源中的数据是:
var myImageData = "0x89504E470D0A1A0A0000000D0F910000000467414D410....."
我使用以下方法转换为字节数组myImageData:
static byte[] GetBytes(string data)
{
var bytes = new byte[data.Length * sizeof(char)];
System.Buffer.BlockCopy(data.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
使用以下方法创建图像源:
private static BitmapImage LoadImage(byte[] imageData)
{
if (imageData == null || imageData.Length == 0) return null;
var image = new BitmapImage();
using (var mem = new MemoryStream(imageData))
{
mem.Position = 0;
image.BeginInit();
image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = null;
image.StreamSource = mem;
image.EndInit();
}
image.Freeze();
return image;
}
在加载窗口中:
var imageDataBytes = GetBytes(myImageData);
MyImage.Source = LoadImage(imageDataBytes);
但是行image.EndInit();
No imaging component suitable to complete this operation was found.# InnerException : The component cannot be found. (Exception from HRESULT: 0x88982F50)