The following example creates an
Image object based on a .bmp file. The code also creates an
ImageAttributes object and sets its bitmap threshold value to 0.6. Then the code draws the image twice: once with no color adjustment and once with the adjustment specified by the threshold.
C++
VOID Example_SetThreshold(HDC hdc)
{
Graphics graphics(hdc);
// Create an Image object based on a .bmp file.
// The image has one stripe with RGB components (160, 0, 0)
// and one stripe with RGB components (0, 140, 0).
Image image(L"RedGreenThreshold.bmp");
// Create an ImageAttributes object, and set its bitmap threshold to 0.6.
ImageAttributes imAtt;
imAtt.SetThreshold(0.6f, ColorAdjustTypeBitmap);
// Draw the image with no color adjustment.
graphics.DrawImage(&image, 10, 10, image.GetWidth(), image.GetHeight());
// Draw the image with the threshold applied.
// 160 > 0.6*255, so the red stripe will be changed to full intensity.
// 140 < 0.6*255, so the green stripe will be changed to zero intensity.
graphics.DrawImage(&image,
Rect(100, 10, image.GetWidth(), image.GetHeight()), // dest rect
0, 0, image.GetWidth(), image.GetHeight(), // source rect
UnitPixel,
&imAtt);
}
PowerBASIC
SUB GDIP_SetImageAttributesThreshold (BYVAL hdc AS DWORD)
LOCAL hStatus AS LONG
LOCAL pGraphics AS DWORD
LOCAL pImage AS DWORD
LOCAL pImgAttr AS DWORD
LOCAL strFileName AS STRING
LOCAL nWidth AS LONG
LOCAL nHeight AS LONG
hStatus = GdipCreateFromHDC(hdc, pGraphics)
' // Create an Image object based on a .bmp file.
' // The image has one stripe with RGB components (160, 0, 0)
' // and one stripe with RGB components (0, 140, 0).
strFileName = UCODE$("RedGreenThreshold.bmp")
hStatus = GdipLoadImageFromFile(STRPTR(strFileName), pImage)
' // Create an ImageAttributes object, and set its bitmap threshold to 0.6.
hStatus = GdipCreateImageAttributes(pImgAttr)
hStatus = GdipSetImageAttributesThreshold(pImgAttr, %ColorAdjustTypeBitmap, %TRUE, 0.6!)
hStatus = GdipGetImageWidth(pImage, nWidth)
hStatus = GdipGetImageHeight(pImage, nHeight)
' // Draw the image with no color adjustment.
hStatus = GdipDrawImageRectI(pGraphics, pImage, 10, 10, nWidth, nHeight)
' // Draw the image with the threshold applied.
' // 160 > 0.6*255, so the red stripe will be changed to full intensity.
' // 140 < 0.6*255, so the green stripe will be changed to zero intensity.
hStatus = GdipDrawImageRectRectI(pGraphics, pImage, _
100, 10, nWidth, nHeight, _ ' dest rect
0, 0, nWidth, nHeight, _ ' source dest
%UnitPixel, pImgAttr, %NULL, %NULL)
' // Cleanup
IF pImgAttr THEN GdipDisposeImageAttributes(pImgAttr)
IF pImage THEN GdipDisposeImage(pImage)
IF pGraphics THEN GdipDeleteGraphics(pGraphics)
END SUB
The following illustration shows the output of the preceding code.
(http://www.jose.it-berater.org/captures/GdipSetImageAttributesThreshold.png)