Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

TextToImage 1.07 - Write text onto an image or screenshot


  • Please log in to reply
64 replies to this topic
bmcclure
  • Members
  • 774 posts
  • Last active: Jan 04 2014 10:44 PM
  • Joined: 24 Nov 2007

I could add the ability for the input to be a handle to an existing image, ad output to a handle

Would be useful in bmcclure's case - i.e. load the button image once, and generate images with text for each button without having to read from or write to disk each time.

God I would love that feature! It seems like that would save a lot of extra loading of the button image from file.

I know this uses GDI and not GDI+, but I would love to see this function supporting transparency on PNGs so that my button backgrounds edges can show through to the window background (eg rounded corners) :)

I like the idea of layering text, but:
1. What purpose is there creating text on top of text that doesn't match? So if anything, why not just add an option for making a second pass that defaults to 0?
2. You would have to have different style/position options for each pass, or there wouldn't be much use for the second pass (it would cover the first).
3. Couldn't this also be achieved with two subsequent function calls? Especially if you allow the function to operate on image handles.

Eg. call it once to draw the shadow, call it again to draw the text on top.

bmcclure
  • Members
  • 774 posts
  • Last active: Jan 04 2014 10:44 PM
  • Joined: 24 Nov 2007
I have one suggestion. In order to support as many text options as possible, how about either:
1. Use an Options parameter and assign letters to toggle-able options so you can just accept a string of options to turn on, or for more robustness:
2. Have some globals like TextToImage_LSpacing, TextToImage_Whatever that can optionally be set before calling the function

Just an idea, since you mentioned the parameters are getting crowded. Thanks for the great function and I look forward to being able to output to an image handle :)

bmcclure
  • Members
  • 774 posts
  • Last active: Jan 04 2014 10:44 PM
  • Joined: 24 Nov 2007
Sorry for all the posts, I think this is a separate issue.

I'm trying to use "Tahoma" for the Font, but it is always using Arial regardless.

TextToImage(thisImgDir . "\btn\Button_" . pSize . "_" . pBg . ".bmp",pName,thisFile1,12,8,9,"Left|Top",100,"Tahoma","FFFFFF")
Should be:
Posted Image
But this is what comes out:
Posted Image

I've got it generating the buttons upon Gui creation into a temp directory, then loading the buttons like I was before, then deleting them upon program exit. It seems to work OK, except for the above issue, and that for some reason when I re-import the files I create with the function (which look fine in Fireworks) into the GUI to display them, the quality is terrible for some reason. That can't be your function however sine the image looks fine when I view it in my image editor.

This is how the above button looks when imported into my GUI with AddGraphicButton() (which works for buttons I make myself):
Posted Image

tic
  • Members
  • 1934 posts
  • Last active: May 30 2018 08:13 PM
  • Joined: 22 Apr 2007
I have fixed the font issue. I have no idea why it is happening as that part of the code is identical. I joined all the options into 1 variable, but had to make font separate for some unknown reason.

I dont know why yours looks strange on that screenshot. What happens if you make a test gui and just add it using gui, add, picture ? does it still look weird then?

I havent released all the updated code yet as I have found a bug where it is not saving the transparencies in pngs. Even if I skip the entire adding text part of the function it still wont keep the transparencies. Ive asked Lexikos to see if he can help, so until then......

When the transparency issue is fixed ill move on to adding the option to output the handle.

bmcclure
  • Members
  • 774 posts
  • Last active: Jan 04 2014 10:44 PM
  • Joined: 24 Nov 2007
When you say you fixed the font issue, did you update your download file? It still looks the same to me.

Unless you meant this fix is part of your next version that you're still trying to get working. I'd be happy to use the code without transparency temporarily :) I'm trying to use generated images for the new Hotkey gui I'm making so I'm just hoping to get it working at all.

Oddly enough, this is what I get using Gui, Add, Picture with an image generated from your script (displays fine with an image created manually):
Posted Image

And on a blank script:
Posted Image

The script:
Gui, Add, Picture,, Add.bmp
Gui, Show


tic
  • Members
  • 1934 posts
  • Last active: May 30 2018 08:13 PM
  • Joined: 22 Apr 2007
Try this.

<!-- m -->https://ahknet.autoh... ... ished-.zip<!-- m -->

As the name suggests, I still know of bugs in it that require fixing before I properly update it.

If it mucks up, try changing the quality, also does it work if you just use the default screen as in parameter?

Value Meaning
ANTIALIASED_QUALITY Windows NT 4.0 and later: Font is antialiased, or smoothed, if the font supports it and the size of the font is not too small or too large.

Windows 95 with Plus!, Windows 98/Me: The display must greater than 8-bit color, it must be a single plane device, it cannot be a palette display, and it cannot be in a multiple display monitor setup. In addition, you must select a TrueType font into a screen DC prior to using it in a DIBSection, otherwise antialiasing does not happen.
CLEARTYPE_QUALITY Windows XP: If set, text is rendered (when possible) using ClearType antialiasing method. See Remarks for more information.
DEFAULT_QUALITY Appearance of the font does not matter.
DRAFT_QUALITY Appearance of the font is less important than when the PROOF_QUALITY value is used. For GDI raster fonts, scaling is enabled, which means that more font sizes are available, but the quality may be lower. Bold, italic, underline, and strikeout fonts are synthesized, if necessary.
NONANTIALIASED_QUALITY Windows 95 with Plus!, Windows 98/Me, Windows NT 4.0 and later: Font is never antialiased, that is, font smoothing is not done.
PROOF_QUALITY Character quality of the font is more important than exact matching of the logical-font attributes. For GDI raster fonts, scaling is disabled and the font closest in size is chosen. Although the chosen font size may not be mapped exactly when PROOF_QUALITY is used, the quality of the font is high and there is no distortion of appearance. Bold, italic, underline, and strikeout fonts are synthesized, if necessary.

Looking at wingdi.h, the values behind these constants are:

#define DEFAULT_QUALITY 0
#define DRAFT_QUALITY 1
#define PROOF_QUALITY 2
#if(WINVER >= 0x0400)
#define NONANTIALIASED_QUALITY 3
#define ANTIALIASED_QUALITY 4
#endif /* WINVER >= 0x0400 */

#if (_WIN32_WINNT >= _WIN32_WINNT_WINXP)
#define CLEARTYPE_QUALITY 5
#define CLEARTYPE_NATURAL_QUALITY 6
#endif



bmcclure
  • Members
  • 774 posts
  • Last active: Jan 04 2014 10:44 PM
  • Joined: 24 Nov 2007
Note: check updated screenshots in above post. I think it's a transparency issue... pure white isn't showing up it seems.

I'll test your new version and see if it changes that as well.

tic
  • Members
  • 1934 posts
  • Last active: May 30 2018 08:13 PM
  • Joined: 22 Apr 2007
I checked out what you meant. it happened to me too. just remove the quality option (all options are optional) and it will be fine. it is trying to antialiase the text and it is too small

Edit:

Also try Quality=4 as that looks better on mine

bmcclure
  • Members
  • 774 posts
  • Last active: Jan 04 2014 10:44 PM
  • Joined: 24 Nov 2007
Tested new version, seems to be the same issue with the font. It looks like Arial at least. Could be something else, but it doesn't look like Tahoma.

Note: In AHK, when I import the buttons created with this script, as Gui Pictures or with AddGraphicButton(), my rollover buttons with gold text show up as a weird light blue text instead, even though it's correct in the image.

How do I change the quality settings like you have in the comments above? Can I enter them like that into my code, or do I need to do something else? Sorry for my stupidity on this part, heh.

One other problem I have is that, this text is so small, I'd like to remove clear-type/anti-aliasing, but I can't figure out exactly how to do that. Maybe once I remove that the font will magiacally look a lot more like Tahoma, heh, but the one that comes out seems to be a little more rounded than Tahoma is.

tic
  • Members
  • 1934 posts
  • Last active: May 30 2018 08:13 PM
  • Joined: 22 Apr 2007
Run the example included in that unfinished zip. Change the input to the name of your bmp, and change the output to another bmp. In the options edit field in that example then delete the bit at the end Quality=5.

That will remove all antialising (gives the same effect as Quality=0)

bmcclure
  • Members
  • 774 posts
  • Last active: Jan 04 2014 10:44 PM
  • Joined: 24 Nov 2007
With your example it creates my button image properly I think, but it still displays the same in any gui I add it to.

Also, even with explicitly setting Quality=0, it still looks like Cleartype. In my two button images posted above, the top one is how I want them to look, which is Tahoma with no text smoothing at all, but below that is how the image always comes out, even if I leave the quality setting out completely (which I was doing before)

tic
  • Members
  • 1934 posts
  • Last active: May 30 2018 08:13 PM
  • Joined: 22 Apr 2007
Hmmm it seems to work fine for me.

Do these exact setting give you something different to my screenshot:

Posted Image

bmcclure
  • Members
  • 774 posts
  • Last active: Jan 04 2014 10:44 PM
  • Joined: 24 Nov 2007
Almost the same, except mine still look like cleartype fonts.

I'm using 64-bit Vista if that matters. :? Hmmm

tic
  • Members
  • 1934 posts
  • Last active: May 30 2018 08:13 PM
  • Joined: 22 Apr 2007
Could you post screenshots with the same settings I have but with Quality=0 and also use Quality=3 added to the end

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006

Also, even with explicitly setting Quality=0, it still looks like Cleartype.

0 is DEFAULT_QUALITY, which uses the system settings. I guess you have ClearType enabled? 3 is NONANTIALIASED_QUALITY.

Even if I skip the entire adding text part of the function it still wont keep the transparencies.

It is because you are converting the GDI+ bitmap to a GDI bitmap and back. You need to do the rendering in GDI+, i.e. without converting the bitmap.

See this line:
DllCall("gdiplus\GdipCreateHBITMAPFromBitmap", "Uint", pBitmap, "UintP", hBM, "Uint", 0)
The last parameter is the colour to make transparent pixels. 0 is black. Strangely, I think the transparent pixels remain transparent until you call GdipCreateBitmapFromHBITMAP (convert it back to a GDI+ bitmap.)

MSDN: GDI+ Flat API might be of some help. Specifically:
Text Functions, see DrawString/GdipDrawString.
Graphics Functions, see GetHDC/GdipGetDC and ReleaseHDC/GdipReleaseDC. Use these to bitblt the screen/window.

One other necessary function doesn't seem to be mentioned on those pages:
GpStatus WINGDIPAPI
GdipGetImageGraphicsContext(GpImage *image, GpGraphics **graphics);
This allows you to get a Graphics object for drawing on the GDI+ bitmap.