• Welcome to Jose's Read Only Forum 2023.
 

GDI+: GdipBitmapSetPixel

Started by José Roca, June 22, 2008, 12:37:05 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca



The following example creates a Bitmap based on a JPEG file. The code draws the bitmap once unaltered. Then the code calls the GdipBitmapSetPixel function to create a checkered pattern of black pixels in the bitmap and draws the altered bitmap.

C++


VOID Example_SetPixel(HDC hdc)

{
   Graphics graphics(hdc);

   // Create a Bitmap object from a JPEG file.
   Bitmap myBitmap(L"Climber.jpg");

   // Draw the bitmap.
   graphics.DrawImage(&myBitmap, 0, 0);

   // Create a checkered pattern with black pixels.
   for (UINT row = 0; row < myBitmap.GetWidth(); row += 2)
   {
      for (UINT col = 0; col < myBitmap.GetHeight(); col += 2)
      {
         myBitmap.SetPixel(row, col, Color(255, 0, 0, 0));
      }
   }

   // Draw the altered bitmap.
   graphics.DrawImage(&myBitmap, 200, 0);
}


PowerBASIC


SUB GDIP_SetPixel (BYVAL hdc AS DWORD)

   LOCAL hStatus AS LONG
   LOCAL pGraphics AS DWORD
   LOCAL pBitmap AS DWORD
   LOCAL strFileName AS STRING
   LOCAL pixelColor AS DWORD
   LOCAL nWidth AS DWORD
   LOCAL nHeight AS DWORD
   LOCAL row AS LONG
   LOCAL col AS LONG

   hStatus = GdipCreateFromHDC(hdc, pGraphics)

   '  // Create a Bitmap object from a JPEG file.
   strFileName = UCODE$("climber.jpg")
   hStatus = GdipCreateBitmapFromFile(STRPTR(strFileName), pBitmap)

   ' // Draw the bitmap
   hStatus = GdipDrawImageI(pGraphics, pBitmap, 0, 0)

   ' // Get the width and height of the bitmap
   hStatus = GdipGetImageWidth(pBitmap, nWidth)
   hStatus = GdipGetImageHeight(pBitmap, nHeight)

   ' // Make an ARGB color
   pixelColor = GDIP_ARGB(255, 0, 0, 0)

   ' // Create a checkered pattern with black pixels.
   FOR row = 0 TO nWidth - 1 STEP 2
      FOR col = 0 TO nHeight STEP 2
         hStatus = GdipBitmapSetPixel(pBitmap, row, col, pixelColor)
      NEXT
   NEXT

   ' // Draw the altered bitmap.
   hStatus = GdipDrawImageI(pGraphics, pBitmap, 200, 0)

   ' // Cleanup
   IF pBitmap THEN GdipDisposeImage(pBitmap)
   IF pGraphics THEN GdipDeleteGraphics(pGraphics)

END SUB