One method (requiring the a small number of API calls, and with very limited 'intelligence' concerning the conversion) would be to BitBlit an image from a color device context to a monochrome device context ...
Strongm,
I was also working on this idea. But I realized that this method has limitations. The color on the bitmap that matches the background color of the source DC is painted as white. All other colors are painted as black.
I think thats why you say 'very limited intelligence'...
This technique is good in color masking. But not for color to monochorme conversion.
I remember there was a thread in which we posted code to convert a color image to grayscale. I used SetColorAdjustment function, and you posted GDI+ code. But I cannot find that thread.
Yep. I didn't want to get onto the more complicated versions straight away.
And I can't find outr old threads either, s0 here's my version using ColorAdjust...
Option Explicit
Private Declare Function GetColorAdjustment Lib "gdi32" (ByVal hdc As Long, lpca As COLORADJUSTMENT) As Long
Private Declare Function SetColorAdjustment Lib "gdi32" (ByVal hdc As Long, lpca As COLORADJUSTMENT) As Long
Private Declare Function SetStretchBltMode Lib "gdi32" (ByVal hdc As Long, ByVal nStretchMode As Long) As Long
Private Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
Private Type COLORADJUSTMENT
caSize As Integer
caFlags As Integer
caIlluminantIndex As Integer
caRedGamma As Integer
caGreenGamma As Integer
caBlueGamma As Integer
caReferenceBlack As Integer
caReferenceWhite As Integer
caContrast As Integer
caBrightness As Integer
caColorfulness As Integer
caRedGreenTint As Integer
End Type
Private Const HALFTONE = 4
Private Sub Picture1_Click()
Dim ca As COLORADJUSTMENT
With Picture1
.AutoRedraw = True
.ScaleMode = vbPixels
SetStretchBltMode .hdc, HALFTONE
GetColorAdjustment .hdc, ca
ca.caColorfulness = -100 'No colors!
SetColorAdjustment .hdc, ca
StretchBlt .hdc, 0, 0, .ScaleWidth, .ScaleHeight, .hdc, 0, 0, .ScaleWidth, .ScaleHeight, vbSrcCopy
.Refresh
End With
End Sub
after loading the code i keep getting error warnings saying
"only coments may appear after end sub end function or end property" and the seconbd line of code declaring 'Private Declare Function getcoloradjustment Lib "gdi32" (ByVal hdc As Long, ipac As coloradjustment) As Long' is highlighted am i missing something
i have move the option explicit and private declare function into the general part of the form but now im only getting user defined type not defined any idea on the cause would be great help
it converted the image to gray scale .thanks alot for your help i guess i should have been able to see were the warning were coming from myself in hindsight
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.