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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# metric transformation 1

Status
Not open for further replies.

arminro

Programmer
Mar 6, 2005
5
RO
how do i transform inches to pixels and viceversa ? if it was c++ i'd use the device context functions lptodp and lptohimetric but i need a C# solution.

i need to resize some richtextboxes to an exact size(on screen). the thing is the size i need to set it to is in inches and i need pixels. normaly you would expect the graphics class to have such transformation functions because these depend on the dpi of the device context but unless i'm missing something i can't find them!!
 
Some info from Chris Sells book.
//from inches to pixels:

Graphics g=this.CreateGraphics();
pixels=inches*g.Dpix;

// to do the other way
g.PageUnit=GraphicsUnit.Inch;
g.PageScale=1; // 1 unit = i inch;

Hope this is useful
 
in your example g.Dpix is the device horizontal Dpi(dots per inch). dots however are not pixels.
 
Sorry Arminro my fault , don't know what I was thinking.
It is g.TransformPoints you need to use.

g.PageUnit=GraphicsUnit.Inch;
g.PageScale=1;
PointF[] bottom;
bottom=new PointF[]{new PointF(this.ClientSize.Width,this.ClientSize.Height)};
g.TransformPoints(CoordinateSpace.Page,CoordinateSpace.Device,bottom);
////
You can convert back and forth between page and device units using this . The first parameter is the destination units (inches in this case) , and the second is source units (Pixels in this case). Device units are pixels.
 
a few things. first thanks .. because you've shed some light on this. it works to a certain point. as in the width of the richtextbox is about fine, but the text isn't how i desired to be. there are diferences betwin the print version and the one i show on the screen and i think it's because of the scrollbar and edges of the workarea. summing these two adds up to something more then a centimeter and that's alot. and ofcourse i resize the clientsize of the richtextbox but .. i think these are inside this area..
anyways thanks again for the help.
 
arminro ,
I don't know whether this will work but try using

from my earlier code

// where e is PrintPageEventArgs e

RectangleF vbp = e.Graphics.VisibleClipBounds ;
.............

bottom = {new PointF(vbp.Size.Width,vbp.Size.Height)};

Let me know hoow you get on , I am curious too.
 
umm
maybe if i'll show you the code i'll explain better:
Graphics g=this.outputrichbox.CreateGraphics();
g.PageUnit=GraphicsUnit.Inch;
g.PageScale=1;
PointF[] bottom=new PointF[3];
bottom[0]=new PointF((float)this.pageSetupDialog1.PageSettings.PaperSize.Width/100,1);
bottom[1]=new PointF((float)this.pageSetupDialog1.PageSettings.Margins.Left/100,1);
bottom[2]=new PointF((float)this.pageSetupDialog1.PageSettings.Margins.Right/100,1); g.TransformPoints(CoordinateSpace.Device,CoordinateSpace.Page,bottom);
g.Dispose();
int pixelix=(int)bottom[0].X;
this.outputrichbox.ClientSize=new Size(pixelix,this.outputrichbox.ClientSize.Height);
pixelix=(int)bottom[1].X; this.outputrichbox.SelectionIndent=pixelix;
pixelix=(int)bottom[2].X; this.outputrichbox.SelectionRightIndent=pixelix;
this.Refresh();

this.outputrichbox is the box i'm trying to get it resized and indented. before i do this i copy it's exact Rtf into another box and that one i print. the resizing seems to be ok but when i compare the texts not everything is allright.
this is what i use to print the contents:

public int Print( int charFrom, int charTo,PrintPageEventArgs e)
{
//Calculate the area to render and print
RECT rectToPrint;
rectToPrint.Top = (int)(e.MarginBounds.Top * anInch);
rectToPrint.Bottom = (int)(e.MarginBounds.Bottom * anInch);
rectToPrint.Left = (int)(e.MarginBounds.Left * anInch);
rectToPrint.Right = (int)(e.MarginBounds.Right * anInch);
//Calculate the size of the page
RECT rectPage;
rectPage.Top = (int)(e.PageBounds.Top * anInch);
rectPage.Bottom = (int)(e.PageBounds.Bottom * anInch);
rectPage.Left = (int)(e.PageBounds.Left * anInch);
rectPage.Right = (int)(e.PageBounds.Right * anInch);

IntPtr hdc = e.Graphics.GetHdc();

FORMATRANGE fmtRange;
fmtRange.chrg.cpMax = charTo; //Indicate character from to character to
fmtRange.chrg.cpMin = charFrom;
fmtRange.hdc = hdc; //Use the same DC for measuring and rendering
fmtRange.hdcTarget = hdc; //Point at printer hDC
fmtRange.rc = rectToPrint; //Indicate the area on page to print
fmtRange.rcPage = rectPage; //Indicate size of page

IntPtr res = IntPtr.Zero;

IntPtr wparam = IntPtr.Zero;
wparam = new IntPtr(1);

//Get the pointer to the FORMATRANGE structure in memory
IntPtr lparam= IntPtr.Zero;
lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange));
Marshal.StructureToPtr(fmtRange, lparam, false);

//Send the rendered data for printing
res = SendMessage(Handle, EM_FORMATRANGE, wparam, lparam);

//Free the block of memory allocated
Marshal.FreeCoTaskMem(lparam);

//Release the device context handle obtained by a previous call
e.Graphics.ReleaseHdc(hdc);

//Return last + 1 character printer
return res.ToInt32();
}
 
well i've traced and debug-ed a bit.
PrintPageEventArgs e.MarginBounds and e.PageBounds are correct and to the exact dimensions i need them to be. as in (if i select A4 paper) 827 in hundreads of an inch, 8,27 in normal inches.
i've set the richtext box Client size to this width as you have seen in my code (because i'm not interested in height just the width) but still the text is not the same betwin the shown version and the printed one ..
so now my problem is this. i have to adjust the box width so as to fit the text. i thought that client size was the thing i had adjust but .. it seems it's not or i just don't get it ..
not to mention i've noticed something almoust incredible: calculated size of width (in pixels) is 793 . to this i modify the richtext box.clientsize.width.
but when i create a new grafics and set the page unit to pixels the width of graphics.visibleclip.width is 777.
how can it be smaller?!?! it should be either equal or larger.. or ?!
 
I am not sure what exactly is going on . Have you tried
e.Graphics.VisibleClipBounds ?

May be you can use that and resize the font ?
for eg : of course height etc. would be your parameters.

Font AxisFont= new Font("Ariel",(Paper.Height/(sim.numDPlot+1))*0.4f );
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top