Search code examples
c#wpfmemorystream

No imaging component suitable to complete this operation was found


Herein is my piece of code but I've got an error

No imaging component suitable to complete this operation was found

in "EndInit()" line.

I've read WIC registry modification (NB: my OS is Windows 7 and my IDE is VS 2010) but it did not work at all :(

I was wondering if you let me know what do I do to solve it.

BitmapImage myBitmapImage = new BitmapImage();


byte[] DM = new byte[307200];

for (int i = 0; i < 640; i++)
    for (int j = 0; j < 480; j++)
        if (i < 500)
            DM[i + j] = i;

using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(DM))
{
    myBitmapImage.BeginInit();
    myBitmapImage.StreamSource = memoryStream;
    myBitmapImage.DecodePixelWidth = 640;
    myBitmapImage.EndInit();

}

img.Source = myBitmapImage;

Solution

  • Well, you cannot just pass in an array of pixel data; there is no way the class can know what length and height, what channels and what color depth the image is supposed to have. You need to provide a valid header for the supported formats, e.g. bmp, jpg or png. There are some others, a listing exists somewhere on MSDN.

    Since you want to create a bitmap from scratch you might want to use a WritableBitmap instead.

    (By the way, DecodePixelWidth does not what you presumably think it does; it just scales the picture down after decoding it to save memory)