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

C#

 
AnswerRe: Plz, des'nt File Delete ,, Pin
Seishin#18-May-10 4:39
Seishin#18-May-10 4:39 
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 
You're not understanding what you are actually doing and how that isn't what you are trying to do.

First, let me try to explain what your Paint method is doing. When you use the Graphics provided by the Paint method and then draw rectangles and other objects over that, none of that gets saved in memory. The only thing that is happening is that it is drawing on the screen. So, when you go to save the image in the PictureBox, none of the user-made rectangles will be saved with it.

What you will need to do is have two different Bitmaps...one for the background image (the image loaded from file) and one for the user-drawn rectangles and such. Then, whenever you paint, first paint the rectangles to the user-drawn bitmap. Then, you can use the graphics provided by the paint to draw it.

Then, when you go to save, you have to draw the user-drawn bitmap over the background image.


Here's some pseudo-code and real code:
C#
Image backgroundImage;
Bitmap userDraw;
Rectangle rect;

private void OpenMenuItem()
{
  //Get Image to open

  backgroundImage = new Image(file);
  pictureBox1.Image = backgroundImage;
  pictureBox1.Invalidate();
}

private void pictureBox_MouseDown()
{
  if (userDraw == null)
  {
    userDraw = new Bitmap(pictureBox1.Width, pictureBox1.Height, pictureBox1.CreateGraphics());
  }

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

private void pictureBox_MouseMove()
{
  if (e.Button == MouseButtons.Left)
  {
    rect = new Rectangle(rect.Left, rect.Top, e.X - rect.Left, e.Y - rect.Top);
    pictureBox1.Invalidate();
  }
}

private void PictureBox_Paint(object sender, PaintEventArgs e)
{
    if (myBitmap == null) { return; }

    using (Pen pen = new Pen(Color.Red, 2))
    {
      using (Graphics g = Graphics.FromImage(myBitmap))
      {
          g.Clear(Color.Transparent);

          g.DrawRectangle(pen, rect);
      }
      e.Graphics.DrawRectangle(pen, rect);
    }
}

private void SaveMenuItem()
{
  if (myBitmap != null)
  {
      using (Graphics g = Graphics.FromImage(BackgroundImage))
      {
          g.DrawImage(myBitmap, 0, 0);
      }

      BackgroundImage.Save("C:\\Temp\\Bitmapsave.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
  }
}


The way that you are doing it, you won't actually get what you're expecting because you're not drawing the rectangles onto any surface that will keep it in memory.

As far as running out of memory, that is something else within your program. It means that you're probably doing other things and not disposing of the memory and therefore are running out. I've tested this and I have no problems when saving an image and then opening it.
GeneralRe: How can save picturebox image using menustrip Pin
Nivas8219-May-10 23:13
Nivas8219-May-10 23:13 
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 

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.