How to get width and height of a specific font in an iamge of a specific DPI

How to get width and height of a specific font in an iamge of a specific DPI

Post by Igor Ili » Fri, 27 Jun 2003 22:37:09



Hi,

I have a C++ Win DLL with some functions that I'm calling from VBA. One of
the functions creates BMP with a text that I send from VBA. Now, I can find
the width and height (in pixels or inches) of a string in MS Word (get hDC
of a screen, create a specific font, get a font in hDC with SelectObject and
find what I need with GetTextExtentPoint32), but how to do the same thing
for a string that will be part of a BMP?

This is the function that needs to be changed.

Private Function DimText(Text As String, fontName As String, _
  fontSize As Double, Optional Bold As Boolean, _
  Optional Italic As Boolean) As SDimText

   Dim hFont As Long, hdc As Long
   Dim PixpInch As Double

   On Error GoTo LocalError

 hdc = GetDC(0)

 PixpInch = GetDeviceCaps(hdc, 90) / 72

  DimText.PixInch = PixpInch
  hFont = CreateFontA(-fontSize * PixpInch, 0, 0, 0, _
    400 - 300 * Bold, -Italic, 0, 0, 1, 0, 0, 0, 0, fontName)

  If hFont Then
    SelectObject hdc, hFont
    GetTextExtentPoint32A hdc, Text, Len(Text), DimText
    DeleteObject hFont
  End If

  Release o, hdc

NormalExit:
   Exit Function

LocalError:
   MsgBox Err.Description, vbExclamation, "DimText"
   SDimText = 0
   Resume NormalExit

End Function

I think that I should get the BMP in hDC and create a font in there but I
just can't figure out how. I have BMP ready on the disk but I would prefer
not to use "LoadImage" or "LoadPicture" if that is possible.

Thanks.

 
 
 

How to get width and height of a specific font in an iamge of a specific DPI

Post by Mike D Sutto » Fri, 27 Jun 2003 22:55:10


Quote:> I have a C++ Win DLL with some functions that I'm calling from VBA. One of
> the functions creates BMP with a text that I send from VBA. Now, I can
find
> the width and height (in pixels or inches) of a string in MS Word (get hDC
> of a screen, create a specific font, get a font in hDC with SelectObject
and
> find what I need with GetTextExtentPoint32), but how to do the same thing
> for a string that will be part of a BMP?

> This is the function that needs to be changed.
<code sniped>
> I think that I should get the BMP in hDC and create a font in there but I
> just can't figure out how. I have BMP ready on the disk but I would prefer
> not to use "LoadImage" or "LoadPicture" if that is possible.

If you mean you have an API Bitmap object being returned to you that you
wish to get the dimensions for, then you can use the GetObject() API call on
the Bitmap handle and look at the returned BITMAP structure:

'***
Private Declare Function GetObjectAPI Lib "gdi32" Alias "GetObjectA" _
    (ByVal hObject As Long, ByVal nCount As Long, ByRef lpObject As Any) As
Long

Private Type BITMAP ' 24 bytes
    bmType As Long
    bmWidth As Long
    bmHeight As Long
    bmWidthBytes As Long
    bmPlanes As Integer
    bmBitsPixel As Integer
    bmBits As Long
End Type

' ...

Dim BMInf As BITMAP

Call GetObject(hBMP, Len(BMInf), BMInf)

Debug.Print BMInf.bmWidth & " * " & BMInf.bmHeight
'***

If the Bitmap is being returned to you already selected into a DC, use the
GetcurrentObject() API call with the OBJ_BITMAP parameter to grab it's
handle.
If the text doesn't cover the entire bitmap then you're not going to be able
to get the width of the text in this way, you may have some luck going
through and looking for the first and last rows with non-background pixels
in, but this will only work if you're drawing on a flat colour background -
It would be better to short circuit the DLL and get the text width there.
Hope this helps,

    Mike

 - Microsoft Visual Basic MVP -

WWW: Http://www.mvps.org/EDais/

 
 
 

How to get width and height of a specific font in an iamge of a specific DPI

Post by Igor Ili » Fri, 27 Jun 2003 23:50:08


Hi Mike,

I have predefined dimensions (Width, Height and DPI) of a Bmp that is going
to be created. The only thing I have to do is to send the string from VBA to
DLL which will create an image. Basically I have to figure out will the
string fit in Bmp before it's created. Unfortunatelly I can't change the DLL
code :(

That's why I'm trying to get the dimensions of that string in VBA. I can
create an empty image first and use it in hDC but I'm not sure if this is
going to wotk...

Thank You for your help.

Igor



> > I have a C++ Win DLL with some functions that I'm calling from VBA. One
of
> > the functions creates BMP with a text that I send from VBA. Now, I can
> find
> > the width and height (in pixels or inches) of a string in MS Word (get
hDC
> > of a screen, create a specific font, get a font in hDC with SelectObject
> and
> > find what I need with GetTextExtentPoint32), but how to do the same
thing
> > for a string that will be part of a BMP?

> > This is the function that needs to be changed.
> <code sniped>
> > I think that I should get the BMP in hDC and create a font in there but
I
> > just can't figure out how. I have BMP ready on the disk but I would
prefer
> > not to use "LoadImage" or "LoadPicture" if that is possible.

> If you mean you have an API Bitmap object being returned to you that you
> wish to get the dimensions for, then you can use the GetObject() API call
on
> the Bitmap handle and look at the returned BITMAP structure:

> '***
> Private Declare Function GetObjectAPI Lib "gdi32" Alias "GetObjectA" _
>     (ByVal hObject As Long, ByVal nCount As Long, ByRef lpObject As Any)
As
> Long

> Private Type BITMAP ' 24 bytes
>     bmType As Long
>     bmWidth As Long
>     bmHeight As Long
>     bmWidthBytes As Long
>     bmPlanes As Integer
>     bmBitsPixel As Integer
>     bmBits As Long
> End Type

> ' ...

> Dim BMInf As BITMAP

> Call GetObject(hBMP, Len(BMInf), BMInf)

> Debug.Print BMInf.bmWidth & " * " & BMInf.bmHeight
> '***

> If the Bitmap is being returned to you already selected into a DC, use the
> GetcurrentObject() API call with the OBJ_BITMAP parameter to grab it's
> handle.
> If the text doesn't cover the entire bitmap then you're not going to be
able
> to get the width of the text in this way, you may have some luck going
> through and looking for the first and last rows with non-background pixels
> in, but this will only work if you're drawing on a flat colour
background -
> It would be better to short circuit the DLL and get the text width there.
> Hope this helps,

>     Mike

>  - Microsoft Visual Basic MVP -

> WWW: Http://www.mvps.org/EDais/

 
 
 

How to get width and height of a specific font in an iamge of a specific DPI

Post by Mike D Sutto » Sat, 28 Jun 2003 00:19:07


Quote:> I have predefined dimensions (Width, Height and DPI) of a Bmp that is
going
> to be created. The only thing I have to do is to send the string from VBA
to
> DLL which will create an image. Basically I have to figure out will the
> string fit in Bmp before it's created. Unfortunatelly I can't change the
DLL
> code :(

> That's why I'm trying to get the dimensions of that string in VBA. I can
> create an empty image first and use it in hDC but I'm not sure if this is
> going to wotk...

I don't see why your original method wouldn't work for a memory bitmap
(Which has no DPI as such, other than that of the screen..)
If you have to convert the coordinates to a specific DPI (In theory) from
pixels, then simply work out what ratio the target DPI is from the screen
and multiply the result from GetTextExtentPoint32() accordingly.  Other than
that I can't see where the problem is, what are you trying to do this for?
Maybe that will give us some more information about the problem/solution.
Hope this helps,

    Mike

 - Microsoft Visual Basic MVP -

WWW: Http://www.mvps.org/EDais/

 
 
 

How to get width and height of a specific font in an iamge of a specific DPI

Post by Igor Ili » Sat, 28 Jun 2003 00:55:12


Quote:> If you have to convert the coordinates to a specific DPI (In theory) from
> pixels, then simply work out what ratio the target DPI is from the screen
> and multiply the result from GetTextExtentPoint32() accordingly.

I tried to do that but the ratio (targt DPI/screen DPI) is not the same as
(target text size/screen text size).  I am trying to figure out string
length that can fit in BMP (with given Font and BMP dimensons) and then wrap
that string to fit image width.

Thanks.

Igor



> > I have predefined dimensions (Width, Height and DPI) of a Bmp that is
> going
> > to be created. The only thing I have to do is to send the string from
VBA
> to
> > DLL which will create an image. Basically I have to figure out will the
> > string fit in Bmp before it's created. Unfortunatelly I can't change the
> DLL
> > code :(

> > That's why I'm trying to get the dimensions of that string in VBA. I can
> > create an empty image first and use it in hDC but I'm not sure if this
is
> > going to wotk...

> I don't see why your original method wouldn't work for a memory bitmap
> (Which has no DPI as such, other than that of the screen..)
> If you have to convert the coordinates to a specific DPI (In theory) from
> pixels, then simply work out what ratio the target DPI is from the screen
> and multiply the result from GetTextExtentPoint32() accordingly.  Other
than
> that I can't see where the problem is, what are you trying to do this for?
> Maybe that will give us some more information about the problem/solution.
> Hope this helps,

>     Mike

>  - Microsoft Visual Basic MVP -

> WWW: Http://www.mvps.org/EDais/

 
 
 

1. Specific texture to specific zone, please...

Hello,

Basicly, each face of my box is composed of 4x4 square.

I would like to select one of those squares and apply a precise texture.
This texture should  be different than the others.

When I try to apply a texture after selecting this specific area, the entire
object receives the mapping...  :(

Could someone tell me the steps to select a specific area of a face, and how
to apply a specific texture to it ?

thank you very much.

jeanphi

2. NEEDED: Windows Drawing program (WMF,BMP,Text)

3. Getting the color of specific texture coordinates?

4. Question

5. Getting the width and height of an GIF ?

6. DirectX and BCB4

7. looking for a specific FONT

8. What is cgi

9. A specific Font

10. to change the resolution and font of one specific window

11. How to calculate width of AnsiString with specific font and text ?

12. [Searching specific Clip-Art]

13. Need Help With .bmp file specifics