converting 24bit color depth image to 1bpp image
-
Hi, I am trying to convert the 24 bit image to 1 bit image. How can I convert it. I had tried the following code and obviously it didn't work. Thank You.
Public Function ConvertTo1bpp(ByVal img As Bitmap) As Bitmap Dim rc As New Rectangle(0, 0, img.Width, img.Height) Dim bmpS As New Bitmap(img.Width, img.Height, PixelFormat.Format1bppIndexed) bmpS.SetResolution(img.HorizontalResolution, img.VerticalResolution) Dim bmpDataSet As BitmapData = bmpS.LockBits(rc, ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed) Dim bmpDataSource As BitmapData = img.LockBits(rc, ImageLockMode.ReadOnly, img.PixelFormat) bmpDataSource.PixelFormat = PixelFormat.Format1bppIndexed img.UnlockBits(bmpDataSource) Return img End Function
-
Hi, I am trying to convert the 24 bit image to 1 bit image. How can I convert it. I had tried the following code and obviously it didn't work. Thank You.
Public Function ConvertTo1bpp(ByVal img As Bitmap) As Bitmap Dim rc As New Rectangle(0, 0, img.Width, img.Height) Dim bmpS As New Bitmap(img.Width, img.Height, PixelFormat.Format1bppIndexed) bmpS.SetResolution(img.HorizontalResolution, img.VerticalResolution) Dim bmpDataSet As BitmapData = bmpS.LockBits(rc, ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed) Dim bmpDataSource As BitmapData = img.LockBits(rc, ImageLockMode.ReadOnly, img.PixelFormat) bmpDataSource.PixelFormat = PixelFormat.Format1bppIndexed img.UnlockBits(bmpDataSource) Return img End Function
This isn't going to work because how does the code know which pixels to turn on and which to turn off?? 1bpp only leaves white and black as your colors, so you need to come up with a cut off point in the original image to make the conversion mean anything. In other words, if the brightness of a color is below a certain threashold, the target pixel should be black, otherwise it's white. This code will not do that for you. 3 seconds worth of Google came up with this[^] C# source to do the conversion your talking about.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
Hi, I am trying to convert the 24 bit image to 1 bit image. How can I convert it. I had tried the following code and obviously it didn't work. Thank You.
Public Function ConvertTo1bpp(ByVal img As Bitmap) As Bitmap Dim rc As New Rectangle(0, 0, img.Width, img.Height) Dim bmpS As New Bitmap(img.Width, img.Height, PixelFormat.Format1bppIndexed) bmpS.SetResolution(img.HorizontalResolution, img.VerticalResolution) Dim bmpDataSet As BitmapData = bmpS.LockBits(rc, ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed) Dim bmpDataSource As BitmapData = img.LockBits(rc, ImageLockMode.ReadOnly, img.PixelFormat) bmpDataSource.PixelFormat = PixelFormat.Format1bppIndexed img.UnlockBits(bmpDataSource) Return img End Function
You can actually create a new 1bit bitmap and draw your 24bit image onto it, if you want to see what sort of conversion you get by default. However, Dave has given you a link if you wanted to control the threshold yourself.
Christian Graus Driven to the arms of OSX by Vista.
-
This isn't going to work because how does the code know which pixels to turn on and which to turn off?? 1bpp only leaves white and black as your colors, so you need to come up with a cut off point in the original image to make the conversion mean anything. In other words, if the brightness of a color is below a certain threashold, the target pixel should be black, otherwise it's white. This code will not do that for you. 3 seconds worth of Google came up with this[^] C# source to do the conversion your talking about.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008Thank You Dave. I had looked at that link and already using it. There is a problem so I had to post this message here. I should be more specified on my question. Here is what i want.. I have color Jpg image file and I converted it to black and white tiff image using colorMatrix. Everything came out good but the tiff image is in 24 bit color depth. I need it to 1 bit color depth and should not change the look of tiff image. I also found out if the tiff image is in floyd-steinberg and it can have grayscal with 1pbb. But I just dont 'know how to do it. Here what i did. 1) convert the jpg to GrayScale() using colorMatrix 2) then use the result image which is black and white with 24 bpp and convert it to 1bpp using the example on this link http://www.wischik.com/lu/programmer/1bpp.html[^] but the image turn all black and white .. no more shadow.. depth like I have before.
-
Thank You Dave. I had looked at that link and already using it. There is a problem so I had to post this message here. I should be more specified on my question. Here is what i want.. I have color Jpg image file and I converted it to black and white tiff image using colorMatrix. Everything came out good but the tiff image is in 24 bit color depth. I need it to 1 bit color depth and should not change the look of tiff image. I also found out if the tiff image is in floyd-steinberg and it can have grayscal with 1pbb. But I just dont 'know how to do it. Here what i did. 1) convert the jpg to GrayScale() using colorMatrix 2) then use the result image which is black and white with 24 bpp and convert it to 1bpp using the example on this link http://www.wischik.com/lu/programmer/1bpp.html[^] but the image turn all black and white .. no more shadow.. depth like I have before.
pnpfriend wrote:
I need it to 1 bit color depth and should not change the look of tiff image. I also found out if the tiff image is in floyd-steinberg and it can have grayscal with 1pbb.
No, that is not true. A 1 bpp image has two colors, black and white. This is greyscale only in that those two colors are the two extremes of the greyscale range. A greyscale image is 8bpp.
pnpfriend wrote:
but the image turn all black and white .. no more shadow.. depth like I have before.
That is exactly what converting to 1bpp will do. EVERY time.
Christian Graus Driven to the arms of OSX by Vista.
-
Thank You Dave. I had looked at that link and already using it. There is a problem so I had to post this message here. I should be more specified on my question. Here is what i want.. I have color Jpg image file and I converted it to black and white tiff image using colorMatrix. Everything came out good but the tiff image is in 24 bit color depth. I need it to 1 bit color depth and should not change the look of tiff image. I also found out if the tiff image is in floyd-steinberg and it can have grayscal with 1pbb. But I just dont 'know how to do it. Here what i did. 1) convert the jpg to GrayScale() using colorMatrix 2) then use the result image which is black and white with 24 bpp and convert it to 1bpp using the example on this link http://www.wischik.com/lu/programmer/1bpp.html[^] but the image turn all black and white .. no more shadow.. depth like I have before.
pnpfriend wrote:
no more shadow.. depth like I have before.
Unless you use some kind of dithering algorithm to get the effect you want, you can't get "shadows" in a single bpp image.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008