Create or Save Image as Black and White

Just starting out? Need help? Post your questions and find answers here.
Xombie
Addict
Addict
Posts: 898
Joined: Thu Jul 01, 2004 2:51 am
Location: Tacoma, WA
Contact:

Create or Save Image as Black and White

Post by Xombie »

In the past I've used...

Code: Select all

TestImage = CreateImage(#PB_Any, 2550, 4200, 1)
hdc = CreateCompatibleDC_(0)
SelectObject_(hdc, ImageID(TestImage))
SetDIBColorTable_(hdc, 0, 2, @palette())
DeleteDC_(hdc) : hdc = 0
...to create a black and white image and it's worked fine. What's the recommended method for PB 4.50?
Thorium
Addict
Addict
Posts: 1305
Joined: Sat Aug 15, 2009 6:59 pm

Re: Create or Save Image as Black and White

Post by Thorium »

4.50 does not support indexed images any longer.
But it supports to save them, so i think the solution is to just use 24bit image and just use black and white, when you save it, you specify it as 1bit.
Xombie
Addict
Addict
Posts: 898
Joined: Thu Jul 01, 2004 2:51 am
Location: Tacoma, WA
Contact:

Re: Create or Save Image as Black and White

Post by Xombie »

I'm using GDI+ to save the image (multipage TIF) so I'll dig around on that to see how to save it as bitonal.

Thanks!
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Create or Save Image as Black and White

Post by srod »

You can use api to create a monochrome dibsection easily enough...

Code: Select all

hdc = CreateCompatibleDC_(0)
If hdc
  With bmi.BITMAPINFO
    \bmiHeader\biSize = SizeOf(BITMAPINFOHEADER) 
    \bmiHeader\biWidth = 300
    \bmiHeader\biHeight   = 300
    \bmiHeader\biPlanes   = 1 
    \bmiHeader\biBitCount = 1
  EndWith 
  hBMP = CreateDIBSection_(hdc, @bmi, #DIB_RGB_COLORS, 0, 0, 0) 
  If hBMP
    oldimage = SelectObject_(hdc, hBMP)
    Dim colors.l(1)
      colors(0) = #Black
      colors(1) = #White 
    SetDIBColorTable_(hdc, 0, 2, @Colors())
    SelectObject_(hdc, oldimage)
  EndIf
  DeleteDC_(hdc)
EndIf
hBMP gives you an image handle which you can use in image gadgets etc.

Alternatively use CreateBitmap_() to create a monochrome ddb. This of course will have no color table.
I may look like a mule, but I'm not a complete ass.
Fred
Administrator
Administrator
Posts: 18247
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Create or Save Image as Black and White

Post by Fred »

You can specify the 'Depth' in SaveImage() in 4.50, it should do the trick (don't use dithering if you only use 2 colors in your 24 bit image).
Xombie
Addict
Addict
Posts: 898
Joined: Thu Jul 01, 2004 2:51 am
Location: Tacoma, WA
Contact:

Re: Create or Save Image as Black and White

Post by Xombie »

And for GDI+ I simply did the following...

Code: Select all

EncoderParameters\Count = 2
;
CopyMemory(?EncoderSaveFlag, @EncoderParameters\Parameter[0]\Guid, SizeOf(GUID))
EncoderParameters\Parameter[0]\type = 4
EncoderParameters\Parameter[0]\NumberOfValues = 1
EncoderParameters\Parameter[0]\Value = @ParameterValue
;
ColorDepth = 1
;
CopyMemory(?EncoderColorDepth, @EncoderParameters\Parameter[1]\Guid, SizeOf(GUID))
EncoderParameters\Parameter[1]\type = 4
EncoderParameters\Parameter[1]\NumberOfValues = 1
EncoderParameters\Parameter[1]\Value = @ColorDepth
Where EncoderColorDepth is...

Code: Select all

EncoderColorDepth:
  Data.l $66087055
  Data.w $AD66, $4C7C
  Data.b $9A, $18, $38, $A2, $31, $0B, $83, $37
Post Reply