在加载在PictureBox的图像,并绘制一个矩形

问题描述:

我写这个代码:在加载在PictureBox的图像,并绘制一个矩形

private void ResizeImage() 
{ 
    SetImage(Image); 
} 

private void SetPen() 
{ 
    CreatePen(10,10,20,60); 
} 

private void CreatePen(int x, int y, int width, int height) 
{ 
    Rectangle = new Rectangle(x, y, width, height); 
    Pen = new Pen(Color.Crimson, 1); 
    (Image.CreateGraphics()).DrawRectangle(Pen, Rectangle); 
    Invalidate(); 
} 

的问题是,该方法Load()取代图像为矩形。我正在做图像裁剪,在那里用户不能创建新的选择。程序自己创建选择,用户只能移动它。

在图片框的Paint事件绘制您的矩形:

void PictureBox_Paint(object sender, PaintEventArgs e) 
{ 
    Rectangle = new Rectangle(x, y, width, height); 
    Pen = new Pen(Color.Crimson, 1); 
    e.Graphics.DrawRectangle(Pen, Rectangle); 
}