C# 加载图片编辑

C# 加载图片编辑,c#,drawing,picturebox,C#,Drawing,Picturebox,我应该加载一个文件中的图像,这个图像应该覆盖80%的pictureBox,然后在上面画一些东西。。。加载时没有问题,但尝试在其上绘制任何内容会导致一个错误,该错误包含一个unpper参数(g.FillRectangle…) 我在堆栈上找到了刷新pictureBox的建议,但它没有改变任何东西… 我不知道如何解决这个问题 private void button1_Click_1(object sender, EventArgs e) { pictureBox1.Width = (int)(

我应该加载一个文件中的图像,这个图像应该覆盖80%的pictureBox,然后在上面画一些东西。。。加载时没有问题,但尝试在其上绘制任何内容会导致一个错误,该错误包含一个unpper参数(g.FillRectangle…)

我在堆栈上找到了刷新pictureBox的建议,但它没有改变任何东西…
我不知道如何解决这个问题

private void button1_Click_1(object sender, EventArgs e)
{
    pictureBox1.Width = (int)(Width * 0.80);
    pictureBox1.Height = (int)(Height * 0.80);

    // open file dialog 
    OpenFileDialog open = new OpenFileDialog();
    // image filters
    open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
    if (open.ShowDialog() == DialogResult.OK)
    {
        // display image in picture box
        pictureBox1.Image = new Bitmap(open.FileName);
        // image file path
        //  textBox1.Text = open.FileName;
        g.FillRectangle(Brushes.Red, 0, 0, 20, 50);
        pictureBox1.Refresh();
    }
}
使用或使用在图像上绘制的方法:

var img = new Bitmap(open.FileName);
using (Graphics g = Graphics.FromImage(img))
{
    g.FillRectangle(Brushes.Red, 0, 0, 20, 50);  
}
pictureBox1.Image = img;
或通过
Paint
事件直接在
PictureBox
上绘制(例如使用):


下面的代码对我来说很好。。。。你能试试同样的吗

private void button1_Click(object sender, EventArgs e)
        {
            pictureBox1.Width = (int)(Width * 0.80);
            pictureBox1.Height = (int)(Height * 0.80);


            // open file dialog 
            OpenFileDialog open = new OpenFileDialog();

            // image filters
            open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
            if (open.ShowDialog() == DialogResult.OK)
            {
                // display image in picture box
                pictureBox1.Image = new Bitmap(open.FileName);
                // image file path
                //  textBox1.Text = open.FileName;
                Graphics g = Graphics.FromImage(pictureBox1.Image);
                g.FillRectangle(Brushes.Red, 0, 0, 20, 50);
                pictureBox1.Refresh();
            }
        }

为什么不在将位图图像加载到图片框之前绘制它?变量
g
来自哪里?我想它是用来表示一个
图形
实例的?我已经编辑了你的标题。请看,“,其中的共识是“不,他们不应该”。大家好!图形g=Graphics.FromImage(pictureBox1.Image);这管用!我不知道为什么我的Graphic g实现不起作用,它是在整个程序开始时直接实现的。。。。
private void button1_Click(object sender, EventArgs e)
        {
            pictureBox1.Width = (int)(Width * 0.80);
            pictureBox1.Height = (int)(Height * 0.80);


            // open file dialog 
            OpenFileDialog open = new OpenFileDialog();

            // image filters
            open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
            if (open.ShowDialog() == DialogResult.OK)
            {
                // display image in picture box
                pictureBox1.Image = new Bitmap(open.FileName);
                // image file path
                //  textBox1.Text = open.FileName;
                Graphics g = Graphics.FromImage(pictureBox1.Image);
                g.FillRectangle(Brushes.Red, 0, 0, 20, 50);
                pictureBox1.Refresh();
            }
        }