我正在尝试在图像上绘制一个矩形,该矩形已绘制但该图像消失了。
我的代码:
private void button12_Click(object sender, EventArgs e)
{
currentBitmap = (Bitmap)pictureBox1.Image;
Bitmap temp1 = new Bitmap(currentBitmap.Height, currentBitmap.Width);
Bitmap bmap = (Bitmap)temp1.Clone();
Graphics gr = Graphics.FromImage(bmap);
Pen pen = new Pen(Color.Red);
gr.DrawRectangle(pen, 120, 120, 100, 100);
currentBitmap = (Bitmap)bmap.Clone();
pictureBox1.Image = currentBitmap;
}
答案 0 :(得分:0)
将绘图方法放入pictureBox1 onPaint方法内。然后使用
pictureBox1.Paint + = picturebox1_OnPaint;
答案 1 :(得分:-1)
我解决了这个问题。 这是我没有意识到的代码错误,我只是在使用图片框的宽度和高度而不是图像数据。
我改变了
Bitmap temp1 = new Bitmap(currentBitmap.Height, currentBitmap.Width);
收件人
Bitmap temp1 = new Bitmap(pictureBox1.Image);
答案 2 :(得分:-2)
这可能是由于您的图片设置在pictureBox1
中。尝试制作一个新的pictureBox来覆盖旧的pictureBox:
pictureBox1
=图片本身
pictureBox2
=位图
祝你好运!