Save image to sql.

Hello, lately I have been triying to create a code to upload an image into my sql database from visual cobol 2012. I have done this in C# with 

byte[] image
object o = Clipboard.GetDataObject().GetData("Bitmap");
if (o != null)
{
pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox.Image = (Image)o;
}

using (MemoryStream ms = new MemoryStream())
{
pictureBox.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
image = ms.ToArray();
}

Im not sure how I can do " pictureBox.Image = (Image)o;"  in visual cobol, since im stuck there I haven't tried to code the rest but I don't think that would be any problem.

How can I manage to do this task in visual cobol? 

  • The following code should work:

       declare image as type Byte occurs any
       declare o as object = type Clipboard::GetDataObject()::GetData("Bitmap");
       if o not = null
          set pictureBox1::SizeMode to type PictureBoxSizeMode::StretchImage
          set pictureBox1::Image to o as type Image
          perform using ms as type System.IO.MemoryStream = new System.IO.MemoryStream
            invoke pictureBox1::Image::Save(ms, type System.Drawing.Imaging.ImageFormat::Jpeg)
            set image to ms::ToArray
          end-perform
       end-if
    
  • im getting

    Cannot implicitly convert type System.Windows.Controls.Image to type System.Drawing.Image.

  • Doesn't have to be the same way, what would be the best way to achieve saving an image to a sql database?.

  • Verified Answer

    For the previous error you can try changing this line to:

        set pictureBox1::Image to o as type System.Drawing.Image

    What database are you using?

    Open up the Samples Browser under the Visual COBOL start menu and click on SQL on the left had side and then select and open up the managed LOB Data Types example. This shows how you can store an image field in a SQL database using OpenESQL.

  • Tried it and gives an error. But the sample should help me a lot ! Ill study the sample. thanks

  • Verified Answer

    I have used the attached method with succes a while ago (1 Gb of pictures)

    The ReadAllBytes does the read (very fast !!)

    Succes,

    Jan Vink

    -------------------------------------------------------------------------------------------------------------------

          01 inpmap-record   pic x(250) value 'C:\Pictures\image9.jpg'.

          01 input-document-data type System.Byte[].

            method-id  get-a-foto

          *>==================================

          *> Get complete image for insert

          *> inpmap-record contains the complete path\filename if the image

          *>==================================

               invoke type System.IO.File::ReadAllBytes(inpmap-record) RETURNING input-document-data.

          *> Test for errors with Try / Catch

          *> Now you can insert the image of the file (or word doc etc..) into the database

          *> Using MS-SQL-server insert statement to insert the image

          *>

          *> where the database field 'foto' is defined as :'[image] NULL,'

             set foto to input-document-data.

          *> Insert into statement

            end method.

  • I managed to save the image into SQL using this code.

                  set pbImage::Image to type System.Windows.Forms.Clipboard::GetImage()

                  set pbImage::SizeMode to type System.Windows.Forms.PictureBoxSizeMode::Zoom

                  invoke pbImage::Image::Save("temp-img", type System.Drawing.Imaging.ImageFormat::Jpeg)

                  set fil to type System.IO.FileStream::New("temp-img", type System.IO.FileMode::Open)

                  set size of aBytes to fil::Length

                  invoke fil::Read(aBytes, 0, aBytes::Count())

                  invoke sql-db::itm-img-to-sql(txtTest::Text, aBytes)

    The image i want to upload doesnt come from a file but from a picturebox. Since the sample only shows how to upload from image I save the image to a temporary file then upload from file.