I am doing a project on steganography ( I hope you know what it is :) ) I have to convert a text file (if possible a RTF File) to an image file like jpg or gif etc. But I do not seem to get around this problem and I'm stuck in it.
Please can anybody help me with this? :-/

Dani AI

Generated

Recommended, programmatic flow (RTF -> image)

  • For true RTF rendering use a Rich Edit control: stream the RTF into the control (EM_STREAMIN) and render it into a device context with EM_FORMATRANGE; this preserves fonts, colors and layout. After drawing into a memory HDC/HBITMAP, save the bitmap with GDI+ or the Windows Imaging Component (WIC). This is the robust route for a C/C++ program that must convert RTF (not just plain text) to PNG/JPEG/GIF. (EM_STREAMIN docs) (learn.microsoft.com) (EM_FORMATRANGE/printing guidance). (learn.microsoft.com)

Practical steps

  • Create a hidden RichEdit window (msftedit or riched20), stream the RTF with EM_STREAMIN, set up a memory DC (CreateCompatibleDC / CreateDIBSection) sized to the formatted output, then call EM_FORMATRANGE to render into that DC. Convert the resulting HBITMAP into a GDI+ Bitmap (Bitmap::FromHBITMAP) and call Image::Save (or use WIC) to write JPEG/GIF/PNG. See the GDI+/WIC save docs for encoder selection and options. () (learn.microsoft.com)

Troubleshooting the compile/runtime errors seen in code

  • Those C2440 messages come from mixing obsolete 16-bit keywords and incompatible pointer types. Drop near/far (they’re relics of 16‑bit models) and use standard char*/wchar_t* as appropriate. (discussion on near/far obsolescence) (learn.microsoft.com)
  • SelectObject returns an HGDIOBJ; assign or cast it explicitly to HFONT/HBITMAP (for example, HFONT old = (HFONT)SelectObject(hdc, hFont);) and always restore the old object before DeleteObject. (SelectObject docs). (learn.microsoft.com)
  • Also check uninitialized handles (for example hWnd used in ReleaseDC) and use safe file-size/read calls (GetFileSizeEx / ReadFile) and null-terminate buffers.

Notes on alternatives and context

  • A quick screen-capture () is fine for manual work but not for automated conversion. Third‑party converters or headless tools (ImageMagick / LibreOffice / commercial SDKs) are viable if embedding a full RTF renderer is undesirable; ’s 3rd-party suggestion points in that direction. Finally, ’s clarification is important: turning text into an image is not steganography itself — steganography would then hide that image inside another image.

Recommended Answers

All 4 Replies

A simple screen capture would do the trick.
Alt-print screen, paste into your image editor to remove extraneous bits.

A simple screen capture would do the trick.
Alt-print screen, paste into your image editor to remove extraneous bits.

Sorry But I have to do this with the help of a C or C++ Program

Till now I have been able to come up with this code:

#include <windows.h>
#include <windowsx.h>
#include <vicdefs.h>
#include <string.h>


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);
}

But its showing some errors.
error C2440: '=' : cannot convert from 'unsigned char *' to 'char *'
error C2440: '=' : cannot convert from 'void *' to 'struct HFONT__ *
error C2440: '=' : cannot convert from 'void *' to 'struct HBITMAP__ *'

try Altsoft Xml2PDF Workstation to convert rtf to jpeg or gif

> I have to convert a text file (if possible a RTF File) to an image file like jpg or gif etc.
Except this isn't steganography.

Steganography takes an existing image, and "hides" a text file inside it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.