加载图片编辑

本文关键字:编辑 加载 | 更新日期: 2023-09-27 17:55:10

我应该从文件中加载一个图像,这个图像应该覆盖 pictureBox 的 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();
    }
}

加载图片编辑

使用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);
下面的

代码对我来说工作正常....你能试试一样吗

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