I have been writing a program for about 10 years and I display image files as thumbnails much as picassa does. I would like an image of a txt file to be displayed also.
GdipLoadImageFromFile fails with non image file.
any suggestions how to write code to display an image of a .txt file ?
many thanks
I know it is possible as one can purchase software that converts rtf to jpg but I need it in my program.
john
unless it is a large thumbnail, there won't be much detail - not enough to make the text legible
if you want the thumbnail to be representative of the appearance of the text file, you can make a small BMP image with a simple reduction
you may want to line wrap at 80 characters and just make a little black and white bitmap
i think windows has the code to generate JPG's from bitmaps - i haven't as yet played with enough of it to know for sure
It is a large thumbnail andit has to be high resolution and readable.
thanks for your thoughts
John
ok - reducing text to readable small text is not a graphics issue
if you do it that way, it will not be legible
what you want to do is create a custom font of your own
using about 5 x 6 pixels, you can make a very legible font
smaller fonts may be made, but are a little more difficult to read - i have used 4 x 5 for some uses
you are going to read the text file in and convert it to the graphics font, creating a bitmap
Quote from: jklosak on October 30, 2009, 02:56:06 AM
It is a large thumbnail andit has to be high resolution and readable.
thanks for your thoughts
John
Technically it might be possible to grab the DC of a richedit window, and save the bitmap to a file. Png would probably yield much smaller thumbnails than jpg, but that is a matter of taste. In any case, it would be great if you could take an existing text, make a screenshot and produce the thumbnail "by hand" with paintshop etc, in order to get an idea of the desired output. As Dave pointed out, fonts may be stumbling block.
I would use GDI+
The steps:
1.) Define a BITMAPINFO
2.) Fill it like this
BMI.bmiHeader.biSize = SIZEOF(BITMAPINFOHEADER)
BMI.bmiHeader.biWidth = 1728
BMI.bmiHeader.biHeight = -2290
BMI.bmiHeader.biPlanes = 1
BMI.bmiHeader.biBitCount = 1
BMI.bmiColors[1].rgbBlue = 0xFF
BMI.bmiColors[1].rgbGreen = 0xFF
BMI.bmiColors[1].rgbRed = 0xFF
3.) Hdc = CreateCompatibleDC(NULL)
4.) hBitmap = CreateDIBSection(Hdc,BMI,DIB_RGB_COLORS,pvBits,0,0)
So you have a B/W DIB section.
5.) hOldBMP = SelectObject(Hdc,hBitmap)
PatBlt(Hdc,0,0,1728,2290,WHITENESS)
6.) You can set fonts and linestyles
7.) SetTextAlign, DCh,TA_UPDATECP to move the cursor.
8.) Use TextOutA, DCh...... to write on the bitmap
9.) Convert the GDI bitmap to GDI+ and clone it
pImage1 = GDIP_BITMAPFROMHBITMAP(hBitmap)
pImage2 = GDIP_CLONE(pImage1)
use resize or what else you want.
EDIT :
Actually you can also leave out GDI+ and just use
CopyImage, hdc,IMAGE_BITMAP,pWidth,pHeight,LR_COPYDELETEORG OR LR_COPYRETURNORG OR LR_MONOCHROME
to resize.
GDI+ may does a better job by converting it to true color and you can set the "GdipSetSmoothingMode" to "SmoothingModeHighQuality" before resize.
At least for CRTs, very small fonts tend to be unreadable on the screen because the effective dot density is too low. I suspect that converting text to an image and then scaling the image would make this problem much worse. I think simply drawing the text in a smaller font would produce better results. The attachment is a crude test app that allows you to view text in an edit control, in a specified font over a range of point sizes. Note that not all fonts are available in the full range of sizes.
Quoteconverting an txt file to a jpg to display as a thumbnail
Just like everything must be converted to binary to run on your computer, ALL images must be converted to a bitmap before being displayed by Windows. Producing an image in any other format would simply require it to be converted back to a bitmap. Ficko's solution to produce a bitmap immediately is definitely the best (and simplest) approach.
Thanks a lot.
I think the use GDI+ sound great else I will try to work with the DC of a richedit window.
thanks very much for all your help
John Klosak