• Welcome to Jose's Read Only Forum 2023.
 

GDI+: GdipSetImageAttributesOutputChannel

Started by José Roca, June 30, 2008, 04:51:22 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca



The following example creates an Image object and calls the GdipDrawImageRectI function to draw the image. Then the code creates an ImageAttributes object and sets its bitmap output channel to cyan (ColorChannelFlagsC). The code calls GdipDrawImageRectI a second time, passing the address of the Image object and the address of the ImageAttributes object. The cyan channel of each pixel is calculated, and the rendered image shows the intensities of the cyan channel as shades of gray. The code calls GdipDrawImageRectI three more times to show the intensities of the magenta, yellow, and black channels.

C++


VOID Example_SetOutputChannel(HDC hdc)
{
   Graphics graphics(hdc);

   Image image(L"Mosaic2.bmp");

   // Draw the image unaltered.
   graphics.DrawImage(&image, 10, 10, width, height);

   UINT width = image.GetWidth();
   UINT height = image.GetHeight();

   ImageAttributes imAtt;

   // Draw the image, showing the intensity of the cyan channel.
   imAtt.SetOutputChannel(ColorChannelFlagsC, ColorAdjustTypeBitmap);
   graphics.DrawImage(
      &image,
      Rect(110, 10, width, height),  // dest rect
      0, 0, width, height,           // source rect
      UnitPixel,
      &imAtt);

   // Draw the image, showing the intensity of the magenta channel.
   imAtt.SetOutputChannel(ColorChannelFlagsM, ColorAdjustTypeBitmap);
   graphics.DrawImage(
      &image,
      Rect(210, 10, width, height),  // dest rect
      0, 0, width, height,           // source rect
      UnitPixel,
      &imAtt);

   // Draw the image, showing the intensity of the yellow channel.
   imAtt.SetOutputChannel(ColorChannelFlagsY, ColorAdjustTypeBitmap);
   graphics.DrawImage(
      &image,
      Rect(10, 110, width, height),  // dest rect
      0, 0, width, height,           // source rect
      UnitPixel,
      &imAtt);

   // Draw the image, showing the intensity of the black channel.
   imAtt.SetOutputChannel(ColorChannelFlagsK, ColorAdjustTypeBitmap);
   graphics.DrawImage(
      &image,
      Rect(110, 110, width, height),  // dest rect
      0, 0, width, height,            // source rect
      UnitPixel,
      &imAtt);
}


PowerBASIC


SUB GDIP_SetImageAttributesOutputChannel (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.
   strFileName = UCODE$("Mosaic2.bmp")
   hStatus = GdipLoadImageFromFile(STRPTR(strFileName), pImage)

   ' // Draw the image unaltered.
   hStatus = GdipGetImageWidth(pImage, nWidth)
   hStatus = GdipGetImageHeight(pImage, nHeight)
   hStatus = GdipDrawImageRectI(pGraphics, pImage, 10, 10, nWidth, nHeight)

   ' // Create an ImageAttributes object
   hStatus = GdipCreateImageAttributes(pImgAttr)

   ' // Draw the image, showing the intensity of the cyan channel.
   hStatus = GdipSetImageAttributesOutputChannel(pImgAttr, %ColorAdjustTypeBitmap, %TRUE, %ColorChannelFlagsC)
   hStatus = GdipDrawImageRectRectI(pGraphics, pImage, _
             110, 10, nWidth, nHeight, _   ' dest rect
             0, 0, nWidth, nHeight, _      ' source dest
             %UnitPixel, pImgAttr, %NULL, %NULL)

   ' // Draw the image, showing the intensity of the magenta channel.
   hStatus = GdipSetImageAttributesOutputChannel(pImgAttr, %ColorAdjustTypeBitmap, %TRUE, %ColorChannelFlagsM)
   hStatus = GdipDrawImageRectRectI(pGraphics, pImage, _
             210, 10, nWidth, nHeight, _   ' dest rect
             0, 0, nWidth, nHeight, _      ' source dest
             %UnitPixel, pImgAttr, %NULL, %NULL)

   ' // Draw the image, showing the intensity of the yellow channel.
   hStatus = GdipSetImageAttributesOutputChannel(pImgAttr, %ColorAdjustTypeBitmap, %TRUE, %ColorChannelFlagsY)
   hStatus = GdipDrawImageRectRectI(pGraphics, pImage, _
             10, 110, nWidth, nHeight, _   ' dest rect
             0, 0, nWidth, nHeight, _      ' source dest
             %UnitPixel, pImgAttr, %NULL, %NULL)

   ' // Draw the image, showing the intensity of the black channel.
   hStatus = GdipSetImageAttributesOutputChannel(pImgAttr, %ColorAdjustTypeBitmap, %TRUE, %ColorChannelFlagsK)
   hStatus = GdipDrawImageRectRectI(pGraphics, pImage, _
             110, 110, 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.