WIC Questions

Dec 12, 2011 at 3:08 PM

Hello I'm trying to get the pixels of a Bitmap
I am using the following code:

        public byte[] GetPixels(Bitmap bmp)
        {

            var wic = this.ImagingFactory.CreateBitmap((int)bmp.Size.Width, (int)bmp.Size.Height, WicPixelFormats.PixelFormat32bppPBGRA, BitmapCreateCacheOption.CacheOnLoad);
            this._wicRenderTarget = this._factory.CreateWicBitmapRenderTarget(wic, new RenderTargetProperties());
            this._wicRenderTarget.BeginDraw();
            this._wicRenderTarget.DrawBitmap(bmp, new RectF(0, 0, bmp.Size.Width, bmp.Size.Height), 1f, BitmapInterpolationMode.Linear);
            this._wicRenderTarget.EndDraw();
            var rect = new WicRect();
            rect.Width = (int)bmp.Size.Width;
            rect.Height = (int)bmp.Size.Height;
            rect.X = 0;
            rect.Y = 0;
            byte[] buffer = new byte[32 * (rect.Width * rect.Height)];
            wic.CopyPixels(rect, 32, buffer);
            return buffer;

        }
But that did not work, what better way to do this?

I also
wonder if it is possible to adjust an RGB Bitmap.

Sorry my bad English, I await answers, Thanks!

Dec 12, 2011 at 5:57 PM

I managed to get the pixels = D
I added the following method in class WicBitmapSource.cpp:
   

array<Byte>^ WicBitmapSource::CopyPixels()
{
    WICRect rect = { 0, 0, Size.Width, Size.Height };
    UINT stride = rect.Width * 4;
    UINT bufferSize = stride * rect.Height;
   
    array<Byte>^ managedBuffer = gcnew array<Byte>( bufferSize );
    pin_ptr<Byte> buffer = &managedBuffer[0];

   ComUtils::CheckResult(GetNative()->CopyPixels((WICRect*)&rect, stride, managedBuffer->Length, (BYTE*)buffer));

    return managedBuffer;
}

and manipulate the RGB using GDI, after converting the Stream to WIC :)

Thanks for all!