It can be easily done. I did not know how to use encoder. IWICBitmapFrameEncode.WritePixels
is where I should put raw pixels:
factory = CreateComObject(CLSID_WICImagingFactory);
IWICBitmapEncoder pngEncoder = factory.CreateEncoder(GUID_ContainerFormatPng, null);
pngEncoder.Initialize(targetStream, WICBitmapEncoderNoCache);
IPropertyBag2 propertyBag;
IWICBitmapFrameEncode frame = pngEncoder.CreateNewFrame(ref propertyBag);
frame.Initialize(propertyBag);
//Set the size of the image
frame.SetSize(640, 480);
//Request the encoder use this format.
Guid format = GUID_WICPixelFormat32bppPBGRA;
//If the encoder doesn't support that format, it will set format to the closest it does support
frame.SetPixelFormat(ref format);
//if the frame encoder doesn't support the pixel format we want to give it
//then we just cannot proceed
Assert(format == GUID_WICPixelFormat32bppPBGRA);
frame.WritePixels(
lineCount,
stride,
bufferSize,
pointerToPixelData);
frame.Commit();
pngEncoder.Commit();