Locking Pixel Data for Writing from a User Input Buffer
' ########################################################################################
' 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
' ########################################################################################
' 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
' ========================================================================================