0

I have code that prints a till receipt which includes users string, the string is not fixed the user can type more or even less, thus i am not sure about the length of string user will type, i have made a rectangle to print the users string and the height will be the amount of letters in the string, it works but when the string has more characters the receipt has a big white gap before it prints the string so basically not the right way. also as the string changes, becomes short the rectangle position changes and distorts everything.

THIS IS LINK OF THE IMAGE HOW ITS PRINTED

this is the code that prints the rectangle:

        graphics.DrawString("CONDITIONS OF SALE", bold2, Brushes.Black, 70, 60 + offX);

         //THIS IS WHERE THE RECTANGLE IS PRINTED


        Graphics pe = e.Graphics;

        float aa = pe.MeasureString(SALE, regular, 0, StringFormat.GenericTypographic).Width + 40;

        Size textSize = TextRenderer.MeasureText(SALE, regular);


        var r = new Rectangle(2, textSize.Height + 130, 275, textSize.Height + offX);
       //THIS IS THE RECTANGLE WHICH IS USED TO PRINT THE STRING WHICH IS CALLED SALE


        StringFormat s = new StringFormat();
        s.Alignment = StringAlignment.Center;
        StringFormat drawFormat = new StringFormat();
        drawFormat.Alignment = StringAlignment.Center;
        drawFormat.LineAlignment = StringAlignment.Center;


        e.Graphics.DrawString(SALE + "[ " + COUNT.ToString() + " ]", regular, Brushes.Black, r, drawFormat);  //PRINTS THE STRING CALLED SALE.//THIS IS THE PROBLEM
       e.HasMorePages = false;


        regular.Dispose();
        bold.Dispose();
abru
  • 1
  • 1
  • TextRenderer is notorious for giving wrong values when measuring a string. Try to use GraphicsPath instead it will more accurately give a measurement. Alternatively (and more costly) you can render the text into a white bitmap, then measure the max bounding box (black pixel top left to black pixel bottom right) to get the size. – Rasmus Søborg Jun 08 '20 at 11:01
  • @RasmusSøborg i have no idea how to go about doing that can you give me an example – abru Jun 08 '20 at 11:05
  • 2
    TextRenderer is not for printing but for rendering controls. Also: Do use the overload of MeasureString that takes and returns a SizeF ! Also a Stringformat! [Printing example](https://stackoverflow.com/questions/28560319/generate-staff-card/28580657#28580657) – TaW Jun 08 '20 at 11:06
  • @abru I am in a bit of a time hurry. If nobody has answered when I am home I can show u how to do it :) – Rasmus Søborg Jun 08 '20 at 11:30
  • @TaW i have seen the example and there they already know the size of the rectangle where as for me i dont have an idea how big the rectangle will be and rectangle accepts int digits only. – abru Jun 08 '20 at 11:42
  • The usual way is to know the width and to let the system measure the height necessary to fit the text in with a given font.. You can pass in a large (tall) rectangle and only care for the resulting height needed or you can feed in a smaller rect and use an overload that tells you how much has fit in.. – TaW Jun 08 '20 at 11:46
  • The two images have different printing contents! The first image has `Total` and `Vat` lines, the second doesn't. The second has a `Conditions of Sale` line, the first doesn't. That what produces that gap. Calculating the locations and sizes routine should take into account these differences. –  Jun 09 '20 at 11:00
  • @JQSOFT i have erased the total and vat lines to write out the problem in red, the *conditions of sale* is there but its distorted because it is covered by the rectangle which goes up if the string is short. thats the problem. – abru Jun 10 '20 at 06:27
  • @RasmusSøborg any luck, still struggling? – abru Jun 19 '20 at 09:02

0 Answers0