Hi Drew,
I think all of the code you need is in the sample but since the whitepaper
is still being processed for publishing I will explain it a bit here:
What happens in the sample is a Bitmap object is drawn onto a panel control.
Then the control is used to create a DIB (device independant bitmap) section
and the pixels are accessed. This data is then used to construct a .bmp
file which is saved.
It sounds like what you need an understanding of is how the pixels are
accessed, so I will explain that code in some detail - see the
CopyImageSource function for the actual code.
The first step is to access the device context associated with the control
that has the image on it and use that to create a compatible device context
in memory which will be used to access the pixels. You can get hwnd using
GetCapture or FindWindow - see the FAQ or sample code for more details.
IntPtr hdc = Windows.GetDC(hwnd);
IntPtr hdcComp = Windows.CreateCompatibleDC(hdc);
The next step is to create a BITMAPINFOHEADER that will specify the pixel
data format to CreateDIBSection. The infoHdr.Store function takes care of
filling the byte stream with the header info.
byte[] dummyBitmapInfo = new byte[52];
MemoryStream msDummy = new MemoryStream(dummyBitmapInfo);
BinaryWriter bwDummy = new BinaryWriter(msDummy);
infoHdr.Store(bwDummy, true);
The next set of code has to be placed in an unsafe code block because we
have a pointer to a pointer that we access.
We access the BITMAPINFOHEADER in the byte stream with a pointer, as well as
the destination of the pixel data. This is where you could provide your own
"bytes" array and simply set pPixelDest to &bytes[0] if you are only
interested in the pixel data. In this sample, pPixelDest is offset into the
.bmp file.
fixed (byte* pBitmapInfo = &dummyBitmapInfo[0], pPixelDest =
&bytes[fileHdr.bfOffBits])
Of course, pPixelSource is the source from which we will get the pixel data.
byte* pPixelSource;
Now we are going to create a DIB section from our control's device context.
This will assign pPixelSource to point at the pixel data.
IntPtr hDibSect = Windows.CreateDIBSection(hdc, pBitmapInfo,
Windows.DIB_RGB_COLORS, &pPixelSource, (IntPtr)0, (uint)0);
Select the DIB section into our memory device context.
IntPtr hbmOld = Windows.SelectObject(hdcComp, hDibSect);
Next we copy the pixel data from the control's device context to the newly
created memory device context
Windows.BitBlt(hdcComp, 0, 0, infoHdr.biWidth, infoHdr.biHeight, hdc, 0, 0,
Windows.SRCCOPY);
Now we copy the pixel data into our destination pointer which is the byte
array - at this point we have the pixel data in the correct place so this is
probably what you are looking to do. I believe you could now access
pPixelDest and modify the pixels, then copy them back into pPixelSource.
Windows.CopyMemory(pPixelDest, pPixelSource, (int)fileHdr.sizeOfImageData);
The rest of the code is clean-up
Windows.SelectObject(hdcComp, hbmOld);
Windows.DeleteObject(hDibSect);
bwDummy.Close();
msDummy.Close();
Windows.DeleteDC(hdcComp);
Windows.ReleaseDC(hwnd, hdc);
I hope this helps.
--
Geoff Schwab
Program Manager
Excell Data Corporation
http://msdn.com/mobility
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/default.aspx
This posting is provided "AS IS" with no warranties, and confers no rights.
Drew said:
I want to access the pixels, modify them, and then store them in a different
Bitmap object
or just display the new image on the screen.
Basically, I'm hoping to do some image manipulation.
In Java, I could use the PixelGrabber class to "grab" a predetermined
set
of
pixels
from the image and store them in a byte array.
I think that's all I'm looking for at this point...
Drew
access
the
http://msdn.microsoft.com/mobility/.../prodtechinfo/devtools/netcf/FAQ/default.aspx