我应该从一个文件中加载一个图像,这个图像应该覆盖pictureBox的80%,然后在上面绘制一些东西…使用加载没有问题,但是尝试在上面绘制任何内容都会导致错误,该错误具有不正确的参数(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();
}
}发布于 2013-01-23 12:29:46
使用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;或者通过Paint事件直接在PictureBox上绘制(例如使用Anonymous Methods):
pictureBox1.Paint += (s, e) => e.Graphics.FillRectangle(Brushes.Red, 0, 0, 20, 50);发布于 2013-01-23 12:47:49
下面的代码对我来说工作得很好...你能试一下同样的吗?
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();
}
}https://stackoverflow.com/questions/14472411
复制相似问题