locked
How to save pictureBox image to file RRS feed

  • Question

  • Hi
    I want to use the following code, it normally build but in run time has error:

    pictureBox2->Image->Save("d:\\a.jpg",System::Drawing::Imaging::ImageFormat::Jpeg);

    the error is:

    An unhandled exception of type 'System.NullReferenceException' occurred in System.Drawing.dll
    
    Additional information: Object reference not set to an instance of an object.

    what's the problem.
    Monday, November 16, 2009 9:01 AM

Answers

  • Try another way:

     

    #include <msclr\auto_handle.h>

    . . . .

     

    using namespace msclr;

     

    Bitmap ^ image = gcnew Bitmap(100, 100, System::Drawing::Imaging::PixelFormat::Format24bppRgb);

    System::Drawing::SolidBrush black(Color::Black); // (or use Brushes predefined members)

    System::Drawing::SolidBrush white(Color::White);

    System::Drawing::Font font("Times New Roman", 12);

    auto_handle<Graphics> g = Graphics::FromImage(image);

     

    g->FillRectangle(%white, Rectangle(0, 0, 100, 100));

    g->DrawString("Text", %font, %black, PointF(10, 10));

     

    image->Save("D:\\a.jpg", System::Drawing::Imaging::ImageFormat::Jpeg);

     

    this->pictureBox2->Image = image;

     

    For prompting the user, consider the FileDialog class [http://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.aspx]. This is probably a subject for separate discussion.

     

     

    • Marked as answer by Engineer Mahdi Monday, November 16, 2009 12:10 PM
    Monday, November 16, 2009 11:54 AM

All replies

  • The error implies you're calling Image::Save when the PictureBox doesn't have an image. In what method/event handler are you calling Image::Save?
    Monday, November 16, 2009 9:16 AM
  • Hi ildjarn

    I use following code to capture from string and show in the picturebox:

    			System::Drawing::SolidBrush ^brush=gcnew System::Drawing::SolidBrush(Color::Black);
    			float nLen=0.0f;
    			System::Drawing::Font ^font=gcnew System::Drawing::Font("B lotus", 12);
    			this->pictureBox2->CreateGraphics()->DrawString(this->textBox3->Text,font,brush,nLen,0);
    			pictureBox2->Image->Save("d:\\a.jpg",System::Drawing::Imaging::ImageFormat::Jpeg);
    Monday, November 16, 2009 9:24 AM
  • I think that you first should create an empty image and draw on this image. Then you will be able to save it.

     

    An image can be created like this:

     

        this->pictureBox2->Image = gcnew Bitmap(100, 100, System::Drawing::Imaging::PixelFormat::Format24bppRgb);

     

    The graphics object should be got like this:

     

        Graphics ^ g = Graphics::FromImage(this->pictureBox2->Image);

     

    You have to draw the background first (e.g. g->FillRectangle). Then you should be able to save it since now it is not null.

     

    If you do not need to display the image, then you do not have to assign it to a picture control; you can work directly with an image object declared like a local variable.

    Monday, November 16, 2009 9:41 AM
  • thanks for your reply Viorel


    I write the following code:


    			System::Drawing::SolidBrush ^brush=gcnew System::Drawing::SolidBrush(Color::Black);
    			float nLen=0.0f;
    			System::Drawing::Font ^font=gcnew System::Drawing::Font("B lotus", 12);
    			this->pictureBox2->CreateGraphics()->DrawString(this->textBox3->Text,font,brush,nLen,0);
    			this->pictureBox2->Image = gcnew Bitmap(pictureBox2->Width, pictureBox2->Height);
    			
    			Graphics ^ g =Graphics::FromImage(this->pictureBox2->Image);
    			g->Save();
     

    but this code has two drawbacks:
    1. the pictureBox fills with a black rectangle and don't show my string.
    2. g->save(); does not ask for an image path and name.

    Can you please help me further?

    thanks
    Monday, November 16, 2009 10:10 AM
  • Try another way:

     

    #include <msclr\auto_handle.h>

    . . . .

     

    using namespace msclr;

     

    Bitmap ^ image = gcnew Bitmap(100, 100, System::Drawing::Imaging::PixelFormat::Format24bppRgb);

    System::Drawing::SolidBrush black(Color::Black); // (or use Brushes predefined members)

    System::Drawing::SolidBrush white(Color::White);

    System::Drawing::Font font("Times New Roman", 12);

    auto_handle<Graphics> g = Graphics::FromImage(image);

     

    g->FillRectangle(%white, Rectangle(0, 0, 100, 100));

    g->DrawString("Text", %font, %black, PointF(10, 10));

     

    image->Save("D:\\a.jpg", System::Drawing::Imaging::ImageFormat::Jpeg);

     

    this->pictureBox2->Image = image;

     

    For prompting the user, consider the FileDialog class [http://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.aspx]. This is probably a subject for separate discussion.

     

     

    • Marked as answer by Engineer Mahdi Monday, November 16, 2009 12:10 PM
    Monday, November 16, 2009 11:54 AM
  • Thanks to you Viorel_
    It works well
    Monday, November 16, 2009 12:10 PM