如何使用ControlPaint.DrawGrid绘制到PictureBox [英] How to use ControlPaint.DrawGrid To Draw To a PictureBox

查看:217
本文介绍了如何使用ControlPaint.DrawGrid绘制到PictureBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一张格子图像绘制成一个图片框,然后将其打印出来。我认为我的最佳攻击途径是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 = //这里应该是什么


解决方案




  • 以下是如何使用网格创建位图:

    / li>






  private void button1_Click(object sender,EventArgs e)
{

位图bmp =新位图(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 。黑色);
}
//现在你可以保存它..
bmp.Save(yourPngFileName,ImageFormat.Png);
// ..或者插入它作为Image ..
pictureBox1.Image = bmp;
// ..or作为背景图片:
pictureBox1.BackgroundImage = bmp;
}

如果您使用不同的网格进行播放,您应该在设置之前将一行插入 Dispose 因此你不会泄漏内存!例如:

  if(pictureBox1.Image!= null)pictureBox1.Image。 Dispose(); 




  • 或者您可以简单地将绘图代码放入Paint事件并让它在每个Invalidate上绘制:





 无效pictureBox1_Paint(对象发件人,PaintEventArgs的E)
{
BMP位图=新位图(pictureBox1.ClientSize.Width,pictureBox1.ClientSize.Height);
尺寸yourGridspacing =新大小((int)numericUpDown1.Value,(int)numericUpDown2。值);
using(Graphics G = pictureBox1.CreateGraphics())
ControlPaint.DrawGrid(G,new Rectangle(Point.Empty,bmp.Size),
yourGridspacing,Color.Black);


请注意 BackColor 参数:


backColor参数用于计算
点的填充颜色,以便网格对于背景始终可见。


这意味着您无法真正控制网格的坐标。相反,系统会选择一个与 BackColor 参数表相关的对象。所以如果你的PictureBox恰好是 White Black 的参数将是不可见的!



所以设置这个参数的最好方法是:
$ b $ pre $ ControlPaint.DrawGrid(G,new Rectangle (Point.Empty,bmp.Size),
yourGridspacing,Color.Black);

除了 Color.Transparent ...(在这种情况下,下面的控件的颜色将决定网格是否可见)。


  • 或者,如果你想你可以使用 BackgroundImage 作为你的网格间距的大小(Tile)的一个微小的位图。与使用大型位图相比,这会占用更少的内存。






  private void button2_Click(object sender,EventArgs e)
{
位图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;
}

这也将使您完全控制实际颜色!


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

解决方案

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!

这篇关于如何使用ControlPaint.DrawGrid绘制到PictureBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆