Loading an image in the PictureBox and draw a rectangle
-
30-03-2021 - |
سؤال
I wrote this code:
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();
}
The problem is that the method Load() replaces image to rectangle. I'm doing image Cropper, where the user can not create new selection. The program creates the selection itself, the user can only move it.
المحلول
In Paint event of PictureBox draw your rectangle:
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);
}
لا تنتمي إلى StackOverflow