# Size of 1 pixel

• 07-26-2002
Size of 1 pixel
I want to ask about the dimension of 1 pixel in inch unit.

My code is below. When I print the Rectangle. It's very small. So I wonder about unit of x and y in this below code.Are they in pixel unit or other unit?

code:
-------------------------------------------------------------
int x = 0 ;
int y = 0 ;

CBrush brush;

brush.CreateSolidBrush(RGB(0, 0, 0));

CRect Rectangle(x, y, x + 2, y + 4) ;

dc.FillRect(&Rectangle , &brush);
-------------------------------------------------------------
• 07-26-2002
frenchfry164
to find that out you would have to take your screen's size horizontally in inches, and divide by the width of the resolution in pixels. then do the same for the height. Then multiply the two answers and you got the area in square inches.
• 07-26-2002
fletch
Looks like MFC.
Quote:

I want to ask about the dimension of 1 pixel in inch unit.
Doesn't matter. Windows can do it all for you.

Windows deals with drawing in terms of logical coordinates and device coordinates. Device coordinates are physical pixels on your monitor. Logical coordinates are a 'mapping system' used to position things in a window. All of the CDC class functions that accept coordinates (like CDC::FillRect) use logical coordinates.

By default, logical coordinates directly correspond to device coordinates (logical coordinate x,y corresponds to exactly one pixel). So, the code
Code:

```brush.CreateSolidBrush(RGB(0, 0, 0)); CRect Rectangle(0, 0, 100, 200) ;  //Simplified your coordinates for example clarity dc.FillRect(&Rectangle , &brush);```
will fill a rectangle that is 100 logical units by 200 logical units. Since, by default, logical coordinates and device coordinates are equal, the rectangle is 100 pixels by 200 pixels. You can, however, change the relationship between logical coordinates and device coordinates by using CDC::SetMapMode(int MappingMode). The Mapping Modes are:
Code:

```MappingMode                    One logical unit equals MM_TEXT                          1 pixel MM_LOMETRIC                      .1 mm MM_HIMETRIC                      .01 mm MM_LOENGLISH                      .01 in MM_HIENGLISH                      .001 in MM_TWIPS                          .0007 in MM_ISOTROPIC                      user defined MM_ANISTROPIC                    user defined```
The code
Code:

```brush.CreateSolidBrush(RGB(0, 0, 0)); CRect Rectangle(0, 0, 100, 200) ;  //Simplified your coordinates for example clarity dc.SetMapMode(MM_LOENGLISH); dc.FillRect(&Rectangle , &brush);```
will still fill a rectangle that is 100 logical units by 200 logical units. But, since we changed the mapping mode to MM_LOENGLISH, that rectangle is now 1 inch by 2 inches. If you measure the rectangle on your monitor, it should actually come out to 1x2 inches, though sometimes it's not exact due to differences in monitors, resolution, etc. If the device context is refering to a printer, it will come out exact because a printer has a fixed resolution (dpi).

Note that logical and device coordinates apply only to CDC class functions. The CRect class takes 4 integers as the corners for the rectangle, but CRect does not differentiate between logical or device coordinates; CDC::FillRect applies the 4 integers as logical coordinates.

fletch
• 07-26-2002
• 07-26-2002
fletch
Sure, no problem. I forgot one thing though...

For all of the mapping modes except MM_TEXT, the window is in the bottom right quadrant of the x,y axis...your 'y' values should be negative.
Code:

```      top left corner ----------------------> +x                       |                       |      Window space                       |                       |                       |                       \/                     -y```
The MM_TEXT mode is the conventional +x,+y that you're used to.
Correcting the previous code...
Code:

```brush.CreateSolidBrush(RGB(0, 0, 0)); CRect Rectangle(0, 0, 100, -200) ;  //Simplified your coordinates for example clarity. -200 vice +200 dc.SetMapMode(MM_LOENGLISH); dc.FillRect(&Rectangle , &brush);```
fletch