Results 1 to 10 of 10

Thread: Simple graphics

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Simple graphics

    Hi.

    What is the best way in C# to create graphics? more specifically, what are the best guidelines to say, make a rectangle on a document?

    What I am trying to do is print out a page. And I am making a simple "invoice" - and of course I would like to have sub sections such as customer, recepient and so on.

    So, I thought I would have these sections have rectangles, then within the rectangle to neatly line up or contain headings and so on.

    how can I do this?

    As well as this, does anyone know how I can put a line break or make a blank line on a PrintDocument object?

    Thanks

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Simple graphics

    PrintDocuments don't have any concept of line breaks. A PrintDocument provides the PrintPage event to draw anything you like on the printing surface. The 'e' argument has a Graphics property that is use to do the drawing. You draw whatever you want wherever you want using its methods like DrawString, DrawRectangle, DrawImage, etc. You control every aspect of what is printed, where it is printed and how it looks. It's all done using GDI+, the same as if you were drawing on a control on-screen. Any GDI+ tutorial is relevant.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Simple graphics

    Thanks, I know about the printpage events and so on - its just actually drawing to it, the graphics which I have never done and it's pretty tough!

    hm. what is the best way to write a string underneath another string. say for example, I have a collection of strings, for each string, how do i say print the current string, then move to the next line and print the next string on the document page?

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Simple graphics

    There is no concept of moving to the next line. You draw the first string at the coordinates you want, then you draw the next string at the coordinates you want, which will presumably be the same X value but a greater Y value so it is below the first. I suppose that you could Join all the strings with line breaks to make a single string and then draw that. The vertical spacing will then be controlled by the Font. If you draw multiple single line strings then you control the vertical spacing by specifying the coordinates for each one.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Simple graphics

    drats this is tough!
    I have made a rectangle and drawn it

    now I am trying to make the label headings inside the rectangle

    but how can I position the label a few bits away from the previous label?

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  6. #6
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Simple graphics

    The DrawString() method accepts a rectangle as one of its parameters, all the text you draw will be slotted neatly into that rect.

    What is particularly bothering you about graphics? e.Graphics.DrawRect(...) is all you need to do.
    I don't live here any more.

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Simple graphics

    really? Well I guess I'll give that a shot. I am not really good with graphics, and co-ordinates and so on lol - I prefer to do things properly the first time and make sure that it is almost "future" prone incase if I Decide to change the positioning of an object. But thats just me!

    So, you are saying if I have a rectangle, and I use drawstring, if I give it that rectangle it will neatly make the text centered?

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Simple graphics

    Take a look at all the methods of the Graphics class that start with "Draw" and the overload lists of those you're interested in. You can basically do whatever you want. You just have to pick the appropriate overload of the appropriate method and pass it the appropriate parameters.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Simple graphics

    Well I have tried that but it does not seem to align the text to be centered:

    Code:
    Rectangle theCompany = new Rectangle(20, 40, 200, 240);
    e.Graphics.DrawString("Some company", Font, Brushes.Black, theCompany);
    Last edited by Techno; Apr 8th, 2006 at 10:55 AM.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Simple graphics

    ok well seem to have resolved this one. use the StringFormat class, and then format the string, and give it to the DrawString method

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width