Click here to Skip to main content
15,889,714 members
Home / Discussions / C#
   

C#

 
QuestionCombobox SelectedValue Problem. Pin
Dotnetkanna17-May-10 23:30
Dotnetkanna17-May-10 23:30 
AnswerRe: Combobox SelectedValue Problem. Pin
Abhinav S18-May-10 0:34
Abhinav S18-May-10 0:34 
AnswerRe: Combobox SelectedValue Problem. Pin
Seishin#18-May-10 4:46
Seishin#18-May-10 4:46 
QuestionHow can save picturebox image using menustrip Pin
Nivas8217-May-10 21:00
Nivas8217-May-10 21:00 
AnswerRe: How can save picturebox image using menustrip Pin
William Winner18-May-10 8:00
William Winner18-May-10 8:00 
GeneralRe: How can save picturebox image using menustrip Pin
Nivas8218-May-10 19:18
Nivas8218-May-10 19:18 
GeneralRe: How can save picturebox image using menustrip Pin
William Winner19-May-10 5:29
William Winner19-May-10 5:29 
GeneralRe: How can save picturebox image using menustrip Pin
Nivas8219-May-10 23:13
Nivas8219-May-10 23:13 
I have rectified the problem. Problem lies in if condition in the save tooltip. if i remove the if condition and save the file it's works.

new code
Image bac;
Bitmap myBitmap;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
            if (mybitmap == null)
            {
                mybitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height, pictureBox1.CreateGraphics());

            }
               
            rect = new Rectangle(e.X, e.Y, 0, 0);
             this.Invalidate();
                
}


private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{

             
               switch (e.Button)
      {
                case MouseButtons.Left:
           {
                    rect = new Rectangle(rect.Left, rect.Top, e.X - rect.Left, e.Y - rect.Top);
                    this.Invalidate();
         
                break;
            }

       }
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{

            if (mybitmap == null)
            {
                return;
            }
            using (Pen pen = new Pen(Color.Red, 2))
            {
                using (Graphics g = Graphics.FromImage(mybitmap))
                {
                e.Graphics.DrawRectangle(pen, rect);
                    
                 if (label1.TextAlign == ContentAlignment.TopLeft)
                    {
                        e.Graphics.DrawString(label1.Text, label1.Font, new SolidBrush(label1.ForeColor), label1.Bounds); 
                        g.DrawString(label1.Text, label1.Font, new SolidBrush(label1.ForeColor), label1.Bounds);
mybitmap.PixelFormat);
                    }
                    else if (label1.TextAlign == ContentAlignment.TopCenter)
                    {
                        SizeF size = e.Graphics.MeasureString(label1.Text, label1.Font);
                        float left = ((float)this.Width + label1.Left) / 2 - size.Width / 2;
                        RectangleF rect1 = new RectangleF(left, (float)label1.Top, size.Width, label1.Height);
                        e.Graphics.DrawString(label1.Text, label1.Font, new SolidBrush(label1.ForeColor), rect1);

                    }
                    else
                    {
                        SizeF size = e.Graphics.MeasureString(label1.Text, label1.Font);
                        float left = (float)label1.Width - size.Width + label1.Left;
                        RectangleF rect1 = new RectangleF(left, (float)label1.Top, size.Width, label1.Height);
                        e.Graphics.DrawString(label1.Text, label1.Font, new SolidBrush(label1.ForeColor), rect1);
                    }

               }

        }
            
}

private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{


            if (mybitmap != null)
            {
                SaveFileDialog SaveFD1 = new SaveFileDialog();
                SaveFD1.FileName = "";
                SaveFD1.InitialDirectory = "C";
                SaveFD1.Title = "save file Name";
                SaveFD1.Filter = "JPG|*.jpg|Bmp|*.bmp";
                 if (SaveFD1.ShowDialog() == DialogResult.OK)
                {
                    
                    System.IO.Stream filename = (System.IO.FileStream)SaveFD1.OpenFile();

                    int r = SaveFD1.FileName.Length;
                    for (int r1 = 0; r1<=r;)
                    {
                        if (SaveFD1.FileName[r1] != '.')
                            r1++;
                        else
                        {
                            r = r1;
                            break;
                        }
                    }
                    
                       if (SaveFD1.FileName[++r] == 'j')
                        {
                            using (Graphics g = Graphics.FromImage(bac))
                        {
                           g.DrawImage(mybitmap, 0, 0);
                        }
                        bac.Save(filename, ImageFormat.Jpeg);
                        }
                        else if (SaveFD1.FileName[r] == 'b')
                        {
                            using (Graphics g = Graphics.FromImage(bac))
                            {
                                g.DrawImage(mybitmap, 0, 0);
                            }
                            bac.Save(filename, ImageFormat.Jpeg);
                        }
                        else
                        {
                            using (Graphics g = Graphics.FromImage(bac))
                            {
                                g.DrawImage(mybitmap, 0, 0);
                            }
                            bac.Save(filename, ImageFormat.Png);
                        }
                    
                    filename.Close();

                }
            }
        }

private void openToolStripMenuItem_Click(object sender, EventArgs e)
{

            OpenFD.FileName = "";
            OpenFD.Title = "open image";
            OpenFD.InitialDirectory = "C";
            OpenFD.Filter = "JPEG|*.jpg|Bmp|*.bmp|All Files|*.*.*";
            if (OpenFD.ShowDialog() == DialogResult.OK)
            {
                file = OpenFD.FileName;
                
                bac = Image.FromFile(file);
                pictureBox1.Image = bac;
                pictureBox1.Invalidate();
                

            }

        }


Now the above code works as expected.

Now can i clear the previous work done.

Ex: I have drawn the rectangle but i don't want this so i am drawing new rectangle (note: the above code will make invisible the previous rectangle when new rectangle been drawn) and saved the file. when i open the saved file its show all the rectangle drawn.

How can i clear the previous work (drawn)?

Thanks
GeneralRe: How can save picturebox image using menustrip [modified] Pin
Nivas8220-May-10 20:54
Nivas8220-May-10 20:54 
QuestionWindows service Pin
Heinzzy17-May-10 18:47
Heinzzy17-May-10 18:47 
AnswerRe: Windows service Pin
N a v a n e e t h17-May-10 18:59
N a v a n e e t h17-May-10 18:59 
GeneralRe: Windows service Pin
Heinzzy17-May-10 19:46
Heinzzy17-May-10 19:46 
GeneralRe: Windows service Pin
Calla17-May-10 19:58
Calla17-May-10 19:58 
GeneralRe: Windows service Pin
Heinzzy17-May-10 20:26
Heinzzy17-May-10 20:26 
GeneralRe: Windows service Pin
Dave Kreskowiak18-May-10 5:18
mveDave Kreskowiak18-May-10 5:18 
AnswerRe: Windows service Pin
#realJSOP17-May-10 23:16
mve#realJSOP17-May-10 23:16 
AnswerRe: Windows service Pin
PIEBALDconsult18-May-10 4:47
mvePIEBALDconsult18-May-10 4:47 
AnswerRe: Windows service Pin
Heinzzy18-May-10 8:18
Heinzzy18-May-10 8:18 
Questionpartition clone with c# Pin
muteb17-May-10 17:13
muteb17-May-10 17:13 
AnswerRe: partition clone with c# Pin
Not Active17-May-10 18:07
mentorNot Active17-May-10 18:07 
AnswerRe: partition clone with c# Pin
PIEBALDconsult17-May-10 18:13
mvePIEBALDconsult17-May-10 18:13 
GeneralRe: partition clone with c# Pin
muteb17-May-10 19:10
muteb17-May-10 19:10 
GeneralRe: partition clone with c# Pin
Calla17-May-10 19:44
Calla17-May-10 19:44 
GeneralRe: partition clone with c# Pin
PIEBALDconsult18-May-10 4:49
mvePIEBALDconsult18-May-10 4:49 
AnswerRe: partition clone with c# Pin
Md. Marufuzzaman17-May-10 19:51
professionalMd. Marufuzzaman17-May-10 19:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.