1

我应该从文件中加载图像,并且该图像应覆盖图片框的 80%,然后在其上绘制一些东西...加载没有问题,但是尝试在其上绘制任何内容会导致错误,该错误具有不正确的参数(g.FillRectangle...)。

我在堆栈建议中找到了刷新图片框的建议,但它没有任何改变......
而且我不知道如何解决这个问题......

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();
    }
}
4

2 回答 2

0

在图像上绘制的使用Graphics.FromImageControl.CreateGraphics方法:

var img = new Bitmap(open.FileName);
using (Graphics g = Graphics.FromImage(img))
{
    g.FillRectangle(Brushes.Red, 0, 0, 20, 50);  
}
pictureBox1.Image = img;

PictureBox或直接按事件绘制Paint(例如使用Anonymous Methods):

pictureBox1.Paint += (s, e) => e.Graphics.FillRectangle(Brushes.Red, 0, 0, 20, 50);
于 2013-01-23T04:29:46.130 回答
0

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

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();
            }
        }
于 2013-01-23T04:47:49.043 回答