Windows applets-pile up monochrome BMP bitmap with text

Source: Internet
Author: User
Tags bmp image image to text readfile
Not updated for a long time ...... Paste a recent image to text size item = w =
# Ifndef UNICODE # define UNICODE # endif # ifndef _ UNICODE // # define _ UNICODE # endif // # include <stdio. h> # include <windows. h> # include <assert. h> # include <malloc. h> lresult callback WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {static HWND hwndText = NULL; static HWND hwndButton = NULL; static WCHAR * pcnChar = NULL; switch (msg) {case WM_CREATE: pcnChar = (WCHAR *) (LPCREATESTRUCT) lParam)-> lpCreateParams; h WndText = CreateWindow (L "EDIT", L ", WS_CHILD | WS_VISIBLE | WS_BORDER, 1, 1, 60, 60, hwnd, (HMENU) 0, (LPCREATESTRUCT) (lParam)-> hInstance, NULL); hwndButton = CreateWindow (L "BUTTON", L "OK", WS_CHILD | WS_VISIBLE | WS_BORDER, 65, 1, 60, 60, hwnd, (HMENU) 1, (LPCREATESTRUCT) (lParam)-> hInstance, NULL); break; case WM_COMMAND: if (HIWORD (wParam) = BN_CLICKED & LOWORD (wParam) = 1) {if (! GetWindowText (hwndText, pcnChar, 2) | * pcnChar <0xFF) {MessageBox (0, L "enter a Chinese character! ", 0, 0); break;} DestroyWindow (hwnd);} break; case WM_DESTROY: if (! * PcnChar & * pcnChar <0xFF) {MessageBox (0, L "failed to get Chinese characters, now exit",); exit (0) ;}postquitmessage (0 ); // GetWindowText (hwndText, pcnChar, 1); break;} return DefWindowProc (hwnd, msg, wParam, lParam);} typedef struct {BYTE * byte_ptr; int bit_pointer;} BitPointer; void BitPointerInc (BitPointer * o) {assert (0 <= o-> bit_pointer & o-> bit_pointer <8); if (o-> bit_pointer = 7) {o-> bit_pointer = 0; o-> byte_ptr + = 1;} else {o-> bit_point Er + = 1 ;}} void SetBit (BitPointer * o) {assert (0 <= o-> bit_pointer & o-> bit_pointer <8 ); * (o-> byte_ptr) | = (1 <(7-(o-> bit_pointer); // pay attention to the bit sequence ......} Int GetBit (BitPointer * o) {assert (0 <= o-> bit_pointer & o-> bit_pointer <8); return (* (o-> byte_ptr )) & (1 <(7-(o-> bit_pointer); // pay attention to the order of bits ......} Void JmpByte (BitPointer * o) {o-> byte_ptr + = 1; o-> bit_pointer = 0;} int CountBits (BitPointer * o, BYTE * start) {return (o-> byte_ptr)-start) * 8 + (o-> bit_pointer);} static WCHAR g_szClassName [] = L "MyWindowClass"; static HINSTANCE g_hInstance; int ReadBitmapData (BYTE * buffer, DWORD size, int nWidth, int nHeight, WCHAR ** rslt) {DWORD bitLineLength = (nWidth % 32 = 0 )? NWidth: 32-nWidth % 32 + nWidth; WCHAR ** bitLine = NULL; int I, j; bitLine = (WCHAR **) malloc (nHeight * sizeof (WCHAR *)); bitPointer ptr = {buffer, 0}; for (I = nHeight-1; I> = 0; -- I) {bitLine [I] = (WCHAR *) malloc (nWidth * sizeof (WCHAR); ZeroMemory (bitLine [I], nWidth * sizeof (WCHAR); for (j = 0; j <nWidth; ++ j, bitPointerInc (& ptr) {bitLine [I] [j] =! GetBit (& ptr);} while (CountBits (& ptr, buffer) % bitLineLength! = 0) {JmpByte (& ptr) ;}} static WCHAR cnChar [2] = L "\ 0"; HWND hwnd = CreateWindow (g_szClassName, L "enter a Chinese character ", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 240,100, NULL, NULL, g_hInstance, cnChar); ShowWindow (hwnd, SW_SHOW); UpdateWindow (hwnd); MSG msg; while (GetMessage (& msg, NULL,) {TranslateMessage (& msg); DispatchMessage (& msg);} if (* cnChar = L' \ 0') {MessageBox (0, L "program error, please report this error to the program Author",) ;}for (I = 0; I <n Height; ++ I) {bitLine [I] = (WCHAR *) realloc (bitLine [I], (nWidth + 2) * sizeof (WCHAR )); bitLine [I] [nWidth] = L' \ R'; bitLine [I] [nWidth + 1] = L' \ n'; for (j = 0; j <nWidth; ++ j) {if (bitLine [I] [j]) bitLine [I] [j] = * cnChar; else bitLine [I] [j] = l '';} if (I> 0) {bitLine [0] = (WCHAR *) realloc (bitLine [0], (I + 1) * (2 + nWidth) * sizeof (WCHAR); memcpy (bitLine [0] + (I) * (2 + nWidth), bitLine [I], (2 + nWidth) * sizeof (WCHAR )); Free (bitLine [I]) ;}} int nRslt = 1 + (I) * (2 + nWidth); bitLine [0] = (WCHAR *) realloc (bitLine [0], nRslt * sizeof (WCHAR); * (bitLine [0] + (I) * (2 + nWidth) = 0; // terminate the unicode string # ifdef _ DEBUGMessageBox (0, bitLine [0], 0, 0); # endif * rslt = bitLine [0]; free (bitLine); return nRslt ;} void Process (PWSTR filename) {// MessageBox (0, filename, 0, 0); HANDLE hFile = CreateFile (filename, GENERIC_READ, file_assist_read, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); assert (hFile! = INVALID_HANDLE_VALUE); static BYTE buffer [8192]; ZeroMemory (buffer, sizeof buffer); DWORD dwRead; assert (ReadFile (hFile, buffer, 2, & dwRead, NULL )); assert (2 = dwRead); if (!! Strcmp ("BM", (const char *) buffer) {MessageBox (0, L "is not a valid BMP file", 0, 0); exit (0 );} assert (SetFilePointer (hFile, 0x0E, 0, FILE_BEGIN); // skip the first entry of the image file Header // read the image information Header, Bitmap Header Size, assert (ReadFile (hFile, buffer, 4, & dwRead, NULL); assert (4 = dwRead & 4 = sizeof (DWORD )); //************************************** * ********* // test the computer's byte sequence, currently, only small-end alignment is supported. {BYTE edian_test [4] = {0xAa, 0xBb, 0xCc, 0xDd}; if (DWORD) 0 xDdCc BbAa! = * (DWORD *) edian_test) {MessageBox (0, L "The program currently does not support large-end processors \ n" L "you may run Windows 8 \ n on some processors or Windows 8 \ n on some ARM processors" L "this program will not handle these cases for the moment, the program exits now. ", L" The program version must be updated for this computer ", 0); exit (0 );}} //************************************** * *********** // if (* (DWORD *) buffer! = 0x28) // the size of the Image Information header should be 0x28 {MessageBox (0, L "do not know this type of BMP file, "L" if there is no special reason, use the Windows built-in drawing program to generate a monochrome BMP file ",); exit (0);} assert (SetFilePointer (hFile, 0 x, FILE_BEGIN); assert (ReadFile (hFile, buffer, 4, & dwRead, NULL); assert (4 = dwRead); if (GetFileSize (hFile, NULL )! = * (DWORD *) buffer) {wsprintf (PWSTR) buffer, L "the BMP file has an incorrect file size. It should be % d bytes, actually % d bytes ", * (DWORD *) buffer, GetFileSize (hFile, NULL); MessageBox (0, (PWSTR) buffer, L "Image File broken", 0); exit (0 );} DWORD dwFileSize = * (DWORD *) buffer; assert (SetFilePointer (hFile, 0x1C, 0, FILE_BEGIN); assert (ReadFile (hFile, buffer, 2, & dwRead, NULL); assert (2 = dwRead); if (* (WORD *) buffer! = 0x01) // mono BMP {MessageBox (0, L "this is a correct BMP file, however, this program is used to convert monochrome BMP into text "L". If there is no special reason, use the Windows built-in drawing program to generate a monochrome BMP file \ n "L" or use the Windows built-in drawing program to open this image. and then save it as a monochrome BMP ", 0, 0); exit (0);} assert (ReadFile (hFile, buffer, 2, & dwRead, NULL); assert (2 = dwRead ); if (* (WORD *) buffer! = 0) {MessageBox (0, L "this is a correct monochrome BMP file \ n" L ", but this program does not process the RLE compression method used for this image \ n" L "for no special reason, please use Windows the drawing program generates a monochrome BMP file \ n "L" or uses the Windows built-in drawing program to open the image and save it as a monochrome BMP ", L "you need to update the program version to support this image", 0); exit (0);} assert (SetFilePointer (hFile, 0x12, 0, FILE_BEGIN); DWORD nWidth, nHeight; assert (ReadFile (hFile, buffer, 4, & dwRead, NULL); assert (4 = dwRead); nWidth = * (DWORD *) buffer; assert (ReadFile (hFile, buffer, 4, & dwRead, NULL); assert (4 = dwRead); nH Eight = * (DWORD *) buffer; assert (ReadFile (hFile, buffer, 4, & dwRead, NULL); assert (4 = dwRead); if (0x00010001! = * (DWORD *) buffer) // small-end byte order {MessageBox (0, L "bit number and number of occupied pixels should be 1", L "damaged monochrome BMP image ", 0); exit (0);} DWORD dwStartOffset = 0; assert (SetFilePointer (hFile, 0x0A, 0, FILE_BEGIN); assert (ReadFile (hFile, buffer, 4, & dwRead, NULL); assert (4 = dwRead); dwStartOffset = * (DWORD *) buffer; assert (SetFilePointer (hFile, 0x22,0, FILE_BEGIN )); assert (ReadFile (hFile, buffer, 4, & dwRead, NULL); assert (4 = dwRead); DWORD dwBitmapDataSize = * (DWORD *) buffer; if (dwBitmapDataSize! = DwFileSize-dwStartOffset) {MessageBox (0, L "damaged BMP file, inconsistent data",); exit (0);} assert (SetFilePointer (hFile, dwStartOffset, 0, FILE_BEGIN); BYTE * dataSpace = (BYTE *) malloc (dwBitmapDataSize); assert (dataSpace); assert (ReadFile (hFile, dataSpace, dwBitmapDataSize, & dwRead, NULL )); assert (dwBitmapDataSize = dwRead); WCHAR * rslt = NULL; int nRslt = ReadBitmapData (dataSpace, dwBitmapDataSize, nWidth, nHeight, & rslt); free (DataSpace); dataSpace = NULL; HGLOBAL gData = GlobalAlloc (GMEM_MOVEABLE, nRslt * sizeof (WCHAR); void * p = GlobalLock (gData); assert (p ); memcpy (p, rslt, nRslt * sizeof (WCHAR); OpenClipboard (NULL); EmptyClipboard (); SetClipboardData (cursor, gData); CloseClipboard (); GlobalUnlock (gData ); globalFree (gData); MessageBox (0, L "the result has been saved to the clipboard. Now you can paste the result to any position.", L "", 0 );} int WINAPI WinMain (HINSTANCE hinst, HINSTANCE hpreI Nst, PSTR lpCmdLine, int nShow) {g_hInstance = hinst; OPENFILENAME ofn; WCHAR szFileName [MAX_PATH] = L ""; ZeroMemory (& ofn, sizeof (ofn); ofn. lStructSize = sizeof (ofn); ofn. lpstrFilter = L "Text Files (*. bmp) \ 0 *. bmp \ 0All Files (*. *) \ 0 *. * \ 0 \ 0 "; ofn. lpstrFile = szFileName; ofn. nMaxFile = MAX_PATH; ofn. lpstrDefExt = L "bmp"; ofn. flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY; if (! GetOpenFileName (& ofn) {MessageBox (0, L "failed to find file",); return 0 ;}wndclass WNDCLASS ={ 0}; WndClass. hIcon = LoadIcon (NULL, IDI_APPLICATION); WndClass. hCursor = LoadCursor (NULL, IDC_ARROW); WndClass. hbrBackground = (HBRUSH) (COLOR_WINDOW + 1); WndClass. lpfnWndProc = WndProc; WndClass. lpszClassName = g_szClassName; if (! RegisterClass (& WndClass) {MessageBox (0, L "Window Registration Failed! ", L" Error! ", MB_ICONEXCLAMATION | MB_ OK | MB_SYSTEMMODAL); return 0;} Process (szFileName); return 0 ;}

I call it a bmp-to-text.c program ~

Gcc-o b2t bmp-to-text.c-finput-charset = gbk-mwindows

Convert BMP monochrome bitmap into text...

For example, if you create a bitmap and draw a cloud word on the in-place graph, the following results can be produced:

Cloud cloud
Cloud cloud
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
Cloud cloud
Cloud cloud
Cloud cloud
Cloud cloud
Cloud cloud
Cloud cloud
Cloud cloud
Cloud cloud
Cloud cloud
Cloud cloud
Cloud cloud
Cloud cloud
Cloud cloud
Cloud cloud
Cloud cloud
Cloud cloud
Cloud cloud
Cloud cloud
Cloud

That is, it's okay to have fun ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.