In a .NET application, I have images of different formats (such as Format1bppIndexed) stored in PictureBoxes. I can fetch them using the property Image of the boxes.
Then I need to convert them to the Bitmap class to access the internal data. (The Image class does not support LockBits, as Bitmap does.)
Now to transfer the Image to a Bitmap, you need the Bitmap constructor taking an Image argument. The trouble starts here: this constructor converts the image to the format Format32bppRgb, whatever the initial type, and there is no overload of the constructor that allows forcing the format.
Worse, when the format is Format16bppGrayScale, a System.ArgumentException is raised by the constructor.
But I want to access the raw data without intermediate conversions, including 16 bits images. Any workaround ?
After more investigation, I observe that the support for 16 bits images is really poor under .NET. In particular, you cannot display them with DrawImage.
Some details about your request are not compeletely clear to me, so I am taking some assumptions.
Simple solution might be to create a "shadow object" that supports what you need and let the
PictureBoxdisplay processed result. Any operations you would perform on theimageinPictureBoxwould, in reality, be applied to your shadow object.But I'm not sure, if it can be applied to this particular scenario. It mostly depends on why you need to put pixels into memmory using
LockBits. It may indicate you're gaining speed and with a shadow object you might be back to square one or worse.