2

Does anyone have any examples of using this System.Drawing.Graphics library to draw tables using DrawRectangle or DrawRectangles or any other method?

I'm trying to set up here with DrawRectangle, but it's difficult, it takes too long to do something simple and it´s a no intuitive tool. I've crafted articles on the web, but I have not found anything beyond what I've learned so far.

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
Vilmar Oliveira
  • 117
  • 1
  • 7
  • You have the right approach - use `DrawLine` for the internal lines, instead of `DrawRectangle`. Use `DrawRectangle` for the outside border (and perhaps cell shading). – SSS Jul 06 '17 at 01:05
  • `it takes too long to do something simple` - Programming isn't necessarily _simple_ just because the outcome is. And it is definitely more complicated when it comes to doing something from scratch. Though the code that you have now is not very long nor extremely complicated. – Visual Vincent Jul 06 '17 at 07:32
  • Please do not post code in comments. Please _edit_ your post and add the code there. – Chris Dunaway Jul 06 '17 at 13:30
  • Thank you guys. I will post the solution with code that I have founded in the answer section bellow due orientation of this website. – Vilmar Oliveira Jul 06 '17 at 17:40
  • For future reference: Based on your answer you're coding in C#, not VB.NET. Might be easier to get answers if you ask questions about the correct language. ;) – Visual Vincent Jul 10 '17 at 10:25

1 Answers1

2

The solution is here bellow:

protected void Page_Load(object sender, EventArgs e) 
{
    var image = new Bitmap(800, 600);
    try 
    {
        var graph = Graphics.FromImage(image);
        graph.FillRectangle(Brushes.White, new Rectangle(new Point(0, 0), image.Size));
        for (int col = 0; col < image.Width; col += 100) {
            graph.DrawLine(Pens.Black, new Point(col, 0), new Point(col, image.Height));
        }
        for (int row = 0; row < image.Height; row += 30) {
            graph.DrawLine(Pens.Black, new Point(0, row), new Point(image.Width, row));
        }
        graph.DrawRectangle(Pens.Black, new Rectangle(0, 0, image.Width - 1, image.Height - 1));

        Response.Clear();
        Response.ContentType = "image/jpeg";
        image.Save(Response.OutputStream, ImageFormat.Jpeg);
        Response.End();
    } 
    finally 
    {
        image.Dispose();
    }
}
Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76
Vilmar Oliveira
  • 117
  • 1
  • 7
  • Don't forget to dispose of your GDI objects when you are done with them. `graph.Dispose()` after you are done drawing. You should dispose of `image` also, after it gets sent the response stream. – Bradley Uffner Jul 06 '17 at 19:43