C# 如何用C行填充图片框#

C# 如何用C行填充图片框#,c#,image,lines,C#,Image,Lines,所以我不得不用线条填充我的图片框,尽管我不明白我做错了什么 public Form1() { InitializeComponent(); PictureBox pb = new PictureBox(); } public void Zimet() { PictureBox pb = new PictureBox(); Graphic

所以我不得不用线条填充我的图片框,尽管我不明白我做错了什么

public Form1()
        {
            InitializeComponent();
            PictureBox pb = new PictureBox();
        }
        public void Zimet()
        {
            PictureBox pb = new PictureBox();

            Graphics g = pb.CreateGraphics();
            Pen pen1 = new Pen(Color.Red);
            for (int i = 0; i < pb.Height; i++)
            {
                g.DrawLine(pen1, pb.Width, 0, 0, pb.Height);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Zimet();
        }
public Form1()
{
初始化组件();
PictureBox pb=新PictureBox();
}
公共空间Zimet()
{
PictureBox pb=新PictureBox();
Graphics g=pb.CreateGraphics();
Pen pen1=新笔(颜色为红色);
对于(int i=0;i
您的
Zimet()
函数创建一个
Picturebox
并将其绘制,然后在函数末尾将其丢弃。

您需要调用
SomeUiObject.Controls.Add(pb)
将其实际放在表单上。

您的
Zimet()
函数创建一个
Picturebox
并绘制到它,然后在函数末尾将其丢弃。

您需要调用
SomeUiObject.Controls.Add(pb)
将其实际放在表单上。

您的
Zimet()
函数创建一个
Picturebox
并绘制到它,然后在函数末尾将其丢弃。

您需要调用
SomeUiObject.Controls.Add(pb)
将其实际放在表单上。

您的
Zimet()
函数创建一个
Picturebox
并绘制到它,然后在函数末尾将其丢弃。


您需要调用
SomeUiObject.Controls.Add(pb)
将其实际放置在表单上。

首先,您的
图片盒应该是
表单的字段,您不应该每次单击按钮都创建它。其次,您的
DrawLine
调用不正确,例如,如果您想要水平线,则需要执行以下操作:

        Pen pen1 = new Pen(Color.Red);
        for (int i = 0; i < pb.Height; i++)
        {
            g.DrawLine(pen1, 0, i, pb.Width, i);
        }
public class Form1:Form
{
    PictureBox pb;
    bool drawLines = false;
    public Form1()
    {
        InitializeComponent();
        pb = new PictureBox();
        pb.Size = new Size(100,100);
        pb.Location = new Point(0,0);
        pb.Paint+=new PaintEventHandler(pb_Paint);
        this.Controls.Add(pb);
    }
    private void pb_Paint(object sender, PaintEventArgs e)
    {
        if(drawLines)
        {
            Pen pen1 = new Pen(Color.Red);
            for (int i = 0; i < pb.Height; i+=2)
            {
               e.Graphic.DrawLine(pen1, pb.Width, 0, 0, pb.Height);
            }
        }
    }
    public void Zimet()
    {
        drawLines = true; //however this may look redundant, it is still OP's code
    }
    private void button1_Click(object sender, EventArgs e)
    {
        Zimet();
    }
Pen pen1=新笔(颜色为红色);
对于(int i=0;i
但这与用红色填充pictureBox是一样的。相反,我建议您通过更新I+=2跳过每隔一行

        Pen pen1 = new Pen(Color.Red);
        for (int i = 0; i < pb.Height; i+=2)
        {
            g.DrawLine(pen1, 0, i, pb.Width, i);
        }
Pen pen1=新笔(颜色为红色);
对于(int i=0;i
尽管如此,我还是建议您不要使用CreateGraphic()方法,而是在Paint事件处理程序中进行所有绘制。原因是,每当表单无效时,图形都将被删除。例如,使用单击按钮时将设置的某些布尔值,如果为真,则绘制图形。大概是这样的:

        Pen pen1 = new Pen(Color.Red);
        for (int i = 0; i < pb.Height; i++)
        {
            g.DrawLine(pen1, 0, i, pb.Width, i);
        }
public class Form1:Form
{
    PictureBox pb;
    bool drawLines = false;
    public Form1()
    {
        InitializeComponent();
        pb = new PictureBox();
        pb.Size = new Size(100,100);
        pb.Location = new Point(0,0);
        pb.Paint+=new PaintEventHandler(pb_Paint);
        this.Controls.Add(pb);
    }
    private void pb_Paint(object sender, PaintEventArgs e)
    {
        if(drawLines)
        {
            Pen pen1 = new Pen(Color.Red);
            for (int i = 0; i < pb.Height; i+=2)
            {
               e.Graphic.DrawLine(pen1, pb.Width, 0, 0, pb.Height);
            }
        }
    }
    public void Zimet()
    {
        drawLines = true; //however this may look redundant, it is still OP's code
    }
    private void button1_Click(object sender, EventArgs e)
    {
        Zimet();
    }
公共类表单1:表单
{
图片盒pb;
布尔抽绳=假;
公共表格1()
{
初始化组件();
pb=新的PictureBox();
pb.尺寸=新尺寸(100100);
pb.位置=新点(0,0);
pb.Paint+=新的PaintEventHandler(pb_Paint);
this.Controls.Add(pb);
}
私有void pb_Paint(对象发送器,PaintEventArgs e)
{
if(抽绳)
{
Pen pen1=新笔(颜色为红色);
对于(int i=0;i
首先,你的
图片盒应该是你的
表单的字段,你不应该每次点击按钮都创建它。其次,你的
绘图线调用是错误的,例如,如果你想要水平线,你需要这样做:

        Pen pen1 = new Pen(Color.Red);
        for (int i = 0; i < pb.Height; i++)
        {
            g.DrawLine(pen1, 0, i, pb.Width, i);
        }
public class Form1:Form
{
    PictureBox pb;
    bool drawLines = false;
    public Form1()
    {
        InitializeComponent();
        pb = new PictureBox();
        pb.Size = new Size(100,100);
        pb.Location = new Point(0,0);
        pb.Paint+=new PaintEventHandler(pb_Paint);
        this.Controls.Add(pb);
    }
    private void pb_Paint(object sender, PaintEventArgs e)
    {
        if(drawLines)
        {
            Pen pen1 = new Pen(Color.Red);
            for (int i = 0; i < pb.Height; i+=2)
            {
               e.Graphic.DrawLine(pen1, pb.Width, 0, 0, pb.Height);
            }
        }
    }
    public void Zimet()
    {
        drawLines = true; //however this may look redundant, it is still OP's code
    }
    private void button1_Click(object sender, EventArgs e)
    {
        Zimet();
    }
Pen pen1=新笔(颜色为红色);
对于(int i=0;i
但这与用红色填充pictureBox是一样的。相反,我建议您通过更新I+=2跳过每隔一行

        Pen pen1 = new Pen(Color.Red);
        for (int i = 0; i < pb.Height; i+=2)
        {
            g.DrawLine(pen1, 0, i, pb.Width, i);
        }
Pen pen1=新笔(颜色为红色);
对于(int i=0;i
尽管如此,我还是建议您不要使用CreateGraphic()方法,而是在Paint事件处理程序中绘制所有图形。原因是,每当表单无效时,您的图形都将被删除。例如,使用一些bool值,当您单击按钮时将设置该值,如果该值为真,则绘制图形。类似于以下内容:

        Pen pen1 = new Pen(Color.Red);
        for (int i = 0; i < pb.Height; i++)
        {
            g.DrawLine(pen1, 0, i, pb.Width, i);
        }
public class Form1:Form
{
    PictureBox pb;
    bool drawLines = false;
    public Form1()
    {
        InitializeComponent();
        pb = new PictureBox();
        pb.Size = new Size(100,100);
        pb.Location = new Point(0,0);
        pb.Paint+=new PaintEventHandler(pb_Paint);
        this.Controls.Add(pb);
    }
    private void pb_Paint(object sender, PaintEventArgs e)
    {
        if(drawLines)
        {
            Pen pen1 = new Pen(Color.Red);
            for (int i = 0; i < pb.Height; i+=2)
            {
               e.Graphic.DrawLine(pen1, pb.Width, 0, 0, pb.Height);
            }
        }
    }
    public void Zimet()
    {
        drawLines = true; //however this may look redundant, it is still OP's code
    }
    private void button1_Click(object sender, EventArgs e)
    {
        Zimet();
    }
公共类表单1:表单
{
图片盒pb;
布尔抽绳=假;
公共表格1()
{
初始化组件();
pb=新的PictureBox();
pb.尺寸=新尺寸(100100);
pb.位置=新点(0,0);
pb.Paint+=新的PaintEventHandler(pb_Paint);
this.Controls.Add(pb);
}
私有void pb_Paint(对象发送器,PaintEventArgs e)
{
if(抽绳)
{
Pen pen1=新笔(颜色为红色);
对于(int i=0;i
首先,你的
图片盒应该是你的
表单的字段,你不应该每次点击按钮都创建它。其次,你的
绘图线调用是错误的,例如,如果你想要水平线,你需要这样做:

        Pen pen1 = new Pen(Color.Red);
        for (int i = 0; i < pb.Height; i++)
        {
            g.DrawLine(pen1, 0, i, pb.Width, i);
        }
public class Form1:Form
{
    PictureBox pb;
    bool drawLines = false;
    public Form1()
    {
        InitializeComponent();
        pb = new PictureBox();
        pb.Size = new Size(100,100);
        pb.Location = new Point(0,0);
        pb.Paint+=new PaintEventHandler(pb_Paint);
        this.Controls.Add(pb);
    }
    private void pb_Paint(object sender, PaintEventArgs e)
    {
        if(drawLines)
        {
            Pen pen1 = new Pen(Color.Red);
            for (int i = 0; i < pb.Height; i+=2)
            {
               e.Graphic.DrawLine(pen1, pb.Width, 0, 0, pb.Height);
            }
        }
    }
    public void Zimet()
    {
        drawLines = true; //however this may look redundant, it is still OP's code
    }
    private void button1_Click(object sender, EventArgs e)
    {
        Zimet();
    }
Pen pen1=新笔(颜色为红色);
对于(int i=0;i