Author Topic: GDI+: Bitmap Examples  (Read 9111 times)

0 Members and 1 Guest are viewing this topic.

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
  • User-Rate: +204/-0
GDI+: Bitmap Examples
« on: November 04, 2011, 12:08:05 AM »
 
The IGdipBitmap interface inherits from the IGdipImage interface. The IGdipImage interface provides methods for loading and saving vector images (metafiles) and raster images (bitmaps). The IGdipBitmap interface expands on the capabilities of the IGdipImage interface by providing additional methods for creating and manipulating raster images.

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
  • User-Rate: +204/-0
GDI+: BitmapGetPixel Example
« Reply #1 on: November 04, 2011, 12:11:43 AM »
 
The following example creates a Bitmap object based on a JPEG file. The code calls the GetPixel method to obtain the color of a pixel in the bitmap and then fills a rectangle with the retrieved color.

Code: [Select]
' ########################################################################################
' Microsoft Windows
' File: CGDIP_BitmapGetPixel.bas
' Contents: GDI+ example
' This version uses the CWindow and CGdiPlus classes, and the GraphCtx graphic control
' Compilers: PBWIN 10+, PBCC 6+
' Headers: Windows API headers 2.03+
' Copyright (c) 2011 José Roca. Freeware. Use at your own risk.
' Portions Copyright (c) Microsoft Corporation. All Rights Reserved.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################

#COMPILE EXE
#DIM ALL
%UNICODE = 1
%USEGRAPHCTX = 1

' // Header files for imported files
#INCLUDE ONCE "CWindow.inc"    ' // CWindow class
#INCLUDE ONCE "CGdiPlus.inc"   ' // CGdiPlus class

%IDC_GRCTX = 1001

' ========================================================================================
' The following example creates a Bitmap object based on a JPEG file. The code calls the
' GetPixel method to obtain the color of a pixel in the bitmap and then fills a rectangle
' with the retrieved color.
' ========================================================================================
SUB Example_GetPixel (BYVAL pGdip AS IGdiPlus, BYVAL hdc AS DWORD)

'   Graphics graphics(hdc);
   LOCAL graphics AS IGdipGraphics
   graphics = pGdip.Graphics(hdc)

'   // Create a Bitmap object from a JPEG file.
'   Bitmap myBitmap(L"Climber.jpg");
   LOCAL myBitmap AS IGdipBitmap
   myBitmap = pGdip.Bitmap("climber.jpg")

'   // Get the value of a pixel from myBitmap.
'   Color pixelColor;
'   myBitmap.GetPixel(25, 25, &pixelColor);
   LOCAL pixelColor AS DWORD
   myBitmap.GetPixel(25, 25, pixelColor)

'   // Fill a rectangle with the pixel color.
'   SolidBrush brush(pixelColor);
'   graphics.FillRectangle(&brush, Rect(0, 0, 100, 100));
   LOCAL brush AS IGdipSolidBrush
   brush = pGdip.SolidBrush(pixelColor)
   graphics.FillRectangle(brush, 0, 0, 100, 100)

END SUB
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS WSTRINGZ PTR, BYVAL nCmdShow AS LONG) AS LONG

   ' // Create an instance of the CWindow class
   LOCAL pWindow AS IWindow
   pWindow = CLASS "CWindow"
   IF ISNOTHING(pWindow) THEN EXIT FUNCTION

   ' // Create the main window
   LOCAL hwnd AS DWORD
   hwnd = pWindow.CreateWindow(%NULL, "GdipBitmapGetPixel", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
   pWindow.SetClientSize 400, 250
   ' // Center the window
   pWindow.CenterWindow

   ' // Create an instance of the GdiPlus class
   LOCAL pGdip AS IGdiPlus
   pGdip = NewGdiPlus

   ' // Add a GDI+ aware graphic control
   LOCAL hCtrl AS DWORD
   hCtrl = pWindow.AddGraphCtx(hwnd, %IDC_GRCTX, "", 0, 0, pWindow.ClientWidth, pWindow.ClientHeight)
   GraphCtx_Clear(hCtrl, %WHITE)

   ' // Get the memory device context of the graphic control
   LOCAL hdc AS DWORD
   hdc = GraphCtx_GetDc(hCtrl)

   ' // Draw the graphics
   Example_GetPixel(pGdip, hdc)

   ' // Default message pump (you can replace it with your own)
   pWindow.DoEvents(nCmdShow)

END FUNCTION
' ========================================================================================

' ========================================================================================
' Main callback function.
' ========================================================================================
FUNCTION WindowProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG

   SELECT CASE uMsg

      CASE %WM_COMMAND
         SELECT CASE LO(WORD, wParam)
            CASE %IDCANCEL
               ' // If the Escape key has been pressed...
               IF HI(WORD, wParam) = %BN_CLICKED THEN
                  ' // ... close the application by sending a WM_CLOSE message
                  SendMessage hwnd, %WM_CLOSE, 0, 0
               END IF
         END SELECT

      CASE %WM_DESTROY
         ' // End the application
         PostQuitMessage 0
         EXIT FUNCTION

   END SELECT

   ' // Pass unprocessed messages to Windows
   FUNCTION = DefWindowProc(hwnd, uMsg, wParam, lParam)

END FUNCTION
' ========================================================================================

Flat API version

Code: [Select]
' ########################################################################################
' Microsoft Windows
' File: GDIP_BitmapGetPixel.bas
' Contents: GDI+ example
' This version uses the CWindow class, the GraphCtx graphic control and the GDI+ Flat API
' Compilers: PBWIN 10+, PBCC 6+
' Headers: Windows API headers 2.03+
' Copyright (c) 2011 José Roca. Freeware. Use at your own risk.
' Portions Copyright (c) Microsoft Corporation. All Rights Reserved.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################

#COMPILE EXE
#DIM ALL
%UNICODE = 1
%USEGRAPHCTX = 1

' // Header files for imported files
#INCLUDE ONCE "CWindow.inc"    ' // CWindow class
#INCLUDE ONCE "GdiPlus.inc"    ' // GdiPlus API

%IDC_GRCTX = 1001

' ========================================================================================
' The following example creates a Bitmap object based on a JPEG file. The code calls the
' GdipBitmapGetPixel function to obtain the color of a pixel in the bitmap and then fills
' a rectangle with the retrieved color.
' ========================================================================================
SUB Example_GetPixel (BYVAL hdc AS DWORD)

'   Graphics graphics(hdc);
   LOCAL graphics AS DWORD
   GdipCreateFromHDC(hdc, graphics)

'   // Create a Bitmap object from a JPEG file.
'   Bitmap myBitmap(L"Climber.jpg");
   LOCAL myBitmap AS DWORD
   GdipCreateBitmapFromFile("climber.jpg", myBitmap)

'   // Get the value of a pixel from myBitmap.
'   Color pixelColor;
'   myBitmap.GetPixel(25, 25, &pixelColor);
   LOCAL pixelColor AS DWORD
   GdipBitmapGetPixel(myBitmap, 25, 25, pixelColor)

'   // Fill a rectangle with the pixel color.
'   SolidBrush brush(pixelColor);
'   graphics.FillRectangle(&brush, Rect(0, 0, 100, 100));
   LOCAL brush AS DWORD
   GdipCreateSolidFill(pixelColor, brush)
   GdipFillRectangle(graphics, brush, 0, 0, 100, 100)

   ' // Cleanup
   GdipDeleteBrush(brush)
   GdipDisposeImage(myBitmap)
   GdipDeleteGraphics(graphics)

END SUB
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS WSTRINGZ PTR, BYVAL nCmdShow AS LONG) AS LONG

   ' // Create an instance of the CWindow class
   LOCAL pWindow AS IWindow
   pWindow = CLASS "CWindow"
   IF ISNOTHING(pWindow) THEN EXIT FUNCTION

   ' // Create the main window
   LOCAL hwnd AS DWORD
   hwnd = pWindow.CreateWindow(%NULL, "GdipBitmapGetPixel", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
   pWindow.SetClientSize 400, 250
   ' // Center the window
   pWindow.CenterWindow

   ' // Add a GDI+ aware graphic control
   LOCAL hCtrl AS DWORD
   hCtrl = pWindow.AddGdipGraphCtx(hwnd, %IDC_GRCTX, "", 0, 0, pWindow.ClientWidth, pWindow.ClientHeight)
   GraphCtx_Clear(hCtrl, %WHITE)

   ' // Get the memory device context of the graphic control
   LOCAL hdc AS DWORD
   hdc = GraphCtx_GetDc(hCtrl)

   ' // Draw the graphics
   Example_GetPixel(hdc)

   ' // Default message pump (you can replace it with your own)
   pWindow.DoEvents(nCmdShow)

END FUNCTION
' ========================================================================================

' ========================================================================================
' Main callback function.
' ========================================================================================
FUNCTION WindowProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG

   SELECT CASE uMsg

      CASE %WM_COMMAND
         SELECT CASE LO(WORD, wParam)
            CASE %IDCANCEL
               ' // If the Escape key has been pressed...
               IF HI(WORD, wParam) = %BN_CLICKED THEN
                  ' // ... close the application by sending a WM_CLOSE message
                  SendMessage hwnd, %WM_CLOSE, 0, 0
               END IF
         END SELECT

      CASE %WM_DESTROY
         ' // End the application
         PostQuitMessage 0
         EXIT FUNCTION

   END SELECT

   ' // Pass unprocessed messages to Windows
   FUNCTION = DefWindowProc(hwnd, uMsg, wParam, lParam)

END FUNCTION
' ========================================================================================

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
  • User-Rate: +204/-0
GDI+: BitmapLockBits1 Example
« Reply #2 on: November 04, 2011, 12:15:59 AM »
 
Locking Pixel Data for Reading

Code: [Select]
' ########################################################################################
' Microsoft Windows
' File: CGDIP_BitmapLockBits1.bas
' Contents: GDI+ example
' This version uses the CGdiPlus class.
' Compilers: PBWIN 10+, PBCC 6+
' Headers: Windows API headers 2.03+
' Copyright (c) 2011 José Roca. Freeware. Use at your own risk.
' Portions Copyright (c) Microsoft Corporation. All Rights Reserved.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################

' ========================================================================================
' Locking Pixel Data for Reading
'
' The following console application creates a Bitmap based on a BMP file. The call to
' the LockBits method locks a 5x3 rectangular portion of the bitmap for
' reading. The locked portion starts at (20, 30); that is, row 30, column 20. One of the
' arguments passed to LockBits is the address of a BitmapData structure When
' LockBits returns, the Scan0 data member of the BitmapData structure points to
' a block of memory that holds the values of the pixels in the 5x3 portion of the bitmap.
' The Stride data member of the BitmapData structure holds the byte offset between one
' scan line and the next scan line in that block of memory.
'
' The nested loops display the hexadecimal values of the fifteen retrieved pixels. Note
' that pixels is a pointer to a DWORD, so the code must calculate the number of DWORD values
' that fit in a scan line. Because each DWORD is four bytes, that number is the stride
' divided by 4.
'
' The code below, along with a particular file, LockBitsTest1.bmp, produces the following
' output:
'
' The stride is 460
'
' FFFF0000
' FFFF0000
' FFFF0000
' FFFF0000
' --------
' FF0000FF
' FF0000FF
' FF0000FF
' FF0000FF
' --------
' FF0000FF
' FF0000FF
' FF0000FF
' FF0000FF
' ========================================================================================

' CSED_PBCC - Use the PBCC compiler
#COMPILE EXE
#DIM ALL
#INCLUDE "CGDIPLUS.INC"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   ' // Create an instance of the GdiPlus class
   LOCAL pGdip AS IGdiPlus
   pGdip = NewGdiplus

   ' // Create a bitmap object
'   Bitmap* bitmap = new Bitmap(L"LockBitsTest1.bmp");
   LOCAL pBitmap AS IGdipBitmap
   pBitmap = pGdip.Bitmap("LockBitsTest1.bmp")
   IF ISNOTHING(pBitmap) THEN EXIT FUNCTION
'   BitmapData* bitmapData = new BitmapData;
   LOCAL bmpData AS BITMAPDATA
'   Rect rect(20, 30, 5, 3);
   LOCAL rc AS RECT
   SetRect rc, 20, 32, 5, 3

   ' // Lock a 5 x 3 rectangular portion of the bitmap for reading
'   bitmap->LockBits(
'      &rect,
'      ImageLockModeRead,
'      PixelFormat32bppARGB,
'      bitmapData);
   pBitmap.LockBits(rc, %ImageLockModeRead, %PixelFormat32bppARGB, bmpData)
   IF pBitmap.GetLastStatus <> %StatusOK THEN EXIT FUNCTION

'   printf("The stride is %d.\n\n", bitmapData->Stride);
   PRINT "The stride is " & STR$(bmpData.stride)
   PRINT

   ' // Display the hexadecimal value of each pixel in the 5x3 rectangle.
'   // Display the hexadecimal value of each pixel in the 5x3 rectangle.
'   UINT* pixels = (UINT*)bitmapData->Scan0;

'   for(UINT row = 0; row < 3; ++row)
'   {
'      for(UINT col = 0; col < 5; ++col)
'      {
'         printf("%x\n", pixels[row * bitmapData->Stride / 4 + col]);
'      }
'      printf("- - - - - - - - - - \n");
'   }
   LOCAL pPixels AS DWORD PTR
   pPixels = bmpData.Scan0
   IF pPixels = %NULL THEN EXIT FUNCTION

   LOCAL row AS LONG
   LOCAL col AS LONG

   FOR row = 0 TO 2
      FOR col = 0 TO 4
         PRINT HEX$(@pPixels[row * bmpData.stride / 4 + col])
      NEXT
      PRINT "--------"
   NEXT

   ' // Unlock the bits
'   bitmap->UnlockBits(bitmapData);
   pBitmap.UnlockBits(bmpData)

   WAITKEY$

END FUNCTION
' ========================================================================================

Flat API version

Code: [Select]
' ########################################################################################
' Microsoft Windows
' File: GDIP_BitmapLockBits1.bas
' Contents: GDI+ example
' This version uses the GdiPlus Flat API.
' Compilers: PBWIN 10+, PBCC 6+
' Headers: Windows API headers 2.03+
' Copyright (c) 2011 José Roca. Freeware. Use at your own risk.
' Portions Copyright (c) Microsoft Corporation. All Rights Reserved.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################

' ========================================================================================
' Locking Pixel Data for Reading
'
' The following console application creates a Bitmap based on a BMP file. The call to
' the GdipBitmapLockBits function locks a 5x3 rectangular portion of the bitmap for
' reading. The locked portion starts at (20, 30); that is, row 30, column 20. One of the
' arguments passed to GdipBitmapLockBits is the address of a BitmapData structure When
' GdipBitmapLockBits returns, the Scan0 data member of the BitmapData structure points to
' a block of memory that holds the values of the pixels in the 5x3 portion of the bitmap.
' The Stride data member of the BitmapData structure holds the byte offset between one
' scan line and the next scan line in that block of memory.
'
' The nested loops display the hexadecimal values of the fifteen retrieved pixels. Note
' that pixels is a pointer to a DWORD, so the code must calculate the number of DWORD values
' that fit in a scan line. Because each DWORD is four bytes, that number is the stride
' divided by 4.
'
' The code below, along with a particular file, LockBitsTest1.bmp, produces the following
' output:
'
' The stride is 460
'
' FFFF0000
' FFFF0000
' FFFF0000
' FFFF0000
' --------
' FF0000FF
' FF0000FF
' FF0000FF
' FF0000FF
' --------
' FF0000FF
' FF0000FF
' FF0000FF
' FF0000FF
' ========================================================================================

' CSED_PBCC - Use the PBCC compiler
#COMPILE EXE
#DIM ALL
#INCLUDE "GDIPLUS.INC"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL token AS DWORD
   LOCAL StartupInput AS GdiplusStartupInput
   LOCAL wszFileName AS WSTRINGZ * 260
   LOCAL pBitmap AS DWORD
   LOCAL bmpData AS BITMAPDATA
   LOCAL rc AS RECT
   LOCAL row AS LONG
   LOCAL col AS LONG
   LOCAL pPixels AS DWORD PTR

   ' // Initialize GDI+
   StartupInput.GdiplusVersion = 1
   GdiplusStartup(token, StartupInput, BYVAL %NULL)

   ' // Create a bitmap object
   wszFileName = "LockBitsTest1.bmp"
   GdipCreateBitmapFromFile(wszFileName, pBitmap)

   ' // Lock a 5 x 3 rectangular portion of the bitmap for reading
   SetRect rc, 20, 32, 5, 3
   GdipBitmapLockBits(pBitmap, rc, %ImageLockModeRead, %PixelFormat32bppARGB, bmpData)

   PRINT "The stride is " & STR$(bmpData.stride)
   PRINT

   ' // Display the hexadecimal value of each pixel in the 5x3 rectangle.
   pPixels = bmpData.Scan0

   FOR row = 0 TO 2
      FOR col = 0 TO 4
         PRINT HEX$(@pPixels[row * bmpData.stride / 4 + col])
      NEXT
      PRINT "--------"
   NEXT

   ' // Unlock the bits
   GdipBitmapUnlockBits(pBitmap, bmpData)

   ' // Cleanup
   GdipDisposeImage(pBitmap)

   ' // Shutdown GDI+
   GdiplusShutdown token

   WAITKEY$

END FUNCTION
' ========================================================================================

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
  • User-Rate: +204/-0
GDI+: BitmapLockBits2 Example
« Reply #3 on: November 04, 2011, 12:19:06 AM »
 
Locking Pixel Data for Writing

Code: [Select]
' ########################################################################################
' Microsoft Windows
' File: CGDIP_BitmapLockBits2.bas
' Contents: GDI+ example
' This version uses the CWindow and CGdiPlus classes, and the GraphCtx graphic control
' Compilers: PBWIN 10.02+, PBCC 6.02+
' Headers: Windows API headers 2.03+
' Copyright (c) 2011 José Roca. Freeware. Use at your own risk.
' Portions Copyright (c) Microsoft Corporation. All Rights Reserved.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################

#COMPILE EXE
#DIM ALL
%UNICODE = 1
%USEGRAPHCTX = 1

' // Header files for imported files
#INCLUDE ONCE "CWindow.inc"    ' // CWindow class
#INCLUDE ONCE "CGdiPlus.inc"   ' // CWindow class

%IDC_GRCTX = 1001

' ========================================================================================
' Locking Pixel Data for Writing
'
' The following example creates a Bitmap based on a BMP file. The call to the
' LockBits method locks a 50x30 rectangular portion of the bitmap for writing.
' The locked portion starts at (20, 10); that is, row 10, column 20. One of the arguments
' passed to the LockBits method is the address of a BitmapData structure. When
' LockBits returns, the Scan0 data member of the BitmapData structure points to
' a block of memory that represents the pixels in the 50x30 portion of the bitmap. The
' Stride data member of the BitmapData structure holds the byte offset between one scan
' line and the next scan line in that block of memory.
' Scan0 does not point to the actual pixel data of the Bitmap; rather, it points to a
' temporary buffer that represents a portion of the pixel data in the Bitmap structure.
' The code writes the value &HFF00FF00 (green) to 1500 locations in the temporary buffer.
' Later, the call to the UnlockBits method copies those values to the Bitmap object itself.
' Note that pixels is a pointer to a DWORD, so the code must calculate the number of DWORD
' values that fit in a scan line. Because each DWORD is four bytes, that number is the
' stride divided by 4.
' ========================================================================================
SUB Example_LockBits2 (BYVAL pGdip AS IGdiPlus, BYVAL hdc AS DWORD)

'   Graphics graphics(hdc);
   LOCAL graphics AS IGdipGraphics
   graphics = pGdip.Graphics(hdc)

'   // Create a Bitmap object from a BMP file.
'   Bitmap bitmap(L"LockBitsTest2.bmp");
   LOCAL pBitmap AS IGdipBitmap
   pBitmap = pGdip.Bitmap("LockBitsTest2.bmp")

'   // Display the bitmap before locking and altering it.
'   graphics.DrawImage(&bitmap, 10, 10);
   graphics.DrawImage(pBitmap, 10, 10)

'   // Lock a 50xs30 rectangular portion of the bitmap for writing.
'   BitmapData bitmapData;
   LOCAL bmpData AS BITMAPDATA
'   Rect rect(20, 10, 50, 30);
   LOCAL rc AS RECT
   SetRect rc, 20, 10, 50, 30
'   bitmap.LockBits(
'      &rect,
'      ImageLockModeWrite,
'      PixelFormat32bppARGB,
'      &bitmapData);
   pBitmap.LockBits(rc, %ImageLockModeWrite, %PixelFormat32bppARGB, bmpData)

'   UINT* pixels;
   LOCAL pPixels AS DWORD PTR
'   // Write to the temporary buffer provided by LockBits.
'   pixels = (UINT*)bitmapData.Scan0;
   pPixels = bmpData.Scan0

'   for(UINT row = 0; row < 30; ++row)
'   {
'      for(UINT col = 0; col < 50; ++col)
'      {
'         pixels[row * bitmapData.Stride / 4 + col] = 0xff00ff00;
'      }
'   }
   LOCAL row AS LONG
   LOCAL col AS LONG
   IF pPixels THEN
      FOR row = 0 TO 29
         FOR col = 0 TO 49
            @pPixels[row * bmpData.Stride / 4 + col] = &HFF00FF00
         NEXT
      NEXT
   END IF

'   // Commit the changes, and unlock the 50x30 portion of the bitmap.
'   bitmap.UnlockBits(&bitmapData);
   pBitmap.UnlockBits(bmpData)

'   // Display the altered bitmap.
'   graphics.DrawImage(&bitmap, 150, 10);
   graphics.DrawImage(pBitmap, 150, 10)

END SUB
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS WSTRINGZ PTR, BYVAL nCmdShow AS LONG) AS LONG

   ' // Create an instance of the CWindow class
   LOCAL pWindow AS IWindow
   pWindow = CLASS "CWindow"
   IF ISNOTHING(pWindow) THEN EXIT FUNCTION

   ' // Create the main window
   LOCAL hwnd AS DWORD
   hwnd = pWindow.CreateWindow(%NULL, "Locking Pixel Data for Writing", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
   pWindow.SetClientSize 400, 250
   ' // Center the window
   pWindow.CenterWindow

   ' // Create an instance of the GdiPlus class
   LOCAL pGdip AS IGdiPlus
   pGdip = NewGdiPlus

   ' // Add a graphic control
   LOCAL hCtrl AS DWORD
   hCtrl = pWindow.AddGraphCtx(hwnd, %IDC_GRCTX, "", 0, 0, pWindow.ClientWidth, pWindow.ClientHeight)
   GraphCtx_Clear(hCtrl, %WHITE)

   ' // Get the memory device context of the graphic control
   LOCAL hdc AS DWORD
   hdc = GraphCtx_GetDc(hCtrl)

   ' // Draw the graphics
   Example_LockBits2(pGdip, hdc)

   ' // Default message pump (you can replace it with your own)
   pWindow.DoEvents(nCmdShow)

END FUNCTION
' ========================================================================================

' ========================================================================================
' Main callback function.
' ========================================================================================
FUNCTION WindowProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG

   LOCAL hCtrl AS DWORD
   LOCAL hDC AS DWORD

   SELECT CASE uMsg

      CASE %WM_COMMAND
         SELECT CASE LO(WORD, wParam)
            CASE %IDCANCEL
               IF HI(WORD, wParam) = %BN_CLICKED THEN
                  SendMessage hwnd, %WM_CLOSE, 0, 0
               END IF
         END SELECT

      CASE %WM_DESTROY
         ' // Close the main window
         PostQuitMessage 0
         EXIT FUNCTION

   END SELECT

   FUNCTION = DefWindowProc(hwnd, uMsg, wParam, lParam)

END FUNCTION
' ========================================================================================

Flat API version

Code: [Select]
' ########################################################################################
' Microsoft Windows
' File: GDIP_BitmapLockBits2.bas
' Contents: GDI+ example
' This version uses the CWindow class, the GraphCtx graphic control and the GDI+ Flat API
' Compilers: PBWIN 10.02+, PBCC 6.02+
' Headers: Windows API headers 2.03+
' Copyright (c) 2011 José Roca. Freeware. Use at your own risk.
' Portions Copyright (c) Microsoft Corporation. All Rights Reserved.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################

#COMPILE EXE
#DIM ALL
%UNICODE = 1
%USEGRAPHCTX = 1

' // Header files for imported files
#INCLUDE ONCE "CWindow.inc"    ' // CWindow class
#INCLUDE ONCE "GdiPlus.inc"    ' // GdiPlus API

%IDC_GRCTX = 1001

' ========================================================================================
' Locking Pixel Data for Writing
'
' The following example creates a Bitmap based on a BMP file. The call to the
' GdipBitmapLockBits function locks a 50x30 rectangular portion of the bitmap for writing.
' The locked portion starts at (20, 10); that is, row 10, column 20. One of the arguments
' passed to GdipBitmapLockBits is the address of a BitmapData structure. When
' GdipBitmapLockBits returns, the Scan0 data member of the BitmapData structure points to
' a block of memory that represents the pixels in the 50x30 portion of the bitmap. The
' Stride data member of the BitmapData structure holds the byte offset between one scan
' line and the next scan line in that block of memory.
' Scan0 does not point to the actual pixel data of the Bitmap; rather, it points to a
' temporary buffer that represents a portion of the pixel data in the Bitmap structure.
' The code writes the value &HFF00FF00 (green) to 1500 locations in the temporary buffer.
' Later, the call to GdipBitmapUnlockBits copies those values to the Bitmap object itself.
' Note that pixels is a pointer to a DWORD, so the code must calculate the number of DWORD
' values that fit in a scan line. Because each DWORD is four bytes, that number is the
' stride divided by 4.
' ========================================================================================
SUB Example_LockBits2 (BYVAL hdc AS DWORD)

'   Graphics graphics(hdc);
   LOCAL graphics AS DWORD
   GdipCreateFromHDC(hdc, graphics)

'   // Create a Bitmap object from a BMP file.
'   Bitmap bitmap(L"LockBitsTest2.bmp");
   LOCAL pBitmap AS DWORD
   GdipCreateBitmapFromFile("LockBitsTest2.bmp", pBitmap)

'   // Display the bitmap before locking and altering it.
'   graphics.DrawImage(&bitmap, 10, 10);
   GdipDrawImage(graphics, pBitmap, 10, 10)

'   // Lock a 50xs30 rectangular portion of the bitmap for writing.
'   BitmapData bitmapData;
   LOCAL bmpData AS BITMAPDATA
'   Rect rect(20, 10, 50, 30);
   LOCAL rc AS RECT
   SetRect rc, 20, 10, 50, 30
'   bitmap.LockBits(
'      &rect,
'      ImageLockModeWrite,
'      PixelFormat32bppARGB,
'      &bitmapData);
   GdipBitmapLockBits(pBitmap, rc, %ImageLockModeWrite, %PixelFormat32bppARGB, bmpData)

'   UINT* pixels;
   LOCAL pPixels AS DWORD PTR
'   // Write to the temporary buffer provided by LockBits.
'   pixels = (UINT*)bitmapData.Scan0;
   pPixels = bmpData.Scan0

'   for(UINT row = 0; row < 30; ++row)
'   {
'      for(UINT col = 0; col < 50; ++col)
'      {
'         pixels[row * bitmapData.Stride / 4 + col] = 0xff00ff00;
'      }
'   }
   LOCAL row AS LONG
   LOCAL col AS LONG
   IF pPixels THEN
      FOR row = 0 TO 29
         FOR col = 0 TO 49
            @pPixels[row * bmpData.Stride / 4 + col] = &HFF00FF00
         NEXT
      NEXT
   END IF

'   // Commit the changes, and unlock the 50x30 portion of the bitmap.
'   bitmap.UnlockBits(&bitmapData);
   GdipBitmapUnlockBits(pBitmap, bmpData)

'   // Display the altered bitmap.
'   graphics.DrawImage(&bitmap, 150, 10);
   GdipDrawImage(graphics, pBitmap, 150, 10)

   ' // Cleanup
   GdipDisposeImage(pBitmap)
   GdipDeleteGraphics(graphics)

END SUB
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS WSTRINGZ PTR, BYVAL nCmdShow AS LONG) AS LONG

   ' // Create an instance of the CWindow class
   LOCAL pWindow AS IWindow
   pWindow = CLASS "CWindow"
   IF ISNOTHING(pWindow) THEN EXIT FUNCTION

   ' // Create the main window
   LOCAL hwnd AS DWORD
   hwnd = pWindow.CreateWindow(%NULL, "Locking Pixel Data for Writing", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
   pWindow.SetClientSize 400, 250
   ' // Center the window
   pWindow.CenterWindow

   ' // Add a GDI+ aware graphic control
   LOCAL hCtrl AS DWORD
   hCtrl = pWindow.AddGdipGraphCtx(hwnd, %IDC_GRCTX, "", 0, 0, pWindow.ClientWidth, pWindow.ClientHeight)
   GraphCtx_Clear(hCtrl, %WHITE)

   ' // Get the memory device context of the graphic control
   LOCAL hdc AS DWORD
   hdc = GraphCtx_GetDc(hCtrl)

   ' // Draw the graphics
   Example_LockBits2(hdc)

   ' // Default message pump (you can replace it with your own)
   pWindow.DoEvents(nCmdShow)

END FUNCTION
' ========================================================================================

' ========================================================================================
' Main callback function.
' ========================================================================================
FUNCTION WindowProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG

   LOCAL hCtrl AS DWORD
   LOCAL hDC AS DWORD

   SELECT CASE uMsg

      CASE %WM_COMMAND
         SELECT CASE LO(WORD, wParam)
            CASE %IDCANCEL
               IF HI(WORD, wParam) = %BN_CLICKED THEN
                  SendMessage hwnd, %WM_CLOSE, 0, 0
               END IF
         END SELECT

      CASE %WM_DESTROY
         ' // Close the main window
         PostQuitMessage 0
         EXIT FUNCTION

   END SELECT

   FUNCTION = DefWindowProc(hwnd, uMsg, wParam, lParam)

END FUNCTION
' ========================================================================================

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
  • User-Rate: +204/-0
GDI+: BitmapLockBits3 Example
« Reply #4 on: November 04, 2011, 12:21:22 AM »
 
Locking Pixel Data for Writing from a User Input Buffer

Code: [Select]
' ########################################################################################
' Microsoft Windows
' File: CGDIP_BitmapLockBits3.bas
' Contents: GDI+ example
' This version uses the CWindow and CGdiPlus classes, and the GraphCtx graphic control
' Compilers: PBWIN 10+, PBCC 6+
' Headers: Windows API headers 2.03+
' Copyright (c) 2011 José Roca. Freeware. Use at your own risk.
' Portions Copyright (c) Microsoft Corporation. All Rights Reserved.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################

#COMPILE EXE
#DIM ALL
%UNICODE = 1
%USEGRAPHCTX = 1

' // Header files for imported files
#INCLUDE ONCE "CWindow.inc"    ' // CWindow class
#INCLUDE ONCE "CGdiPlus.inc"   ' // CWindow class

%IDC_GRCTX = 1001

' ========================================================================================
' Locking Pixel Data for Writing from a User Input Buffer
'
' The following example creates a Bitmap based on a BMP file. The call to the
' LockBits method locks a 50x30 rectangular portion of the bitmap for writing.
' The locked portion starts at (20, 10); that is, row 10, column 20. The
' ImageLockModeUserInputBuf flag of the flags parameter is set, so lockedBitmapData serves
' as an input parameter.
' Before calling LockBits, the code allocates a buffer and fills that buffer
' with the pixel data (in this case, all aqua pixels) that will later be written to the
' Bitmap. The code creates a BitmapData structure and sets its Scan0 data member to
' the address of the pixel data buffer. The code also initializes the other data members
' of the BitmapData structure with the attributes (width, height, format, and stride) of
' the pixel data buffer.
' The code passes the address of the initialized BitmapData structure to the
' LockBits method. The subsequent call to UnlockBits copies the
' pixel data in the buffer to the Bitmap.
' Note that each scan line in the pixels array has 50 DWORD values, but the code must set
' the stride to the number of bytes in each scan line. Because each DWORD is four bytes,
' that number is the width (50) multiplied by 4.
' ========================================================================================
SUB Example_LockBits3 (BYVAL pGdip AS IGdiPlus, BYVAL hdc AS DWORD)

'   Graphics graphics(hdc);
   LOCAL graphics AS IGdipGraphics
   graphics = pGdip.Graphics(hdc)

'   INT row, col;
   LOCAL row AS LONG
   LOCAL col AS LONG

'   // Create and fill a pixel data buffer.
'   UINT pixels[30][50];
'   for(row = 0; row > 30; ++row)
'      for(col = 0; col > 50; ++col)
'         pixels[row][col] = 0xff00ffff;  // aqua
   DIM pxs(29, 49) AS DWORD
   FOR row = 0 TO 29
      FOR col = 0 TO 49
         pxs(row, col) = &HFF00FFFF  ' // aqua
      NEXT
   NEXT

'   BitmapData bitmapData;
'   bitmapData.Width = 50,
'   bitmapData.Height = 30,
'   bitmapData.Stride = 4*bitmapData.Width;
'   bitmapData.PixelFormat = PixelFormat32bppARGB;
'   bitmapData.Scan0 = (VOID*)pixels;
'   bitmapData.Reserved = NULL;

   LOCAL bmpData AS BITMAPDATA
   bmpData.Width = 50
   bmpData.Height = 30
   bmpData.Stride = 4 * bmpData.Width
   bmpData.PixelFormat = %PixelFormat32bppARGB
   bmpData.Scan0 = VARPTR(pxs(0))
   bmpData.Reserved = %NULL

'   // Create a Bitmap object from a BMP file.
'   Bitmap bitmap(L"LockBitsTest2.bmp");
   LOCAL pBitmap AS IGdipBitmap
   pBitmap = pGdip.Bitmap("LockBitsTest3.bmp")

'   // Display the bitmap before locking and altering it.
'   graphics.DrawImage(&bitmap, 10, 10);
   graphics.DrawImage(pBitmap, 10, 10)

'   // Lock a 50x30 rectangular portion of the bitmap for writing.
'   Rect rect(20, 10, 50, 30);
   LOCAL rc AS RECT
   SetRect rc, 20, 10, 50, 30

'   bitmap.LockBits(
'      &rect,
'      ImageLockModeWrite|ImageLockModeUserInputBuf,
'      PixelFormat32bppARGB,
'      &bitmapData);
   pBitmap.LockBits(rc, %ImageLockModeWrite OR %ImageLockModeUserInputBuf, %PixelFormat32bppARGB, bmpData)

'   // Commit the changes and unlock the 50x30 portion of the bitmap.
'   bitmap.UnlockBits(&bitmapData);
   pBitmap.UnlockBits(bmpData)

'   // Display the altered bitmap.
'   graphics.DrawImage(&bitmap, 150, 10);
   graphics.DrawImage(pBitmap, 150, 10)

END SUB
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS WSTRINGZ PTR, BYVAL nCmdShow AS LONG) AS LONG

   ' // Create an instance of the CWindow class
   LOCAL pWindow AS IWindow
   pWindow = CLASS "CWindow"
   IF ISNOTHING(pWindow) THEN EXIT FUNCTION

   ' // Create the main window
   LOCAL hwnd AS DWORD
   hwnd = pWindow.CreateWindow(%NULL, "Locking Pixel Data for Writing from a User Input Buffer", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
   pWindow.SetClientSize 400, 250
   ' // Center the window
   pWindow.CenterWindow

   ' // Create an instance of the GdiPlus class
   LOCAL pGdip AS IGdiPlus
   pGdip = NewGdiPlus

   ' // Add a graphic control
   LOCAL hCtrl AS DWORD
   hCtrl = pWindow.AddGraphCtx(hwnd, %IDC_GRCTX, "", 0, 0, pWindow.ClientWidth, pWindow.ClientHeight)
   GraphCtx_Clear(hCtrl, %WHITE)

   ' // Get the memory device context of the graphic control
   LOCAL hdc AS DWORD
   hdc = GraphCtx_GetDc(hCtrl)

   ' // Draw the graphics
   Example_LockBits3(pGdip, hdc)

   ' // Default message pump (you can replace it with your own)
   pWindow.DoEvents(nCmdShow)

END FUNCTION
' ========================================================================================

' ========================================================================================
' Main callback function.
' ========================================================================================
FUNCTION WindowProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG

   LOCAL hCtrl AS DWORD
   LOCAL hDC AS DWORD

   SELECT CASE uMsg

      CASE %WM_COMMAND
         SELECT CASE LO(WORD, wParam)
            CASE %IDCANCEL
               IF HI(WORD, wParam) = %BN_CLICKED THEN
                  SendMessage hwnd, %WM_CLOSE, 0, 0
               END IF
         END SELECT

      CASE %WM_DESTROY
         ' // Close the main window
         PostQuitMessage 0
         EXIT FUNCTION

   END SELECT

   FUNCTION = DefWindowProc(hwnd, uMsg, wParam, lParam)

END FUNCTION
' ========================================================================================

Flat API version

Code: [Select]
' ########################################################################################
' Microsoft Windows
' File: CGDIP_BitmapLockBits3.bas
' Contents: GDI+ example
' This version uses the CWindow class, the GraphCtx graphic control and the GDI+ Flat API
' Compilers: PBWIN 10+, PBCC 6+
' Headers: Windows API headers 2.03+
' Copyright (c) 2011 José Roca. Freeware. Use at your own risk.
' Portions Copyright (c) Microsoft Corporation. All Rights Reserved.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################

#COMPILE EXE
#DIM ALL
%UNICODE = 1
%USEGRAPHCTX = 1

' // Header files for imported files
#INCLUDE ONCE "CWindow.inc"    ' // CWindow class
#INCLUDE ONCE "GdiPlus.inc"    ' // GdipPlus API

%IDC_GRCTX = 1001

' ========================================================================================
' Locking Pixel Data for Writing from a User Input Buffer
'
' The following example creates a Bitmap based on a BMP file. The call to the
' GdipBitmapLockBits function locks a 50x30 rectangular portion of the bitmap for writing.
' The locked portion starts at (20, 10); that is, row 10, column 20. The
' ImageLockModeUserInputBuf flag of the flags parameter is set, so lockedBitmapData serves
' as an input parameter.
' Before calling GdipBitmapLockBits, the code allocates a buffer and fills that buffer
' with the pixel data (in this case, all aqua pixels) that will later be written to the
' Bitmap. The code creates a BitmapData structure and sets its Scan0 data member to
' the address of the pixel data buffer. The code also initializes the other data members
' of the BitmapData structure with the attributes (width, height, format, and stride) of
' the pixel data buffer.
' The code passes the address of the initialized BitmapData structure to the
' GdipBitmapLockBits function. The subsequent call to GdipBitmapUnlockBits copies the
' pixel data in the buffer to the Bitmap.
' Note that each scan line in the pixels array has 50 DWORD values, but the code must set
' the stride to the number of bytes in each scan line. Because each DWORD is four bytes,
' that number is the width (50) multiplied by 4.
' ========================================================================================
SUB Example_LockBits3 (BYVAL hdc AS DWORD)

'   Graphics graphics(hdc);
   LOCAL graphics AS DWORD
   GdipCreateFromHDC(hdc, graphics)

'   INT row, col;
   LOCAL row AS LONG
   LOCAL col AS LONG

'   // Create and fill a pixel data buffer.
'   UINT pixels[30][50];
'   for(row = 0; row > 30; ++row)
'      for(col = 0; col > 50; ++col)
'         pixels[row][col] = 0xff00ffff;  // aqua
   DIM pxs(29, 49) AS DWORD
   FOR row = 0 TO 29
      FOR col = 0 TO 49
         pxs(row, col) = &HFF00FFFF  ' // aqua
      NEXT
   NEXT

'   BitmapData bitmapData;
'   bitmapData.Width = 50,
'   bitmapData.Height = 30,
'   bitmapData.Stride = 4*bitmapData.Width;
'   bitmapData.PixelFormat = PixelFormat32bppARGB;
'   bitmapData.Scan0 = (VOID*)pixels;
'   bitmapData.Reserved = NULL;

   LOCAL bmpData AS BITMAPDATA
   bmpData.Width = 50
   bmpData.Height = 30
   bmpData.Stride = 4 * bmpData.Width
   bmpData.PixelFormat = %PixelFormat32bppARGB
   bmpData.Scan0 = VARPTR(pxs(0))
   bmpData.Reserved = %NULL

'   // Create a Bitmap object from a BMP file.
'   Bitmap bitmap(L"LockBitsTest2.bmp");
   LOCAL pBitmap AS DWORD
   GdipCreateBitmapFromFile("LockBitsTest3.bmp", pBitmap)

'   // Display the bitmap before locking and altering it.
'   graphics.DrawImage(&bitmap, 10, 10);
   GdipDrawImageI(graphics, pBitmap, 10, 10)

'   // Lock a 50x30 rectangular portion of the bitmap for writing.
'   Rect rect(20, 10, 50, 30);
   LOCAL rc AS RECT
   SetRect rc, 20, 10, 50, 30

'   bitmap.LockBits(
'      &rect,
'      ImageLockModeWrite|ImageLockModeUserInputBuf,
'      PixelFormat32bppARGB,
'      &bitmapData);
   GdipBitmapLockBits(pBitmap, rc, %ImageLockModeWrite OR %ImageLockModeUserInputBuf, %PixelFormat32bppARGB, bmpData)

'   // Commit the changes and unlock the 50x30 portion of the bitmap.
'   bitmap.UnlockBits(&bitmapData);
   GdipBitmapUnlockBits(pBitmap, bmpData)

'   // Display the altered bitmap.
'   graphics.DrawImage(&bitmap, 150, 10);
   GdipDrawImage(graphics, pBitmap, 150, 10)

   ' // Cleanup
   GdipDisposeImage(pBitmap)
   GdipDeleteGraphics(graphics)

END SUB
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS WSTRINGZ PTR, BYVAL nCmdShow AS LONG) AS LONG

   ' // Create an instance of the CWindow class
   LOCAL pWindow AS IWindow
   pWindow = CLASS "CWindow"
   IF ISNOTHING(pWindow) THEN EXIT FUNCTION

   ' // Create the main window
   LOCAL hwnd AS DWORD
   hwnd = pWindow.CreateWindow(%NULL, "Locking Pixel Data for Writing from a User Input Buffer", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
   pWindow.SetClientSize 400, 250
   ' // Center the window
   pWindow.CenterWindow

   ' // Add a GDI+ aware graphic control
   LOCAL hCtrl AS DWORD
   hCtrl = pWindow.AddGdipGraphCtx(hwnd, %IDC_GRCTX, "", 0, 0, pWindow.ClientWidth, pWindow.ClientHeight)
   GraphCtx_Clear(hCtrl, %WHITE)

   ' // Get the memory device context of the graphic control
   LOCAL hdc AS DWORD
   hdc = GraphCtx_GetDc(hCtrl)

   ' // Draw the graphics
   Example_LockBits3(hdc)

   ' // Default message pump (you can replace it with your own)
   pWindow.DoEvents(nCmdShow)

END FUNCTION
' ========================================================================================

' ========================================================================================
' Main callback function.
' ========================================================================================
FUNCTION WindowProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG

   LOCAL hCtrl AS DWORD
   LOCAL hDC AS DWORD

   SELECT CASE uMsg

      CASE %WM_COMMAND
         SELECT CASE LO(WORD, wParam)
            CASE %IDCANCEL
               IF HI(WORD, wParam) = %BN_CLICKED THEN
                  SendMessage hwnd, %WM_CLOSE, 0, 0
               END IF
         END SELECT

      CASE %WM_DESTROY
         ' // Close the main window
         PostQuitMessage 0
         EXIT FUNCTION

   END SELECT

   FUNCTION = DefWindowProc(hwnd, uMsg, wParam, lParam)

END FUNCTION
' ========================================================================================

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
  • User-Rate: +204/-0
GDI+: BitmapSetPixel Example
« Reply #5 on: November 04, 2011, 12:25:02 AM »
 
The following example creates a Bitmap object based on a JPEG file. The code draws the bitmap once unaltered. Then the code calls the SetPixel method to create a checkered pattern of black pixels in the bitmap and draws the altered bitmap.

Code: [Select]
' ########################################################################################
' Microsoft Windows
' File: CGDIP_BitmapSetPixel.bas
' Contents: GDI+ example
' This version uses the CWindow and CGdiPlus classes, and the GraphCtx graphic control
' Compilers: PBWIN 10+, PBCC 6+
' Headers: Windows API headers 2.03+
' Copyright (c) 2011 José Roca. Freeware. Use at your own risk.
' Portions Copyright (c) Microsoft Corporation. All Rights Reserved.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################

#COMPILE EXE
#DIM ALL
%UNICODE = 1
%USEGRAPHCTX = 1

' // Header files for imported files
#INCLUDE ONCE "CWindow.inc"    ' // CWindow class
#INCLUDE ONCE "CGdiPlus.inc"   ' // CGdiPlus class

%IDC_GRCTX = 1001

' ========================================================================================
' The following example creates a Bitmap object based on a JPEG file. The code draws the
' bitmap once unaltered. Then the code calls the SetPixel method to create a
' checkered pattern of black pixels in the bitmap and draws the altered bitmap.
' ========================================================================================
SUB Example_SetPixel (BYVAL pGdip AS IGdiPlus, BYVAL hdc AS DWORD)

'   Graphics graphics(hdc);
   LOCAL graphics AS IGdipGraphics
   graphics = pGdip.Graphics(hdc)

'   // Create a Bitmap object from a JPEG file.
'   Bitmap myBitmap(L"Climber.jpg");
   LOCAL myBitmap AS IGdipBitmap
   myBitmap = pGdip.Bitmap("climber.jpg")

'   // Draw the bitmap.
'   graphics.DrawImage(&myBitmap, 0, 0);
   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));
'      }
'   }

   ' // Get the width and height of the bitmap
   LOCAL nWidth AS DWORD
   LOCAL nHeight AS DWORD
   nWidth = myBitmap.GetWidth
   nHeight = myBitmap.GetHeight

   ' // Make an ARGB color
   LOCAL pixelColor AS DWORD
   pixelColor = pGdip.Color(255, 0, 0, 0)

   LOCAL row AS LONG
   LOCAL col AS LONG
   FOR row = 0 TO nWidth - 1 STEP 2
      FOR col = 0 TO nHeight STEP 2
         myBitmap.SetPixel(row, col, pixelColor)
      NEXT
   NEXT

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

END SUB
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS WSTRINGZ PTR, BYVAL nCmdShow AS LONG) AS LONG

   ' // Create an instance of the CWindow class
   LOCAL pWindow AS IWindow
   pWindow = CLASS "CWindow"
   IF ISNOTHING(pWindow) THEN EXIT FUNCTION

   ' // Create the main window
   LOCAL hwnd AS DWORD
   hwnd = pWindow.CreateWindow(%NULL, "GdipBitmapSetPixel", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
   pWindow.SetClientSize 400, 250
   ' // Center the window
   pWindow.CenterWindow

   ' // Create an instance of the GdiPlus class
   LOCAL pGdip AS IGdiPlus
   pGdip = NewGdiPlus

   ' // Add a graphic control
   LOCAL hCtrl AS DWORD
   hCtrl = pWindow.AddGraphCtx(hwnd, %IDC_GRCTX, "", 0, 0, pWindow.ClientWidth, pWindow.ClientHeight)
   GraphCtx_Clear(hCtrl, %WHITE)

   ' // Get the memory device context of the graphic control
   LOCAL hdc AS DWORD
   hdc = GraphCtx_GetDc(hCtrl)

   ' // Draw the graphics
   Example_SetPixel(pGdip, hdc)

   ' // Default message pump (you can replace it with your own)
   pWindow.DoEvents(nCmdShow)

END FUNCTION
' ========================================================================================

' ========================================================================================
' Main callback function.
' ========================================================================================
FUNCTION WindowProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG

   SELECT CASE uMsg

      CASE %WM_COMMAND
         SELECT CASE LO(WORD, wParam)
            CASE %IDCANCEL
               ' // If the Escape key has been pressed...
               IF HI(WORD, wParam) = %BN_CLICKED THEN
                  ' // ... close the application by sending a WM_CLOSE message
                  SendMessage hwnd, %WM_CLOSE, 0, 0
               END IF
         END SELECT

      CASE %WM_DESTROY
         ' // End the application
         PostQuitMessage 0
         EXIT FUNCTION

   END SELECT

   ' // Pass unprocessed messages to Windows
   FUNCTION = DefWindowProc(hwnd, uMsg, wParam, lParam)

END FUNCTION
' ========================================================================================

Flat API version

Code: [Select]
' ########################################################################################
' Microsoft Windows
' File: GDIP_BitmapSetPixel.bas
' Contents: GDI+ example
' This version uses the CWindow class, the GraphCtx graphic control and the GDI+ Flat API
' Compilers: PBWIN 10+, PBCC 6+
' Headers: Windows API headers 2.03+
' Copyright (c) 2011 José Roca. Freeware. Use at your own risk.
' Portions Copyright (c) Microsoft Corporation. All Rights Reserved.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################

#COMPILE EXE
#DIM ALL
%UNICODE = 1
%USEGRAPHCTX = 1

' // Header files for imported files
#INCLUDE ONCE "CWindow.inc"    ' // CWindow class
#INCLUDE ONCE "GdiPlus.inc"    ' // GdiPlus API

%IDC_GRCTX = 1001

' ========================================================================================
' The following example creates a Bitmap object 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.
' ========================================================================================
SUB Example_SetPixel (BYVAL hdc AS DWORD)

'   Graphics graphics(hdc);
   LOCAL graphics AS DWORD
   GdipCreateFromHDC(hdc, graphics)

'   // Create a Bitmap object from a JPEG file.
'   Bitmap myBitmap(L"Climber.jpg");
   LOCAL myBitmap AS DWORD
   GdipCreateBitmapFromFile("climber.jpg", myBitmap)

'   // Draw the bitmap.
'   graphics.DrawImage(&myBitmap, 0, 0);
   GdipDrawImage(graphics, 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));
'      }
'   }

   ' // Get the width and height of the bitmap
   LOCAL nWidth AS DWORD
   LOCAL nHeight AS DWORD
   GdipGetImageWidth(myBitmap, nWidth)
   GdipGetImageHeight(myBitmap, nHeight)

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

   LOCAL row AS LONG
   LOCAL col AS LONG
   FOR row = 0 TO nWidth - 1 STEP 2
      FOR col = 0 TO nHeight STEP 2
         GdipBitmapSetPixel(myBitmap, row, col, pixelColor)
      NEXT
   NEXT

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

   ' // Cleanup
   GdipDisposeImage(myBitmap)
   GdipDeleteGraphics(graphics)

END SUB
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS WSTRINGZ PTR, BYVAL nCmdShow AS LONG) AS LONG

   ' // Create an instance of the CWindow class
   LOCAL pWindow AS IWindow
   pWindow = CLASS "CWindow"
   IF ISNOTHING(pWindow) THEN EXIT FUNCTION

   ' // Create the main window
   LOCAL hwnd AS DWORD
   hwnd = pWindow.CreateWindow(%NULL, "GdipBitmapSetPixel", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
   pWindow.SetClientSize 400, 250
   ' // Center the window
   pWindow.CenterWindow

   ' // Add a GDI+ aware graphic control
   LOCAL hCtrl AS DWORD
   hCtrl = pWindow.AddGdipGraphCtx(hwnd, %IDC_GRCTX, "", 0, 0, pWindow.ClientWidth, pWindow.ClientHeight)
   GraphCtx_Clear(hCtrl, %WHITE)

   ' // Get the memory device context of the graphic control
   LOCAL hdc AS DWORD
   hdc = GraphCtx_GetDc(hCtrl)

   ' // Draw the graphics
   Example_SetPixel(hdc)

   ' // Default message pump (you can replace it with your own)
   pWindow.DoEvents(nCmdShow)

END FUNCTION
' ========================================================================================

' ========================================================================================
' Main callback function.
' ========================================================================================
FUNCTION WindowProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG

   SELECT CASE uMsg

      CASE %WM_COMMAND
         SELECT CASE LO(WORD, wParam)
            CASE %IDCANCEL
               ' // If the Escape key has been pressed...
               IF HI(WORD, wParam) = %BN_CLICKED THEN
                  ' // ... close the application by sending a WM_CLOSE message
                  SendMessage hwnd, %WM_CLOSE, 0, 0
               END IF
         END SELECT

      CASE %WM_DESTROY
         ' // End the application
         PostQuitMessage 0
         EXIT FUNCTION

   END SELECT

   ' // Pass unprocessed messages to Windows
   FUNCTION = DefWindowProc(hwnd, uMsg, wParam, lParam)

END FUNCTION
' ========================================================================================

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
  • User-Rate: +204/-0
GDI+: CreateBitmapFromResource Example
« Reply #6 on: November 04, 2011, 12:27:34 AM »
 
The following example creates a Bitmap based on an application instance handle and the name of a bitmap resource.

Code: [Select]
' ########################################################################################
' Microsoft Windows
' File: CGDIP_CreateBitmapFromResource.bas
' Contents: GDI+ example
' This version uses the CWindow and CGdiPlus classes, and the GraphCtx graphic control
' Compilers: PBWIN 10+, PBCC 6+
' Headers: Windows API headers 2.03+
' Copyright (c) 2011 José Roca. Freeware. Use at your own risk.
' Portions Copyright (c) Microsoft Corporation. All Rights Reserved.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################

#COMPILE EXE
#DIM ALL
%UNICODE = 1
%USEGRAPHCTX = 1

' // Header files for imported files
#INCLUDE ONCE "CWindow.inc"    ' // CWindow class
#INCLUDE ONCE "CGdiPlus.inc"   ' // CWindow class
#RESOURCE RES, "CGDIP_CreateBitmapFromResource.res"

%IDC_GRCTX = 1001

' ========================================================================================
' The following example creates a Bitmap based on an application instance handle and the
' name of a bitmap resource.
' ========================================================================================
SUB GDIP_BmpFromResource (BYVAL pGdip AS IGdiPlus, BYVAL hdc AS DWORD)

   ' // Create a Graphics pbject
   LOCAL graphics AS IGdipGraphics
   graphics = pGdip.Graphics(hdc)

   ' // Create a bitmap from a resource
   LOCAL pBitmap AS IGdipBitmap
   pBitmap = pGdip.BitmapFromResource(GetModuleHandle(BYVAL %NULL), "BitmapResource1")

   ' // Draw the bitmap
   graphics.DrawImage(pBitmap, 10, 10)

END SUB
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS WSTRINGZ PTR, BYVAL nCmdShow AS LONG) AS LONG

   ' // Create an instance of the CWindow class
   LOCAL pWindow AS IWindow
   pWindow = CLASS "CWindow"
   IF ISNOTHING(pWindow) THEN EXIT FUNCTION

   ' // Create the main window
   LOCAL hwnd AS DWORD
   hwnd = pWindow.CreateWindow(%NULL, "GdipCreateBitmapFromResource", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
   pWindow.SetClientSize 400, 250
   ' // Center the window
   pWindow.CenterWindow

   ' // Create an instance of the GdiPlus class
   LOCAL pGdip AS IGdiPlus
   pGdip = NewGdiPlus

   ' // Add a graphic control
   LOCAL hCtrl AS DWORD
   hCtrl = pWindow.AddGraphCtx(hwnd, %IDC_GRCTX, "", 0, 0, pWindow.ClientWidth, pWindow.ClientHeight)
   GraphCtx_Clear(hCtrl, %WHITE)

   ' // Get the memory device context of the graphic control
   LOCAL hdc AS DWORD
   hdc = GraphCtx_GetDc(hCtrl)

   ' // Draw the graphics
   GDIP_BmpFromResource(pGdip, hdc)

   ' // Default message pump (you can replace it with your own)
   pWindow.DoEvents(nCmdShow)

END FUNCTION
' ========================================================================================

' ========================================================================================
' Main callback function.
' ========================================================================================
FUNCTION WindowProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG

   SELECT CASE uMsg

      CASE %WM_COMMAND
         SELECT CASE LO(WORD, wParam)
            CASE %IDCANCEL
               ' // If the Escape key has been pressed...
               IF HI(WORD, wParam) = %BN_CLICKED THEN
                  ' // ... close the application by sending a WM_CLOSE message
                  SendMessage hwnd, %WM_CLOSE, 0, 0
               END IF
         END SELECT

      CASE %WM_DESTROY
         ' // End the application
         PostQuitMessage 0
         EXIT FUNCTION

   END SELECT

   ' // Pass unprocessed messages to Windows
   FUNCTION = DefWindowProc(hwnd, uMsg, wParam, lParam)

END FUNCTION
' ========================================================================================

Flat API version

Code: [Select]
' ########################################################################################
' Microsoft Windows
' File: GDIP_CreateBitmapFromResource.bas
' Contents: GDI+ example
' This version uses the CWindow class, the GraphCtx graphic control and the GDI+ Flat API
' Compilers: PBWIN 10+, PBCC 6+
' Headers: Windows API headers 2.03+
' Copyright (c) 2011 José Roca. Freeware. Use at your own risk.
' Portions Copyright (c) Microsoft Corporation. All Rights Reserved.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################

#COMPILE EXE
#DIM ALL
%UNICODE = 1
%USEGRAPHCTX = 1

' // Header files for imported files
#INCLUDE ONCE "CWindow.inc"    ' // CWindow class
#INCLUDE ONCE "GdiPlus.inc"    ' // GdipPlus API
#RESOURCE RES, "CGDIP_CreateBitmapFromResource.res"

%IDC_GRCTX = 1001

' ========================================================================================
' The following example creates a Bitmap based on an application instance handle and the
' name of a bitmap resource.
' ========================================================================================
SUB GDIP_BmpFromResource (BYVAL hdc AS DWORD)

   ' // Create a Graphics pbject
   LOCAL graphics AS DWORD
   GdipCreateFromHDC(hdc, graphics)

   ' // Create a bitmap from a resource
   LOCAL pBitmap AS DWORD
   GdipCreateBitmapFromResource(GetModuleHandle(BYVAL %NULL), "BitmapResource1", pBitmap)

   ' // Draw the bitmap
   GdipDrawImage(graphics, pBitmap, 10, 10)

   ' // Cleanup
   GdipDisposeImage(pBitmap)
   GdipDeleteGraphics(graphics)

END SUB
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS WSTRINGZ PTR, BYVAL nCmdShow AS LONG) AS LONG

   ' // Create an instance of the CWindow class
   LOCAL pWindow AS IWindow
   pWindow = CLASS "CWindow"
   IF ISNOTHING(pWindow) THEN EXIT FUNCTION

   ' // Create the main window
   LOCAL hwnd AS DWORD
   hwnd = pWindow.CreateWindow(%NULL, "GdipCreateBitmapFromResource", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
   pWindow.SetClientSize 400, 250
   ' // Center the window
   pWindow.CenterWindow

   ' // Add a GDI+ aware graphic control
   LOCAL hCtrl AS DWORD
   hCtrl = pWindow.AddGdipGraphCtx(hwnd, %IDC_GRCTX, "", 0, 0, pWindow.ClientWidth, pWindow.ClientHeight)
   GraphCtx_Clear(hCtrl, %WHITE)

   ' // Get the memory device context of the graphic control
   LOCAL hdc AS DWORD
   hdc = GraphCtx_GetDc(hCtrl)

   ' // Draw the graphics
   GDIP_BmpFromResource(hdc)

   ' // Default message pump (you can replace it with your own)
   pWindow.DoEvents(nCmdShow)

END FUNCTION
' ========================================================================================

' ========================================================================================
' Main callback function.
' ========================================================================================
FUNCTION WindowProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG

   SELECT CASE uMsg

      CASE %WM_COMMAND
         SELECT CASE LO(WORD, wParam)
            CASE %IDCANCEL
               ' // If the Escape key has been pressed...
               IF HI(WORD, wParam) = %BN_CLICKED THEN
                  ' // ... close the application by sending a WM_CLOSE message
                  SendMessage hwnd, %WM_CLOSE, 0, 0
               END IF
         END SELECT

      CASE %WM_DESTROY
         ' // End the application
         PostQuitMessage 0
         EXIT FUNCTION

   END SELECT

   ' // Pass unprocessed messages to Windows
   FUNCTION = DefWindowProc(hwnd, uMsg, wParam, lParam)

END FUNCTION
' ========================================================================================

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
  • User-Rate: +204/-0
GDI+: CreateBitmapFromStream Example
« Reply #7 on: November 04, 2011, 12:35:58 AM »
 
The following example demonstrates the use of the BitmapFromStream method. Its main use is to load images stored in compound files, but in this example we will create an stream on global memory from the contents of a disk file.

Code: [Select]
' ########################################################################################
' Microsoft Windows
' File: CGDIP_CreateBitmapFromStream.bas
' Contents: GDI+ example
' This version uses the CWindow and CGdiPlus classes, and the GraphCtx graphic control
' Compilers: PBWIN 10+, PBCC 6+
' Headers: Windows API headers 2.03+
' Copyright (c) 2011 José Roca. Freeware. Use at your own risk.
' Portions Copyright (c) Microsoft Corporation. All Rights Reserved.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################

#COMPILE EXE
#DIM ALL
%UNICODE = 1
%USEGRAPHCTX = 1

' // Header files for imported files
#INCLUDE ONCE "CWindow.inc"    ' // CWindow class
#INCLUDE ONCE "CGdiPlus.inc"   ' // CWindow class

%IDC_GRCTX = 1001

' ========================================================================================
' The following example demonstrates the use of the BitmapFromStream method.
' Its main use is to load images stored in compound files, but in this example we will
' create an stream on global memory from the contents of a disk file.
' ========================================================================================
SUB GDIP_CreateBitmapFromStream (BYVAL pGdip AS IGdiPlus, BYVAL hdc AS DWORD)

   LOCAL pGraphics AS IGdipGraphics
   pGraphics = pGdip.Graphics(hdc)

   ' // Load the image in a string buffer
   LOCAL hFile AS LONG
   LOCAL imageSize AS DWORD
   LOCAL strBuffer AS STRING
   LOCAL strFileName AS STRING

   strFilename = "RYDER_Winona_01.jpg"
   hFile = FREEFILE
   OPEN strFilename FOR BINARY ACCESS READ SHARED AS #hFile
      imageSize = LOF(hFile)
      strBuffer = SPACE$(imageSize)
      GET #hFile, , strBuffer
   CLOSE #hFile

   LOCAL hGlobal AS DWORD
   LOCAL pGlobalBuffer AS DWORD
   LOCAL pImageStream AS IStream
   LOCAL pBitmap AS IGdipBitmap

   ' // Allocate memory to hold the image
   hGlobal = GlobalAlloc(%GMEM_MOVEABLE, imageSize)
   IF hGlobal THEN
      ' // Lock the memory
      pGlobalBuffer = GlobalLock(hGlobal)
      IF pGlobalBuffer THEN
         ' // Copy the image from the string buffer to global memory
         CopyMemory pGlobalBuffer, STRPTR(strBuffer), imageSize
         ' // Create an stream in global memory
         IF CreateStreamOnHGlobal(hGlobal, %FALSE, pImageStream) = %S_OK THEN
            ' // Create a bitmap from the data contained in the stream
            pBitmap = pGdip.BitmapFromStream(pImageStream)
            IF ISOBJECT(pBitmap) THEN
               ' // Draw the image
               pGraphics.DrawImage(pBitmap, 10, 10)
            END IF
            ' // Release the bitmap
            pBitmap = NOTHING
            ' // Release the stream
            pImageStream = NOTHING
         END IF
         ' // Unlock the memory
         GlobalUnlock pGlobalBuffer
      END IF
      ' // Free the memory
      GlobalFree hGlobal
   END IF

END SUB
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS ASCIIZ PTR, BYVAL nCmdShow AS LONG) AS LONG

   ' // Create an instance of the CWindow class
   LOCAL pWindow AS IWindow
   pWindow = CLASS "CWindow"
   IF ISNOTHING(pWindow) THEN EXIT FUNCTION

   ' // Create the main window
   LOCAL hwnd AS DWORD
   hwnd = pWindow.CreateWindow(%NULL, "GdipCreateBitmapFromStream", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
   pWindow.SetClientSize 326, 352
   ' // Center the window
   pWindow.CenterWindow

   ' // Create an instance of the GdiPlus class
   LOCAL pGdip AS IGdiPlus
   pGdip = NewGdiPlus

   ' // Add a graphic control
   LOCAL hCtrl AS DWORD
   hCtrl = pWindow.AddGraphCtx(hwnd, %IDC_GRCTX, "", 0, 0, pWindow.ClientWidth, pWindow.ClientHeight)
   GraphCtx_Clear(hCtrl, %WHITE)

   ' // Get the memory device context of the graphic control
   LOCAL hdc AS DWORD
   hdc = GraphCtx_GetDc(hCtrl)

   ' // Draw the graphics
   GDIP_CreateBitmapFromStream(pGdip, hdc)

   ' // Default message pump (you can replace it with your own)
   pWindow.DoEvents(nCmdShow)

END FUNCTION
' ========================================================================================

' ========================================================================================
' Main callback function.
' ========================================================================================
FUNCTION WindowProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG

   LOCAL hCtrl AS DWORD
   LOCAL hDC AS DWORD

   SELECT CASE uMsg

      CASE %WM_COMMAND
         SELECT CASE LO(WORD, wParam)
            CASE %IDCANCEL
               IF HI(WORD, wParam) = %BN_CLICKED THEN
                  SendMessage hwnd, %WM_CLOSE, 0, 0
               END IF
         END SELECT

      CASE %WM_DESTROY
         ' // Close the main window
         PostQuitMessage 0
         EXIT FUNCTION

   END SELECT

   FUNCTION = DefWindowProc(hwnd, uMsg, wParam, lParam)

END FUNCTION
' ========================================================================================

Flat API version

Code: [Select]
' ########################################################################################
' Microsoft Windows
' File: GDIP_CreateBitmapFromStream.bas
' Contents: GDI+ example
' This version uses the CWindow class, the GraphCtx graphic control and the GDI+ Flat API
' Compilers: PBWIN 10+, PBCC 6+
' Headers: Windows API headers 2.03+
' Copyright (c) 2011 José Roca. Freeware. Use at your own risk.
' Portions Copyright (c) Microsoft Corporation. All Rights Reserved.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################

#COMPILE EXE
#DIM ALL
%UNICODE = 1
%USEGRAPHCTX = 1

' // Header files for imported files
#INCLUDE ONCE "CWindow.inc"    ' // CWindow class
#INCLUDE ONCE "GdiPlus.inc"    ' // GdipPlus API

%IDC_GRCTX = 1001

' ========================================================================================
' The following example demonstrates the use of the GdipCreateBitmapFromStream function.
' Its main use is to load images stored in compound files, but in this example we will
' create an stream on global memory from the contents of a disk file.
' ========================================================================================
SUB GDIP_CreateBitmapFromStream (BYVAL hdc AS DWORD)

   LOCAL hStatus AS LONG
   LOCAL pGraphics AS DWORD
   GdipCreateFromHDC(hdc, pGraphics)

   ' // Load the image in a string buffer
   LOCAL hFile AS LONG
   LOCAL imageSize AS DWORD
   LOCAL strBuffer AS STRING
   LOCAL strFileName AS STRING

   strFilename = "RYDER_Winona_01.jpg"
   hFile = FREEFILE
   OPEN strFilename FOR BINARY ACCESS READ SHARED AS #hFile
      imageSize = LOF(hFile)
      strBuffer = SPACE$(imageSize)
      GET #hFile, , strBuffer
   CLOSE #hFile

   LOCAL hGlobal AS DWORD
   LOCAL pGlobalBuffer AS DWORD
   LOCAL pImageStream AS IStream
   LOCAL pBitmap AS DWORD

   ' // Allocate memory to hold the image
   hGlobal = GlobalAlloc(%GMEM_MOVEABLE, imageSize)
   IF hGlobal THEN
      ' // Lock the memory
      pGlobalBuffer = GlobalLock(hGlobal)
      IF pGlobalBuffer THEN
         ' // Copy the image from the string buffer to global memory
         CopyMemory pGlobalBuffer, STRPTR(strBuffer), imageSize
         ' // Create an stream in global memory
         IF CreateStreamOnHGlobal(hGlobal, %FALSE, pImageStream) = %S_OK THEN
            ' // Create a bitmap from the data contained in the stream
            hStatus = GdipCreateBitmapFromStream(pImageStream, pBitmap)
            IF hStatus = %StatusOk THEN
               ' // Draw the image
               GdipDrawImageI(pGraphics, pBitmap, 10, 10)
            END IF
            ' // Release the bitmap
            GdipDisposeImage(pBitmap)
            ' // Release the stream
            pImageStream = NOTHING
         END IF
         ' // Unlock the memory
         GlobalUnlock pGlobalBuffer
      END IF
      ' // Free the memory
      GlobalFree hGlobal
   END IF

   ' // Cleanup
   GdipDeleteGraphics(pGraphics)

END SUB
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS ASCIIZ PTR, BYVAL nCmdShow AS LONG) AS LONG

   ' // Create an instance of the CWindow class
   LOCAL pWindow AS IWindow
   pWindow = CLASS "CWindow"
   IF ISNOTHING(pWindow) THEN EXIT FUNCTION

   ' // Create the main window
   LOCAL hwnd AS DWORD
   hwnd = pWindow.CreateWindow(%NULL, "GdipCreateBitmapFromStream", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
   pWindow.SetClientSize 326, 352
   ' // Center the window
   pWindow.CenterWindow

   ' // Add a GDI+ aware graphic control
   LOCAL hCtrl AS DWORD
   hCtrl = pWindow.AddGdipGraphCtx(hwnd, %IDC_GRCTX, "", 0, 0, pWindow.ClientWidth, pWindow.ClientHeight)
   GraphCtx_Clear(hCtrl, %WHITE)

   ' // Get the memory device context of the graphic control
   LOCAL hdc AS DWORD
   hdc = GraphCtx_GetDc(hCtrl)

   ' // Draw the graphics
   GDIP_CreateBitmapFromStream(hdc)

   ' // Default message pump (you can replace it with your own)
   pWindow.DoEvents(nCmdShow)

END FUNCTION
' ========================================================================================

' ========================================================================================
' Main callback function.
' ========================================================================================
FUNCTION WindowProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG

   LOCAL hCtrl AS DWORD
   LOCAL hDC AS DWORD

   SELECT CASE uMsg

      CASE %WM_COMMAND
         SELECT CASE LO(WORD, wParam)
            CASE %IDCANCEL
               IF HI(WORD, wParam) = %BN_CLICKED THEN
                  SendMessage hwnd, %WM_CLOSE, 0, 0
               END IF
         END SELECT

      CASE %WM_DESTROY
         ' // Close the main window
         PostQuitMessage 0
         EXIT FUNCTION

   END SELECT

   FUNCTION = DefWindowProc(hwnd, uMsg, wParam, lParam)

END FUNCTION
' ========================================================================================

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
  • User-Rate: +204/-0
GDI+: CreateBitmapFromStream Example (2)
« Reply #8 on: November 04, 2011, 12:38:57 AM »
 
The following example creates two Image objects. Each Image object is based on a stream that is part of a compound file. The code calls StgOpenStorage to open the compound file and get a pointer to its IStorage interface. Then the code calls IStorage.OpenStream to get a pointer to an IStream interface that represents one of the streams in the compound file. The code constructs an Image object based on that IStream pointer and then calls DrawImage method to display the image on the screen. The code uses a similar process to construct an Image object based on a second stream that is part of the same compound file.

Code: [Select]
' ########################################################################################
' Microsoft Windows
' File: CGDIP_CreateBitmapFromStream.bas
' Contents: GDI+ example
' This version uses the CWindow and CGdiPlus classes, and the GraphCtx graphic control
' Compilers: PBWIN 10+, PBCC 6+
' Headers: Windows API headers 2.03+
' Copyright (c) 2011 José Roca. Freeware. Use at your own risk.
' Portions Copyright (c) Microsoft Corporation. All Rights Reserved.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################

#COMPILE EXE
#DIM ALL
%UNICODE = 1
%USEGRAPHCTX = 1

' // Header files for imported files
#INCLUDE ONCE "CWindow.inc"    ' // CWindow class
#INCLUDE ONCE "CGdiPlus.inc"   ' // CWindow class

%IDC_GRCTX = 1001

' ========================================================================================
' The following example creates two Image objects. Each Image object is based on a stream
' that is part of a compound file. The code calls StgOpenStorage to open the compound file
' and get a pointer to its IStorage interface. Then the code calls IStorage.OpenStream to get
' a pointer to an IStream interface that represents one of the streams in the compound file.
' The code constructs an Image object based on that IStream pointer and then calls
' DrawImage method to display the image on the screen. The code uses a similar process to
' construct an Image object based on a second stream that is part of the same compound file.
' ========================================================================================
SUB GDIP_CreateBitmapFromStream (BYVAL pGdip AS IGdiPlus, BYVAL hdc AS DWORD)

   LOCAL pGraphics AS IGdipGraphics
   pGraphics = pGdip.Graphics(hdc)

   ' // Open an existing compound file, and get a pointer
   ' // to its IStorage interface.
   LOCAL hr AS LONG
   LOCAL pStorage AS IStorage
   LOCAL wszName AS WSTRINGZ * %MAX_PATH
   wszName = "CompoundFile.cmp"
   hr = StgOpenStorage(wszName, NOTHING, %STGM_READ OR %STGM_SHARE_EXCLUSIVE, 0, %NULL, pStorage)
   IF FAILED(hr) THEN
      MSGBOX "StgOpenStorage failure: " & HEX$(hr)
      EXIT SUB
   END IF

   ' // Get a pointer to the stream StreamImage1 in the compound file.
   LOCAL pStream1 AS IStream
   wszName = "StreamImage1"
   hr = pStorage.OpenStream(wszName, 0, %STGM_READ OR %STGM_SHARE_EXCLUSIVE, 0, pStream1)
   IF FAILED(hr) THEN
      MSGBOX "IStorage.OpenStream failure: " & HEX$(hr)
      EXIT SUB
   END IF

   ' // Get a pointer to the stream StreamImage2 in the compound file.
   LOCAL pStream2 AS IStream
   wszName = "StreamImage2"
   hr = pStorage.OpenStream(wszName, 0, %STGM_READ OR %STGM_SHARE_EXCLUSIVE, 0, pStream2)
   IF FAILED(hr) THEN
      MSGBOX "IStorage.OpenStream failure: " & HEX$(hr)
      EXIT SUB
   END IF

   ' // Construct a new Image object based on StreamImage1.
   LOCAL pBitmap1 AS IGdipBitmap
   pBitmap1 = pGdip.BitmapFromStream(pStream1)
   IF ISNOTHING(pBitmap1) THEN EXIT SUB

   ' // Draw the image
   pGraphics.DrawImage(pBitmap1, 10, 10)

   ' // Construct a new Image object based on StreamImage2.
   LOCAL pBitmap2 AS IGdipBitmap
   pBitmap2 = pGdip.BitmapFromStream(pStream2)
   IF ISNOTHING(pBitmap2) THEN EXIT SUB

   ' // Draw the image
   pGraphics.DrawImage(pBitmap2, 200, 10)

END SUB
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS ASCIIZ PTR, BYVAL nCmdShow AS LONG) AS LONG

   ' // Create an instance of the CWindow class
   LOCAL pWindow AS IWindow
   pWindow = CLASS "CWindow"
   IF ISNOTHING(pWindow) THEN EXIT FUNCTION

   ' // Create the main window
   LOCAL hwnd AS DWORD
   hwnd = pWindow.CreateWindow(%NULL, "GdipCreateBitmapFromStream", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
   pWindow.SetClientSize 400, 250
   ' // Center the window
   pWindow.CenterWindow

   ' // Create an instance of the GdiPlus class
   LOCAL pGdip AS IGdiPlus
   pGdip = NewGdiPlus

   ' // Add a graphic control
   LOCAL hCtrl AS DWORD
   hCtrl = pWindow.AddGraphCtx(hwnd, %IDC_GRCTX, "", 0, 0, pWindow.ClientWidth, pWindow.ClientHeight)
   GraphCtx_Clear(hCtrl, %WHITE)

   ' // Get the memory device context of the graphic control
   LOCAL hdc AS DWORD
   hdc = GraphCtx_GetDc(hCtrl)

   ' // Draw the graphics
   GDIP_CreateBitmapFromStream(pGdip, hdc)

   ' // Default message pump (you can replace it with your own)
   pWindow.DoEvents(nCmdShow)

END FUNCTION
' ========================================================================================

' ========================================================================================
' Main callback function.
' ========================================================================================
FUNCTION WindowProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG

   LOCAL hCtrl AS DWORD
   LOCAL hDC AS DWORD

   SELECT CASE uMsg

      CASE %WM_COMMAND
         SELECT CASE LO(WORD, wParam)
            CASE %IDCANCEL
               IF HI(WORD, wParam) = %BN_CLICKED THEN
                  SendMessage hwnd, %WM_CLOSE, 0, 0
               END IF
         END SELECT

      CASE %WM_DESTROY
         ' // Close the main window
         PostQuitMessage 0
         EXIT FUNCTION

   END SELECT

   FUNCTION = DefWindowProc(hwnd, uMsg, wParam, lParam)

END FUNCTION
' ========================================================================================

Flat API version

Code: [Select]
' ########################################################################################
' Microsoft Windows
' File: GDIP_CreateBitmapFromStream.bas
' Contents: GDI+ example
' This version uses the CWindow class, the GraphCtx graphic control and the GDI+ Flat API
' Compilers: PBWIN 10+, PBCC 6+
' Headers: Windows API headers 2.03+
' Copyright (c) 2011 José Roca. Freeware. Use at your own risk.
' Portions Copyright (c) Microsoft Corporation. All Rights Reserved.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################

#COMPILE EXE
#DIM ALL
%UNICODE = 1
%USEGRAPHCTX = 1

' // Header files for imported files
#INCLUDE ONCE "CWindow.inc"    ' // CWindow class
#INCLUDE ONCE "GdiPlus.inc"    ' // GdipPlus API

%IDC_GRCTX = 1001

' ========================================================================================
' The following example creates two Image objects. Each Image object is based on a stream
' that is part of a compound file. The code calls StgOpenStorage to open the compound file
' and get a pointer to its IStorage interface. Then the code calls IStorage.OpenStream to get
' a pointer to an IStream interface that represents one of the streams in the compound file.
' The code constructs an Image object based on that IStream pointer and then calls
' GdripDrawImage to display the image on the screen. The code uses a similar process to
' construct an Image object based on a second stream that is part of the same compound file.
' ========================================================================================
SUB GDIP_CreateBitmapFromStream (BYVAL hdc AS DWORD)

   LOCAL hStatus AS DWORD
   LOCAL pGraphics AS DWORD
   hStatus = GdipCreateFromHDC(hdc, pGraphics)

   ' // Open an existing compound file, and get a pointer
   ' // to its IStorage interface.
   LOCAL hr AS LONG
   LOCAL pStorage AS IStorage
   LOCAL wszName AS WSTRINGZ * %MAX_PATH
   wszName = "CompoundFile.cmp"
   hr = StgOpenStorage(wszName, NOTHING, %STGM_READ OR %STGM_SHARE_EXCLUSIVE, 0, %NULL, pStorage)
   IF FAILED(hr) THEN
      MSGBOX "StgOpenStorage failure: " & HEX$(hr)
      EXIT SUB
   END IF

   ' // Get a pointer to the stream StreamImage1 in the compound file.
   LOCAL pStream1 AS IStream
   wszName = "StreamImage1"
   hr = pStorage.OpenStream(wszName, 0, %STGM_READ OR %STGM_SHARE_EXCLUSIVE, 0, pStream1)
   IF FAILED(hr) THEN
      MSGBOX "IStorage.OpenStream failure: " & HEX$(hr)
      EXIT SUB
   END IF

   ' // Get a pointer to the stream StreamImage2 in the compound file.
   LOCAL pStream2 AS IStream
   wszName = "StreamImage2"
   hr = pStorage.OpenStream(wszName, 0, %STGM_READ OR %STGM_SHARE_EXCLUSIVE, 0, pStream2)
   IF FAILED(hr) THEN
      MSGBOX "IStorage.OpenStream failure: " & HEX$(hr)
      EXIT SUB
   END IF

   ' // Construct a new Image object based on StreamImage1.
   LOCAL pBitmap1 AS DWORD
   hStatus = GdipCreateBitmapFromStream(pStream1, pBitmap1)
   IF hStatus THEN
      GdipDeleteGraphics(pGraphics)
      EXIT SUB
   END IF

   ' // Draw the image
   GdipDrawImage(pGraphics, pBitmap1, 10, 10)

   ' // Construct a new Image object based on StreamImage2.
   LOCAL pBitmap2 AS DWORD
   hStatus = GdipCreateBitmapFromStream(pStream2, pBitmap2)
   IF hStatus THEN
      GdipDeleteGraphics(pGraphics)
      EXIT SUB
   END IF

   ' // Draw the image
   GdipDrawImage(pGraphics, pBitmap2, 200, 10)

   ' // Cleanup
   GdipDisposeImage(pBitmap1)
   GdipDisposeImage(pBitmap2)
   GdipDeleteGraphics(pGraphics)

END SUB
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS ASCIIZ PTR, BYVAL nCmdShow AS LONG) AS LONG

   ' // Create an instance of the CWindow class
   LOCAL pWindow AS IWindow
   pWindow = CLASS "CWindow"
   IF ISNOTHING(pWindow) THEN EXIT FUNCTION

   ' // Create the main window
   LOCAL hwnd AS DWORD
   hwnd = pWindow.CreateWindow(%NULL, "GdipCreateBitmapFromStream", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
   pWindow.SetClientSize 400, 250
   ' // Center the window
   pWindow.CenterWindow

   ' // Add a GDI+ aware graphic control
   LOCAL hCtrl AS DWORD
   hCtrl = pWindow.AddGdipGraphCtx(hwnd, %IDC_GRCTX, "", 0, 0, pWindow.ClientWidth, pWindow.ClientHeight)
   GraphCtx_Clear(hCtrl, %WHITE)

   ' // Get the memory device context of the graphic control
   LOCAL hdc AS DWORD
   hdc = GraphCtx_GetDc(hCtrl)

   ' // Draw the graphics
   GDIP_CreateBitmapFromStream(hdc)

   ' // Default message pump (you can replace it with your own)
   pWindow.DoEvents(nCmdShow)

END FUNCTION
' ========================================================================================

' ========================================================================================
' Main callback function.
' ========================================================================================
FUNCTION WindowProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG

   LOCAL hCtrl AS DWORD
   LOCAL hDC AS DWORD

   SELECT CASE uMsg

      CASE %WM_COMMAND
         SELECT CASE LO(WORD, wParam)
            CASE %IDCANCEL
               IF HI(WORD, wParam) = %BN_CLICKED THEN
                  SendMessage hwnd, %WM_CLOSE, 0, 0
               END IF
         END SELECT

      CASE %WM_DESTROY
         ' // Close the main window
         PostQuitMessage 0
         EXIT FUNCTION

   END SELECT

   FUNCTION = DefWindowProc(hwnd, uMsg, wParam, lParam)

END FUNCTION
' ========================================================================================

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
  • User-Rate: +204/-0
GDI+: CloneArea Example
« Reply #9 on: November 04, 2011, 12:41:08 AM »
 
The following example creates a Bitmap from an image file, clones the upper-left portion of the image, and then draws the cloned image.

Code: [Select]
' ########################################################################################
' Microsoft Windows
' File: CGDIP_CloneArea.bas
' Contents: GDI+ example
' This version uses the CWindow and CGdiPlus classes, and the GraphCtx graphic control
' Compilers: PBWIN 10+, PBCC 6+
' Headers: Windows API headers 2.03+
' Copyright (c) 2011 José Roca. Freeware. Use at your own risk.
' Portions Copyright (c) Microsoft Corporation. All Rights Reserved.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################

#COMPILE EXE
#DIM ALL
%UNICODE = 1
%USEGRAPHCTX = 1

' // Header files for imported files
#INCLUDE ONCE "CWindow.inc"    ' // CWindow class
#INCLUDE ONCE "CGdiPlus.inc"   ' // CGdiPlus class

%IDC_GRCTX = 1001

' ========================================================================================
' The following example creates a Bitmap from an image file, clones the upper-left
' portion of the image, and then draws the cloned image.
' ========================================================================================
SUB Example_CloneArea (BYVAL pGdip AS IGdiPlus, BYVAL hdc AS DWORD)

'   Graphics graphics(hdc);
   LOCAL graphics AS IGdipGraphics
   graphics = pGdip.Graphics(hdc)

'   // Create a Bitmap object from a JPEG file.
'   Bitmap bitmap(L"Climber.jpg");
   LOCAL pBitmap AS IGdipBitmap
   pBitmap = pGdip.Bitmap("climber.jpg")

'   // Clone a portion of the bitmap.
'   Bitmap* clone;
'   clone = bitmap.Clone(0.0f, 0.0f, 100.0f, 100.0f, PixelFormatDontCare);
   LOCAL clone AS IGdipBitmap
   clone = pBitmap.CloneArea(0, 0, 100, 100, %PixelFormatDontCare)

'   // Draw the clone.
'   graphics.DrawImage(clone, 0, 0);
   graphics.DrawImage(clone, 0, 0)

'   delete clone;
   clone = NOTHING

END SUB
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS WSTRINGZ PTR, BYVAL nCmdShow AS LONG) AS LONG

   ' // Create an instance of the CWindow class
   LOCAL pWindow AS IWindow
   pWindow = CLASS "CWindow"
   IF ISNOTHING(pWindow) THEN EXIT FUNCTION

   ' // Create the main window
   LOCAL hwnd AS DWORD
   hwnd = pWindow.CreateWindow(%NULL, "GdipCloneBitmapArea", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
   pWindow.SetClientSize 400, 250
   ' // Center the window
   pWindow.CenterWindow

   ' // Create an instance of the GdiPlus class
   LOCAL pGdip AS IGdiPlus
   pGdip = NewGdiPlus

   ' // Add a graphic control
   LOCAL hCtrl AS DWORD
   hCtrl = pWindow.AddGraphCtx(hwnd, %IDC_GRCTX, "", 0, 0, pWindow.ClientWidth, pWindow.ClientHeight)
   GraphCtx_Clear(hCtrl, %WHITE)

   ' // Get the memory device context of the graphic control
   LOCAL hdc AS DWORD
   hdc = GraphCtx_GetDc(hCtrl)

   ' // Draw the graphics
   Example_CloneArea(pGdip, hdc)

   ' // Default message pump (you can replace it with your own)
   pWindow.DoEvents(nCmdShow)

END FUNCTION
' ========================================================================================

' ========================================================================================
' Main callback function.
' ========================================================================================
FUNCTION WindowProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG

   SELECT CASE uMsg

      CASE %WM_COMMAND
         SELECT CASE LO(WORD, wParam)
            CASE %IDCANCEL
               ' // If the Escape key has been pressed...
               IF HI(WORD, wParam) = %BN_CLICKED THEN
                  ' // ... close the application by sending a WM_CLOSE message
                  SendMessage hwnd, %WM_CLOSE, 0, 0
               END IF
         END SELECT

      CASE %WM_DESTROY
         ' // End the application
         PostQuitMessage 0
         EXIT FUNCTION

   END SELECT

   ' // Pass unprocessed messages to Windows
   FUNCTION = DefWindowProc(hwnd, uMsg, wParam, lParam)

END FUNCTION
' ========================================================================================

Flat API version

Code: [Select]
' ########################################################################################
' Microsoft Windows
' File: GDIP_CloneArea.bas
' Contents: GDI+ example
' This version uses the CWindow class, the GraphCtx graphic control and the GDI+ Flat API
' Compilers: PBWIN 10+, PBCC 6+
' Headers: Windows API headers 2.03+
' Copyright (c) 2011 José Roca. Freeware. Use at your own risk.
' Portions Copyright (c) Microsoft Corporation. All Rights Reserved.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################

#COMPILE EXE
#DIM ALL
%UNICODE = 1
%USEGRAPHCTX = 1

' // Header files for imported files
#INCLUDE ONCE "CWindow.inc"    ' // CWindow class
#INCLUDE ONCE "GdiPlus.inc"    ' // GdipPlus API

%IDC_GRCTX = 1001

' ========================================================================================
' The following example creates a Bitmap from an image file, clones the upper-left
' portion of the image, and then draws the cloned image.
' ========================================================================================
SUB Example_CloneArea (BYVAL hdc AS DWORD)

'   Graphics graphics(hdc);
   LOCAL graphics AS DWORD
   GdipCreateFromHDC(hdc, graphics)

'   // Create a Bitmap object from a JPEG file.
'   Bitmap bitmap(L"Climber.jpg");
   LOCAL pBitmap AS DWORD
   GdipCreateBitmapFromFile("climber.jpg", pBitmap)

'   // Clone a portion of the bitmap.
'   Bitmap* clone;
'   clone = bitmap.Clone(0.0f, 0.0f, 100.0f, 100.0f, PixelFormatDontCare);
   LOCAL clone AS DWORD
   GdipCloneBitmapAreaI(0, 0, 100, 100, %PixelFormatDontCare, pBitmap, clone)

'   // Draw the clone.
'   graphics.DrawImage(clone, 0, 0);
   GdipDrawImageI(graphics, clone, 0, 0)

'   delete clone;
   ' // Cleanup
   GdipDisposeImage(clone)
   GdipDisposeImage(pBitmap)
   GdipDeleteGraphics(graphics)

END SUB
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION WinMain (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS WSTRINGZ PTR, BYVAL nCmdShow AS LONG) AS LONG

   ' // Create an instance of the CWindow class
   LOCAL pWindow AS IWindow
   pWindow = CLASS "CWindow"
   IF ISNOTHING(pWindow) THEN EXIT FUNCTION

   ' // Create the main window
   LOCAL hwnd AS DWORD
   hwnd = pWindow.CreateWindow(%NULL, "GdipCloneBitmapArea", 0, 0, 0, 0, 0, 0, CODEPTR(WindowProc))
   pWindow.SetClientSize 400, 250
   ' // Center the window
   pWindow.CenterWindow

   ' // Add a GDI+ aware graphic control
   LOCAL hCtrl AS DWORD
   hCtrl = pWindow.AddGdipGraphCtx(hwnd, %IDC_GRCTX, "", 0, 0, pWindow.ClientWidth, pWindow.ClientHeight)
   GraphCtx_Clear(hCtrl, %WHITE)

   ' // Get the memory device context of the graphic control
   LOCAL hdc AS DWORD
   hdc = GraphCtx_GetDc(hCtrl)

   ' // Draw the graphics
   Example_CloneArea(hdc)

   ' // Default message pump (you can replace it with your own)
   pWindow.DoEvents(nCmdShow)

END FUNCTION
' ========================================================================================

' ========================================================================================
' Main callback function.
' ========================================================================================
FUNCTION WindowProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG

   SELECT CASE uMsg

      CASE %WM_COMMAND
         SELECT CASE LO(WORD, wParam)
            CASE %IDCANCEL
               ' // If the Escape key has been pressed...
               IF HI(WORD, wParam) = %BN_CLICKED THEN
                  ' // ... close the application by sending a WM_CLOSE message
                  SendMessage hwnd, %WM_CLOSE, 0, 0
               END IF
         END SELECT

      CASE %WM_DESTROY
         ' // End the application
         PostQuitMessage 0
         EXIT FUNCTION

   END SELECT

   ' // Pass unprocessed messages to Windows
   FUNCTION = DefWindowProc(hwnd, uMsg, wParam, lParam)

END FUNCTION
' ========================================================================================