0

I am trying to draw a rectangle on a win form, and save the selected rectangle as image (bmp or jpeg) on the disk. But I am struck on save part. Currently I can draw rectangle and gives me the rectangle as mRect variable

I have tried hard via google search and various articles but in vain. My current form events code are:

    Point selPoint;
    Rectangle mRect;

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            selPoint = e.Location;
        }


        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Point p = e.Location;
                int x = Math.Min(selPoint.X, p.X);
                int y = Math.Min(selPoint.Y, p.Y);
                int w = Math.Abs(p.X - selPoint.X);
                int h = Math.Abs(p.Y - selPoint.Y);
                mRect = new Rectangle(x, y, w, h);
                this.Invalidate();
            }
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawRectangle(Pens.Blue, mRect);
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
          // ??? 
        }
Usman Waheed
  • 86
  • 1
  • 8
  • related: https://stackoverflow.com/questions/12909905/saving-image-to-file – Daniel A. White Nov 26 '21 at 15:21
  • What do you want to save: a plain recangle or an area from the form? Will it contain stuff you draw or controls or both? - The usual way to get form or control content to a bitmap is DrawToBitmap. Look it up. There are a lot of examples around. Onc you have it bmp.Save(..) will do, maybe with png as file format to keep it crips.. – TaW Nov 26 '21 at 15:56
  • I need to draw rectangle any where on the form and thus get that cropped image to be save in any common image file formats – Usman Waheed Nov 26 '21 at 16:42
  • @DanielA.White No sir, thats doing something different. I need to draw a rectangle on form with mouse, that is already being done as per my code. Then I need to save that selected area as jpeg or bmp etc. – Usman Waheed Nov 26 '21 at 17:20
  • @TaW I just need to save the triangle drawn on the form what ever comes inside that rectangle, should be save as a picture file. `g.CopyFromScreen( )` is so far helping me out. But its not the exact rectangle drawn, its skewed and I cannot figure out how to fix it. – Usman Waheed Nov 26 '21 at 17:23
  • DrawToBitmap is the most direct way then. But CopyFromScreen will work as well. It won't be 'skewed' but may not hit the correct location, depending on your code.. Note that the triangle needs (!!) to be drawn in a Paint event! – TaW Nov 26 '21 at 18:26
  • @TaW Thank you for your help mate, but I think DrawToBitmap needs a Control class, I want a generic solution i.e. any where on the winform – Usman Waheed Nov 26 '21 at 18:38
  • 1
    No, it can be called from any control or from the form. – TaW Nov 26 '21 at 18:45

1 Answers1

1

Use RectangleToScreen() with your Form to convert your selection rectangle from client coords to screen coords:

Rectangle screenRC = this.RectangleToScreen(mRect);
Bitmap bmp = new Bitmap(screenRC.Width, screenRC.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
    g.CopyFromScreen(screenRC.Left, screenRC.Top, 0, 0, bmp.Size);
}
bmp.Save("a1.bmp");
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40