我应该从文件中加载一个图像,这个图像应该覆盖80%的pictureBox,然后在上面绘制一些内容......加载时没有问题,但是尝试在其上绘制任何内容会丢失一个错误一个不合适的参数(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();
}
}
答案 0 :(得分:0)
使用Graphics.FromImage
或Control.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);
答案 1 :(得分: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();
}
}