Contrariwise,
if it was so, it might be;
and if it were so, it would be;
but as it isn't, it ain't.
That's logic.
--Lewis Carroll
|
|
|
| The text | The image |
Victor Library for 32-bit Windows holds images in memory as device independent bitmaps (DIBs). Under 32-bit Windows the image in a DIB Section can be selected into a device context where you can operate on it with Windows drawing functions.
To convert the text into an image, the steps are:
To save the image, call one of the Victor savefile functions in the Victor Library. In the Visual Basic example below we save the image to a BMP file with the Victor Library savebmp function.
Private Sub mnutexttoimage_Click() Dim lf As LOGFONT Dim hFont, hOldFont As Long Dim hOldBitmap As Long Dim hMemDC As Long Dim rc As RECT Dim tmptext As imgdes Dim rcode, captionHt, captionWd As Long Dim caption As String Dim count As Long caption = "Sample text string" ' Enter string directly, or read a text file: count = 0 Open "text.txt" For Input As #1 Do While Not EOF(1) abyte = Input(1, #1) count = count + 1 Loop Close #1 Open "text.txt" For Input As #1 caption = Input(count, #1) Close #1 lf.lfHeight = 0 lf.lfWidth = 0 lf.lfEscapement = 0 ' Set lfEscapement and lfOrientation to 10 * angle to rotate text lf.lfOrientation = 0 ' for example, to rotate 45 degrees set the values to 450 lf.lfWeight = FW_NORMAL lf.lfItalic = 0 lf.lfUnderline = 0 lf.lfStrikeOut = 0 lf.lfCharSet = ANSI_CHARSET lf.lfOutPrecision = OUT_DEFAULT_PRECIS lf.lfClipPrecision = CLIP_DEFAULT_PRECIS lf.lfQuality = DEFAULT_QUALITY lf.lfPitchAndFamily = DEFAULT_PITCH Or FF_DONTCARE lf.lfFaceName(0) = (65) '(65, 82, 73, 65, 76) is "ARIAL" lf.lfFaceName(1) = (82) lf.lfFaceName(2) = (73) lf.lfFaceName(3) = (65) lf.lfFaceName(4) = (76) ' Set the font point size lf.lfHeight = GetDeviceCaps(hdc, LOGPIXELSY) lf.lfHeight = -MulDiv(FONT_SIZE, lf.lfHeight, 72) hMemDC = CreateCompatibleDC(hdc) hFont = CreateFontIndirectA(lf) ' Create our font rcode = SetBkMode(hMemDC, TRANSPARENT) hOldFont = SelectObject(hMemDC, hFont) ' Select font into DC ' Don't write any text, just fill rc with text dimensions rcode = DrawText(hMemDC, caption, -1, rc, DT_CALCRECT Or DT_LEFT Or DT_EXPANDTABS Or DT_NOPREFIX) ' Calculate the text size and create temp image large enough to hold the text captionHt = rc.bottom - rc.top + 1 captionWd = rc.right - rc.left + 1 rcode = allocimage(tmptext, captionWd, captionHt, 1) rcode = zeroimage(255, tmptext) ' Create a white background ' Select bitmap into the memory DC hOldBitmap = SelectObject(hMemDC, tmptext.hBitmap) ' Set the rect for adding the text rcode = SetRect(rc, 0, 0, tmptext.endx, tmptext.endy) ' Write the text rcode = DrawText(hMemDC, caption, -1, rc, DT_LEFT Or DT_EXPANDTABS Or DT_NOPREFIX) ' Restore the original bitmap and font and delete the new font rcode = SelectObject(hMemDC, hOldBitmap) DeleteObject (SelectObject(hMemDC, hOldFont)) DeleteDC (hMemDC) ' Delete memory DC ' Save the new image to a file rcode = savebmp("test.bmp", tmptext, 0) ' If we're done with the image buffer, release it freeimage tmptext ' Or else . . . ' To view the new image in the loadnsho.bas example ' comment out the preceeding instruction "freeimage tmptext" and ' uncomment the following: 'freeimage vimage 'copyimgdes tmptext, vimage 'MainWnd.BackColor = &HFFFFFF ' Erase previous image 'form_paint 'Redraw image 'set_scroll vimage ' Reset the scroll ranges for the new image End Sub ........... Add these defines and declarations to your Global module ........... ' Image descriptor Type imgdes ibuff As Long stx As Long sty As Long endx As Long endy As Long buffwidth As Long palette As Long colors As Long imgtype As Long bmh As Long hBitmap As Long End Type Type RECT left As Long top As Long right As Long bottom As Long End Type Declare Function allocimage Lib "VIC32.DLL" (image As imgdes, ByVal wid As Long, ByVal leng As Long, ByVal BPPixel As Long) As Long Declare Function zeroimage Lib "VIC32.DLL" (ByVal level As Long, image As imgdes) As Long Declare Function savebmp Lib "VIC32.DLL" (ByVal Fname As String, srcimg As imgdes, ByVal cmp As Long) As Long Declare Sub freeimage Lib "VIC32.DLL" (image As imgdes) Declare Sub copyimgdes Lib "VIC32.DLL" (srcimg As imgdes, desimg As imgdes) ' Logical Font Public Const LF_FACESIZE = 32 Public Const LF_FULLFACESIZE = 64 Public Const FONT_SIZE = 12 Public Const NO_ERROR = 0 Public Const ANSI_CHARSET = 0 Public Const OUT_DEFAULT_PRECIS = 0 Public Const CLIP_DEFAULT_PRECIS = 0 Public Const DEFAULT_QUALITY = 0 Public Const DEFAULT_PITCH = 0 Public Const FF_DONTCARE = 0 Public Const LOGPIXELSY = 90 Public Const TRANSPARENT = 1 Type LOGFONT lfHeight As Long lfWidth As Long lfEscapement As Long lfOrientation As Long lfWeight As Long lfItalic As Byte lfUnderline As Byte lfStrikeOut As Byte lfCharSet As Byte lfOutPrecision As Byte lfClipPrecision As Byte lfQuality As Byte lfPitchAndFamily As Byte lfFaceName(LF_FACESIZE) As Byte End Type ' DrawText() Format Flags Public Const DT_TOP = &H0 Public Const DT_LEFT = &H0 Public Const DT_CENTER = &H1 Public Const DT_RIGHT = &H2 Public Const DT_VCENTER = &H4 Public Const DT_BOTTOM = &H8 Public Const DT_WORDBREAK = &H10 Public Const DT_SINGLELINE = &H20 Public Const DT_EXPANDTABS = &H40 Public Const DT_TABSTOP = &H80 Public Const DT_NOCLIP = &H100 Public Const DT_EXTERNALLEADING = &H200 Public Const DT_CALCRECT = &H400 Public Const DT_NOPREFIX = &H800 Public Const DT_INTERNAL = &H1000 Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long Declare Function MulDiv Lib "kernel32" (ByVal nNumber As Long, ByVal nNumerator As Long, ByVal nDenominator As Long) As Long Declare Function CreateFontIndirectA Lib "gdi32" (lpLogFont As LOGFONT) As Long Declare Function SetBkMode Lib "gdi32" (ByVal hdc As Long, ByVal nBkMode As Long) As Long Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long Declare Function SetRect Lib "user32" (lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
int text_to_image(char far *fname, imgdes *resimg)
{
#define FONT_SIZE 12
static LOGFONT lf = { // Use font described by LOGFONT struct
0, 0, 0, 0, FW_NORMAL, 0, 0, 0,
ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "ARIAL"
};
HFONT hFont, hOldFont;
HBITMAP hOldBitmap;
HWND hWnd;
HDC hDC, hMemDC;
RECT rc;
imgdes tmptext;
int rcode = NO_ERROR, captionHt, captionWd;
char *caption;
HANDLE fhandle;
DWORD filesize, dwBytsRd;
fhandle = CreateFile(fname, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
filesize = SetFilePointer(fhandle, 0L, NULL, FILE_END);
SetFilePointer(fhandle, 0L, NULL, FILE_BEGIN);
filesize++;
if((caption = (UCHAR near *)calloc(filesize,1)) == NULL)
return(BAD_MEM); // If area not allocated
// Read the file
ReadFile(fhandle, (LPSTR)caption, (unsigned)filesize, &dwBytsRd, NULL);
CloseHandle(fhandle);
hDC = GetDC(GetActiveWindow());
hMemDC = CreateCompatibleDC(hDC);
// Set the font point size
lf.lfHeight= - MulDiv(FONT_SIZE, GetDeviceCaps(hDC,LOGPIXELSY), 72);
//Set lfEscapement and lfOrientation to 10 * angle to rotate text
//for example, to rotate 45 degrees set the values to 450
hFont = CreateFontIndirect(&lf); // Create our font
SetBkMode(hMemDC, TRANSPARENT);
hOldFont = SelectObject(hMemDC, hFont); // Select font into DC
// Don't write any text, just fill rc with text dimensions
DrawText(hMemDC, caption, -1, &rc, DT_CALCRECT | DT_LEFT | DT_EXPANDTABS | DT_NOPREFIX);
// Calculate the text size and create temp image
captionHt = rc.bottom - rc.top + 1;
captionWd = rc.right - rc.left + 1;
rcode = allocimage(&tmptext, captionWd, captionHt, 1);
zeroimage(255, &tmptext); // Create a white background
// Select bitmap into the memory DC
hOldBitmap = SelectObject(hMemDC, tmptext.hBitmap);
// Set the rect for adding the text
SetRect(&rc, 0, 0, tmptext.endx, tmptext.endy);
// Write the text
DrawText(hMemDC, caption, -1, &rc, DT_LEFT | DT_EXPANDTABS | DT_NOPREFIX);
// Restore the original bitmap and font and delete the new font
SelectObject(hMemDC, hOldBitmap);
DeleteObject(SelectObject(hMemDC, hOldFont));
DeleteDC(hMemDC); // Delete memory DC
ReleaseDC(hWnd, hDC);
free(caption);
freeimage(resimg);
copyimgdes(&tmptext, resimg);
return(rcode);
}
Victor Image Processing Library homepage | Victor Product Summary | more source code