Saving image in picbox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vicky87.eie@gmail.com

    Saving image in picbox

    I used a picture box to draw lines and rectangle using its graphics
    object in paint event. Now i need to save those lines i have drawn and
    print them. I need to know how to save them. I tried image.save. But i
    didn't work...
  • =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?=

    #2
    RE: Saving image in picbox

    Hi Vicky,

    It is a common mistake that the PictureBox is meant for drawing, which it
    isn't. It is meant to display the image inserted into the Image property, so
    Image.Save would only save an existing image.

    Anyhow, to save what you draw in the Paint event, extract the drawing code
    to a separate method taking the Graphics object from PaintEventArgs as a
    parameter.
    When you want to save the drawing, simply create a Graphics object from a
    blank Bitmap object and call the drawing method with this Graphics object as
    a parameter.

    Something like:

    OnPaint(PaintEv entArgs e)
    {
    Draw(e.Graphics );
    }

    SaveImage()
    {
    Bitmap bmp = new Bitmap(100, 100);
    using(Graphics g = Graphics.FromIm age(bmp))
    {
    Draw(g);
    }
    bmp.Save(filena me);
    }

    --
    Happy Coding!
    Morten Wennevik [C# MVP]


    "vicky87.eie@gm ail.com" wrote:
    I used a picture box to draw lines and rectangle using its graphics
    object in paint event. Now i need to save those lines i have drawn and
    print them. I need to know how to save them. I tried image.save. But i
    didn't work...
    >

    Comment

    • vicky87.eie@gmail.com

      #3
      Re: Saving image in picbox

      I tried as you said. It created a bmp file. But when i open it there
      was no image. It was just an empty bmp file :( When i tried to open it
      with paint it said format not supported :(

      this is my code


      public void drawwire(Graphi cs gr)
      {
      Pen p1 = new Pen(System.Draw ing.Color.Black , 6);
      Color customColor = Color.Black;
      SolidBrush b1 = new SolidBrush(cust omColor);
      Point begin = new Point(), end = new Point();
      for (int count1 = 1; count1 <= wire_cont; count1++)
      {
      if (modify.item == 1)
      {
      p1.Color = Color.Black;
      b1.Color = Color.Black;

      if (modify.no == count1)
      {
      p1.Color = Color.Red;
      b1.Color = Color.Red;
      }
      }
      if (wire[count1] != null)
      {
      begin = wire[count1].getBeginning() ;
      end = wire[count1].getEnding();
      gr.FillRectangl e(b1, begin.X - 4, begin.Y - 4, 8,
      8);
      gr.FillRectangl e(b1, end.X - 4, end.Y - 4, 8, 8);
      gr.DrawLine(p1, begin, end);
      }
      }
      }


      private void toolStripButton 2_Click(object sender, EventArgs e)
      {
      Bitmap print = new Bitmap(100, 100);
      using (Graphics pri = Graphics.FromIm age(print))
      {
      drawwire(pri);
      }
      print.Save("E:\ \print.bmp");
      }

      Comment

      • =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?=

        #4
        Re: Saving image in picbox

        Hi Vicky,

        Your code is correct as far as I can see, although I cannot reproduce your
        code as the modify and wire data is unknown. The error would indicate you
        are not producing a regular bitmap so if you are doing additional stuff to
        the bitmap I would check that code.

        Below is some reference code that should produce a 100x100 bitmap with a red
        80x80 rectangle in the middle. This rectangle is also painted in a Panel
        control using the exact same drawing code.

        public Form1()
        {
        InitializeCompo nent();

        panel1.Paint += new PaintEventHandl er(panel1_Paint );
        }

        void panel1_Paint(ob ject sender, PaintEventArgs e)
        {
        drawwire(e.Grap hics);
        }

        public void drawwire(Graphi cs gr)
        {
        gr.FillRectangl e(Brushes.Red, 10, 10, 80, 80);
        }

        private void button1_Click(o bject sender, EventArgs e)
        {
        Bitmap print = new Bitmap(100, 100);
        using (Graphics pri = Graphics.FromIm age(print))
        {
        drawwire(pri);
        }
        print.Save("C:\ \print.bmp");
        }

        --
        Happy Coding!
        Morten Wennevik [C# MVP]


        "vicky87.eie@gm ail.com" wrote:
        I tried as you said. It created a bmp file. But when i open it there
        was no image. It was just an empty bmp file :( When i tried to open it
        with paint it said format not supported :(
        >
        this is my code
        >
        >
        public void drawwire(Graphi cs gr)
        {
        Pen p1 = new Pen(System.Draw ing.Color.Black , 6);
        Color customColor = Color.Black;
        SolidBrush b1 = new SolidBrush(cust omColor);
        Point begin = new Point(), end = new Point();
        for (int count1 = 1; count1 <= wire_cont; count1++)
        {
        if (modify.item == 1)
        {
        p1.Color = Color.Black;
        b1.Color = Color.Black;
        >
        if (modify.no == count1)
        {
        p1.Color = Color.Red;
        b1.Color = Color.Red;
        }
        }
        if (wire[count1] != null)
        {
        begin = wire[count1].getBeginning() ;
        end = wire[count1].getEnding();
        gr.FillRectangl e(b1, begin.X - 4, begin.Y - 4, 8,
        8);
        gr.FillRectangl e(b1, end.X - 4, end.Y - 4, 8, 8);
        gr.DrawLine(p1, begin, end);
        }
        }
        }
        >
        >
        private void toolStripButton 2_Click(object sender, EventArgs e)
        {
        Bitmap print = new Bitmap(100, 100);
        using (Graphics pri = Graphics.FromIm age(print))
        {
        drawwire(pri);
        }
        print.Save("E:\ \print.bmp");
        }
        >

        Comment

        • vicky87.eie@gmail.com

          #5
          Re: Saving image in picbox

          Hey it worked man :) :) :) Thanks a lot :) Do you know how to print
          that picture? I'm using inkjet printer?????

          Comment

          • vicky87.eie@gmail.com

            #6
            Re: Saving image in picbox

            I tried to open the BMP file using MSPAINT. But it's giving error that
            "Paint cannot read this file. This is not a valid BMP file or this
            format is not currently supported". What to do :(

            Comment

            • vicky87.eie@gmail.com

              #7
              Re: Saving image in picbox

              You told me that this one will create a bmp file.
              print.Save("C:\ \print.bmp",
              ImageFormat.Bmp );

              Ya it created one. But the content in pic box was nt
              found. It just had a black background any nothig else :( :(

              Comment

              • vicky87.eie@gmail.com

                #8
                Re: Saving image in picbox

                The printing worked :) thanks ya...

                Comment

                • Vicky

                  #9
                  Re: Saving image in picbox

                  Wennevik i did as you told. But the resulting bitmap had only a black
                  background. Tell me how to overcome it ya

                  Comment

                  Working...