0

I am trying to open a .jpg in Windows Form Application, draw a rectangle on it and save it with the rectangle. In the following code I can load an .jpg and draw a rectangle to the pictureBox, but cannot save the .jpg with the rectangle. Someone knows the problem?

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace EditScreen
    {
    public partial class Form1 : Form
    {

    Rectangle mRect;
    Image mainimage;
    Bitmap newBitmap;
    Graphics g;
    Boolean opened = false;
    OpenFileDialog ofd = new OpenFileDialog();
    SaveFileDialog sfd = new SaveFileDialog();
    public Form1()
    {
        InitializeComponent();
        this.DoubleBuffered = true;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        DialogResult dr = ofd.ShowDialog();
        if (dr == DialogResult.OK)
        {
            mainimage = Image.FromFile(ofd.FileName);
            newBitmap = new Bitmap(ofd.FileName);
            pictureBox1.Image = mainimage;
            opened = true;  
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        DialogResult dr = sfd.ShowDialog();
        if (opened)
        {
           mainimage.Save(sfd.FileName);
        }
    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        mRect = new Rectangle(e.X, e.Y, 0, 0);
        pictureBox1.Invalidate();
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
    mRect = new Rectangle(mRect.Left, mRect.Top, e.X - mRect.Left, e.Y - mRect.Top);                               
            pictureBox1.Invalidate();
        }
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        using (Pen pen = new Pen(Color.Red, 2))
        {
            Graphics g = e.Graphics;
            g.DrawRectangle(pen, mRect);
        }
    }
}
}

I heard that I have to paint on the bitmap but when I do this I dont know how to use the PaintEventArgs. Can you give me a example for my code to save it with the drawings?

Saverio Terracciano
  • 3,885
  • 1
  • 30
  • 42
  • [this will help](http://stackoverflow.com/questions/11402862/how-to-draw-a-line-on-a-image), it's for drawing a line but the key concept of getting the `Graphics` object is what you need. You can then use the [Image.Save](http://msdn.microsoft.com/en-us/library/9t4syfhh(v=vs.110).aspx) method – musefan Mar 05 '14 at 10:06
  • Hello, thank you for the fast answer. But if i do it like in this example you given, I cannot draw with my mouse, how can I do it with the mouse? In the example it makes the rectangle directly after start the application – TheClassyGuy Mar 05 '14 at 10:16
  • The example I have given you is what you should use in your "Save()" function. You should still keep your paint code so it draws while the user interacts – musefan Mar 05 '14 at 10:22
  • When I do that save like your example, It just save the loaded picture without the Graphics, can you make a code snippet to show me? – TheClassyGuy Mar 05 '14 at 10:51
  • When I do it in the example that is given, it works, but in that example I cannot draw with the mouse. – TheClassyGuy Mar 05 '14 at 10:59
  • You existing code draws with the mouse correct? So you don't change that bit at all... it already works! You need to create a Save function that is called when the users saves (button click). In this function you load the Image Graphics (like in the link example). You draw your `mRect` object on the image graphics, then you call `Image.Save(...)`, and then that's it. – musefan Mar 05 '14 at 11:12

0 Answers0