Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inches to Pixel? 3

Status
Not open for further replies.

vbSun

Programmer
Dec 9, 2002
504
US
I just need some way to convert the inches measure to pixels. I am making a canvas with the inches measure, and i need to set the width and height of a picture box based on this.

Please help.

Thanks,
Sunil.

------------------------------------------
The faulty interface lies between the chair and the keyboard.
 
This could be a problem. For one thing, you'll have to consider the resolution at which the program is ran. Next, ask yourself if you really do mean pixels, or just twips...
 
Thanks for replying.

I think twips. Cos am setting the width and height of a picturebox using this.

------------------------------------------
The faulty interface lies between the chair and the keyboard.
 
MSDN said:
TWIP->A screen-independent unit used to ensure that placement and proportion of screen elements in your screen application are the same on all display systems. A twip is a unit of screen measurement equal to 1/20 of a printer's point. There are approximately 1440 twips to a logical inch or 567 twips to a logical centimeter (the length of a screen item measuring one inch or one centimeter when printed).[/Quote MSDN]

or did i miss the point?

good luck!

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
To get the best response to a question, please check out FAQ222-2244 first
A General Guide To Excel in VB FAQ222-3383
 
1440



MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
>a screen item measuring one inch

Oh, I'd be very careful before I accepted the idea that a screen was an arbitarily correct unit of measurement...
 
not my quote... straight out of MSDN under definition of a twip!

but i understand your concern [neutral]

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
To get the best response to a question, please check out FAQ222-2244 first
A General Guide To Excel in VB FAQ222-3383
 
Many monitors will (falsely) report Twips/pixel as being 15. Most printers will provide a correct answer, so in general stuff will come out right size on a printer, but probably not on a monitor

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
In fact it's probably worth linking back to our last in-depth discussion on the whole twips thing: thread222-455004
 
OK, so what is the actual problem here?

Is it:
I have a canvas that is using a scale of TWIPS and a picture box that is in inches, so how do I make them the same size?

Or is it:
How to I ensure that a picture box on screen is exactly (x) inches by (y) inches when I hold a ruler to the screen?

If a) then try setting the scalemode of all your forms to be the same (pixels, twips, whatever) - then you don't have to convert anything.




Then, for the sake of completeness...

If b) there is no easy way to get this right.
An 800 x600 screen on a 14" monitor will show the same image as an 800 x 600 screen on a 21" monitor, but clearly they are sized differently when measured in inches using a rulre held to the glass.

The only way I know of to get that sort of thing working (but really - don't even consider this for real) is to have the user measure what you consider to be a 2 inch square shape on screen, then enter the real size.
Using the entered size, use this as a multiplier and adjust your controls accordingly.

(ie user says your 2 inch square is actually 2.5 inches on their mega monitor, then all your controls are adjusted to 2/2.5 of their intended size)

Jeff
 
JeffTullin,

<If b) there is no easy way to get this right.
An 800 x600 screen on a 14&quot; monitor will show the same image as an 800 x 600 screen on a 21&quot; monitor, but clearly they are sized differently when measured in inches using a rulre held to the glass.>

This isn't strictly accurate - see my post above and strongm's referenced thread for a fuller understanding of the relationship between screen twips, pixels and inches.

If your video display correctly reports twips/pixel, and your code calls for an object to be sized in twips, then its displayed size will correct, regardless of monitor size or resolution - that's the whole point of twips!

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
It would be a miracle if the 'display' correctly reported twips per pixel.
The display DRIVER has its own ideas, but can it always know what monitor it is plugged into?

Extreme example:
plug your laptop into an external projector.

 
It's what this new 'Plug and Play' technology is supposed to cover. If you look in Device manager, you'll find your monitor identified (if it's fully P'n'P compatible).

However having said that, many monotors don't report fully, hence my previous comment about correctly reporting twips/pixel

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
>but can it always know what monitor it is plugged into?

Yes, thanks to VESA's DDC and EDID which provide Microsoft's plug and play functionality for monitors.

Your BIOS and graphics adapter can use DDC to interrogate your monitor, returning the EDID

EDID is a 128-byte data structure containing info about the monitor (it's where Windows gets info about valid resolutions and refresh rates). Two integers in the EDID contain the actual, physical monitor display dimensions in centimeters
 
>many monotors don't report fully

Pah! I think they do, but get ignored by lazy graphics driver writers...

Just for fun, you might be interested in this little utility which will read and decode your monitor's info. Bytes 21 and 22 contain the physical display dimensions.

In fact, having found that, I think I see a VB program coming on...
 
And here we go. It's rough, and actually interrogates the copy of the EDID info that Windows sticks into the registry after detecting and querying a new Plug and Play monitor being connected rather than directly querying the monitor itself. But it shows (or at least should do) that your monitor does supply it's physical size, and that Windows takes a record of that.

OK, the following declarations in a module:
[tt]
Option Explicit

Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_ENUMERATE_SUB_KEYS = &H8
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const ERROR_SUCCESS = 0
Public Const ERROR_NO_MORE_ITEMS = 259&

Public Declare Function RegOpenKeyEx Lib &quot;advapi32&quot; Alias &quot;RegOpenKeyExA&quot; (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, ByRef phkResult As Long) As Long
Public Declare Function RegQueryValueEx Lib &quot;advapi32&quot; Alias &quot;RegQueryValueExA&quot; (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, ByRef lpType As Long, ByVal lpData As String, ByRef lpcbData As Long) As Long
Public Declare Function RegCloseKey Lib &quot;advapi32&quot; (ByVal hKey As Long) As Long
Public Declare Function RegEnumKey Lib &quot;advapi32.dll&quot; Alias &quot;RegEnumKeyA&quot; (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long
[/tt]
And the following in a form:
[tt]
Option Explicit

Private Sub Form_Load()
Me.Show
MonitorPeek
End Sub

Private Sub MonitorPeek()
Dim hKey As Long
Dim strSubkey As String
Dim cbName As Long
Dim result As Long
Dim Index As Long
Dim MonitorKey As String
Dim DisplayKey As String

Form1.Cls
DisplayKey = &quot;System\CurrentControlSet\Enum\DISPLAY&quot;
RegOpenKeyEx HKEY_LOCAL_MACHINE, DisplayKey, 0, KEY_ENUMERATE_SUB_KEYS, hKey
Debug.Print hKey
strSubkey = Space(1024)
cbName = 1024

Print &quot;Windows has interrogated the following monitors:&quot;

result = RegEnumKey(hKey, Index, strSubkey, cbName)
Do Until result = ERROR_NO_MORE_ITEMS
MonitorKey = Left(strSubkey, InStr(strSubkey, Chr(0)) - 1)
Print MonitorKey
GetMonitorEDID DisplayKey & &quot;\&quot; & MonitorKey
Index = Index + 1
result = RegEnumKey(hKey, Index, strSubkey, cbName)
Loop
RegCloseKey hKey
End Sub

Private Sub GetMonitorEDID(strKey As String)
Dim hKey As Long

Dim strSubkey As String
Dim cbName As Long
Dim result As Long
Dim NewSubKey As String
Dim bBuffer As String * 256
Dim cbBuffer As Long

RegOpenKeyEx HKEY_LOCAL_MACHINE, strKey, 0, KEY_ENUMERATE_SUB_KEYS, hKey


strSubkey = Space(1024)
cbName = 1024
result = RegEnumKey(hKey, 0, strSubkey, cbName)
NewSubKey = Left(strSubkey, InStr(strSubkey, Chr(0)) - 1)
RegCloseKey hKey
RegOpenKeyEx HKEY_LOCAL_MACHINE, strKey + &quot;\&quot; + NewSubKey + &quot;\Device Parameters&quot;, 0, KEY_QUERY_VALUE, hKey
cbBuffer = 256
RegQueryValueEx hKey, &quot;EDID&quot;, 0&, 0&, bBuffer, cbBuffer
Print Asc(Mid(bBuffer, 22, 1)) * 0.3937 & &quot; x &quot; & Asc(Mid(bBuffer, 23, 1)) * 0.3937 ' centimeters to inches
RegCloseKey hKey
End Sub
 
Nice code strongm (works better than the download!).

My previous statement was based on my kit close to hand:
>>many monotors don't report fully

>Pah! I think they do, but get ignored by lazy graphics driver writers...

I guess the kit you have access to is more up-to-date (or just better kit) than the stuff I have immediate access to.

NEC Multisync - works
IBM1990 - works
EcoScan17 - doesn't work


EDID.EXE (from the download) produces similar results.

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Well, it can depend on both your monitor and your VGA cable being DDC-compliant. Most VGA cables are, but I'd agree that there may well be a few monitors around that only implement the bare minimum features required to be described as plug and play; any model with the word 'Eco' in it, where 'eco' is short for 'Economy' rather than 'Ecological' might be a fair candidate...;-)
 
I have thanked strongm for his valuable post.

I must admit this is way over my head, and i am still confused how to convert the user specified inches to the width and height of my picture box.

I am in the development team of an image processing application. We have a dialog that opens when the user wants to create a new image, and asks him for the size of the file. If the user enters 5 1/4 inches and 6 inches for width and height, i want to set the size of the picture box, with this inches, which will be the size of the image that he is developing. If you print the image and measure, that must be the 5 1/4 X 6 inches, which the user has input earlier.

Pls suggest.

Thanks ,
Sunil

------------------------------------------
The faulty interface lies between the chair and the keyboard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top