Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Any one have idea about how to handle 16 bit gray scale images in .net consider this two simple code:
VB
Dim Bitmap As New Bitmap(100, 100)
       Dim BitmapData As BitmapData = Bitmap.LockBits(New Rectangle(0, 0, Bitmap.Width, Bitmap.Height), ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale)

when you debug this code this error will happen:

Parameter is not valid.

Is there any other way to handle 16 bit gray scale image in .net?
Attention please I know I can use 48bpp instead it but my consider in the speed of my application specially when change contrast/brightness, for a 3072*3072 image the process of changing contrast/brightness is around 0.7 second and it is clear that it is not suitable for real time. Please give another solution.
Posted
Updated 6-Jul-15 1:38am
v2
Comments
Kornfeld Eliyahu Peter 6-Jul-15 7:35am    
Format16bppGrayScale?
mehdishahgholi 6-Jul-15 7:38am    
sorry I update the question

1 solution

The pixel format in LockBits must be compatible with the format of the bitmap you want to lock...A simple new Bitmap(w,h) with no additional parameters will create a 32 bit RGB image, and it can not be locked as 16 bit gray-scale!!!
Use the constructor variant that can get pixel format too: https://msdn.microsoft.com/en-us/library/3z132tat(v=vs.110).aspx[^]
VB
Dim Bitmap As New Bitmap(100, 100, PixelFormat.Format16bppGrayScale)
 
Share this answer
 
v2
Comments
mehdishahgholi 7-Jul-15 1:24am    
@Kornfeld Eliyahu Peter: Dear Peter
Yes I know I try this before:
Dim Bitmap As New Bitmap(100, 100, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale)
Dim a As Integer = System.Drawing.Imaging.PixelFormat.Format16bppGrayScale
Dim BitmapData As BitmapData = Bitmap.LockBits(New Rectangle(0, 0, Bitmap.Width, Bitmap.Height), ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale)
Dim GrayLevelMap(100 * 100 - 1) As Short
Dim R As New Random
For i = 0 To GrayLevelMap.GetUpperBound(0)
GrayLevelMap(i) = R.Next(0, 32768)
Next
Marshal.CleanupUnusedObjectsInCurrentContext()
Marshal.Copy(GrayLevelMap, 0, BitmapData.Scan0, GrayLevelMap.Length)
Bitmap.UnlockBits(BitmapData)
PictureBox2.Image = Bitmap
but picturebox show a white box with a red x on it !!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900