我试图像画格的图片框一个方格纸,然后打印出来。 我想我最好的进攻路线,这将是DrawGrid功能。 我得到了一些东西下来,然后土崩瓦解后,我到了控制油漆部分。 我如何能画转换为绘画。 请解释一下,因为现在我相当困惑。
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
如果您从创建代码的图像,你需要创建正确尺寸的位图。 然后通过做根据需要选择颜色的所有像素双回路。 然后设置为图片的图片框和/或保存到文件。
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!