How to use ControlPaint.DrawGrid To Draw To a PictureBox

浪尽此生 提交于 2019-12-11 06:46:55

问题


I am attempting to draw a graph paper like grid to a picture box and then printing it out. I figured my best route of attack for this would be the DrawGrid function. I got some stuff down and then it fell apart after I got to the control paint part. How can I convert a Painting to drawing. Please explain, as of now I am fairly confused.

   Rectangle rect = new Rectangle();
            rect.Location = new System.Drawing.Point(0,0);
            rect.Height = (int)numericUpDown1.Value;
            rect.Width = (int)numericUpDown2.Value;

            ControlPaint.DrawGrid(File(yourImage), mygrid, yourImage.Size, System.Drawing.Color.Black);

            pictureBox1.Image = //What should be here

回答1:


If your creating an Image from code, you need create a Bitmap of correct dimensions. Then do double loop through all pixels choosing the colors as needed. Then set as Image for the PictureBox and/or save to file.




回答2:


I see three ways to do it:

  • Here is how you can create a Bitmap with the Grid:

private void button1_Click(object sender, EventArgs e)
{

   Bitmap bmp = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height);
   Size yourGridspacing = new Size((int)numericUpDown1.Value, (int)numericUpDown2.Value);
   using (Graphics G = Graphics.FromImage(bmp))
   {
      ControlPaint.DrawGrid(G, new Rectangle(Point.Empty, bmp.Size), 
                               yourGridspacing , Color.Black);
   }
   // now you can save it..
   bmp.Save("yourPngFileName, ImageFormat.Png);
   // ..or insert it as the Image..      
   pictureBox1.Image = bmp;
   // ..or as the Background Image:
   pictureBox1.BackgroundImage = bmp;
}

You should insert a line to Dispose the Image before setting it, if you play with different grid spacing, so you don't leak the memory! E.g.:

   if (pictureBox1.Image != null) pictureBox1.Image.Dispose();
  • Or you can instead simply put the Drawing code into the Paint event and let it be drawn on each Invalidate:

void pictureBox1_Paint(object sender, PaintEventArgs e)
{
   Bitmap bmp = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height);
   Size yourGridspacing = new Size((int)numericUpDown1.Value, (int)numericUpDown2.Value);
   using (Graphics G = pictureBox1.CreateGraphics())
         ControlPaint.DrawGrid(G, new Rectangle(Point.Empty, bmp.Size), 
                                  yourGridspacing , Color.Black);

}

Please note the meaning of the BackColor parameter:

The backColor parameter is used to calculate the fill color of the dots so that the grid is always visible against the background.

This means that you can't really control the coor of the Grid. Instead the system will pick one that contrats with the BackColor parmeter. So if your PictureBox happens to be White a param of Black will be invisible!!

So the best way to set that param is:

ControlPaint.DrawGrid(G, new Rectangle(Point.Empty, bmp.Size), 
                         yourGridspacing , Color.Black);

which will always work except for Color.Transparent.. (in which case the Color of the control below will decide if the Grid is visible..)

  • Or, if you want to, you may instead use the BackgroundImage as a tiny Bitmap of the Size of your grid spacing, which you Tile. That will take the less memory than using a large Bitmap.:

private void button2_Click(object sender, EventArgs e)
{
    Bitmap bmp = new Bitmap((int)numericUpDown1.Value, (int)numericUpDown2.Value);
    using (Graphics G = Graphics.FromImage(bmp))
    {
        bmp.SetPixel(0, 0, Color.Black);
    }
    if (pictureBox1.Image != null) pictureBox1.Image.Dispose();
    pictureBox1.BackgroundImage = bmp;
    pictureBox1.BackgroundImageLayout = ImageLayout.Tile;
}

This will also give you full control over the actual color!



来源:https://stackoverflow.com/questions/28206510/how-to-use-controlpaint-drawgrid-to-draw-to-a-picturebox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!