How to set string value to wpf image source?

0

I want to set hex string The following format that start with 0x to source of <Image> control. Data in resource is :

var myImageData = "0x89504E470D0A1A0A0000000D0F910000000467414D410....."

I used the following method to convert to byte array 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;
}

And use the following method to create image source :

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;
}

In load window :

var imageDataBytes = GetBytes(myImageData);
MyImage.Source = LoadImage(imageDataBytes); 

But has error in line image.EndInit();

No imaging component suitable to complete this operation was found.# InnerException : The component cannot be found. (Exception from HRESULT: 0x88982F50)

c#
wpf
asked on Stack Overflow May 23, 2015 by Mohsen

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0