Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a picturebox, after loading image on that i'm drwaing some lines and rectangle using drawing objects, also adding some controls at runtime.
but during printing or saving dat image it saves only dat image without any lines or rectangle...
pl help how to save with drawing part

my load image code is like:
C#
private void loadImageToolStripMenuItem_Click(object sender, EventArgs e)
       {
           try
           {
               OpenFileDialog open = new OpenFileDialog();
               open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
               if (open.ShowDialog() == DialogResult.OK)
               {
                   pictureBox1.Image = new Bitmap(open.FileName);
                   trackBar1.Value = pictureBox1.Height;
                   trackBar2.Value = pictureBox1.Height;
               }
           }
           catch (ApplicationException ex)
           {
               MessageBox.Show(ex.Message.ToString());
           }
           trackBar1.Enabled = true;
           trackBar2.Enabled = true;
           pictureBox1.Enabled = true;
           lineToolStripMenuItem.Enabled = false;
       }
//code for creating drawing
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            Graphics g = null;
            g = Graphics.FromImage(pictureBox1.Image);
            g = pictureBox1.CreateGraphics();
            g.DrawLine(Pens.Red, 0, Convert.ToInt32(trackBar1.Maximum) - trackBar1.Value, Convert.ToInt32(pictureBox1.Width), Convert.ToInt32(trackBar1.Maximum) - trackBar1.Value);
            g.DrawLine(Pens.Blue, 0, Convert.ToInt32(trackBar2.Maximum) - trackBar2.Value, Convert.ToInt32(pictureBox1.Width), Convert.ToInt32(trackBar2.Maximum) - trackBar2.Value);
            g.Dispose();
            mouseMovePt = e.Location;
        }
// code for saving image
private void saveImageToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            SaveFileDialog savefile = new SaveFileDialog();
            savefile.DefaultExt = "jpeg";
            savefile.Filter = "All files*.*|*.*";
            if (savefile.ShowDialog() == DialogResult.OK)
            {
                int width = pictureBox1.Width;
                int height = pictureBox1.Height;
                Bitmap bitmap = new Bitmap(width, height);
                Rectangle rc = new Rectangle(0, 0, width, height);
                pictureBox1.DrawToBitmap(bitmap, rc);
                bitmap.Save(savefile.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
        }


thnxxxxxxx
Posted

I wrote an article about how to render text onto a bitmap. You should be able to use the same general technique to draw lines/shapes on an image as well.

Render Text On A Bitmap (and other stuff)[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-Feb-11 11:18am    
Good, my 5.
--SA
Did you try DrawToBitmap function on your PictureBox control? It should do exactly what you want: it saves the content of a control into a bitmap.

Just make sure that all the painting code is inside your paint handler. For example, do not draw in your mouse move handler. You can just keep the points coordinates you need to draw the lines, and draw the lines inside a paint event handler.
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 9-Feb-11 11:17am    
My 5.
Strictly, drawing in mouse event handler can be used the way it makes sense, but novice could be puzzled by the results (you know why).
--SA
sharmarun 16-Mar-11 3:06am    
how to do for web application
Olivier Levrey 16-Mar-11 4:55am    
I have no idea I have never developped any web applications...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900