Highlighted
Absent Member.
Absent Member.
3489 views

Save image to sql.

Jump to solution

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? 

0 Likes
2 Solutions

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Save image to sql.

Jump to solution

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.

View solution in original post

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Save image to sql.

Jump to solution

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.

View solution in original post

0 Likes
8 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Save image to sql.

Jump to solution

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
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Save image to sql.

Jump to solution

Thanks Chris Glazier very helpfull as always !

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Save image to sql.

Jump to solution

im getting

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

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Save image to sql.

Jump to solution

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

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Save image to sql.

Jump to solution

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.

View solution in original post

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Save image to sql.

Jump to solution

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

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Save image to sql.

Jump to solution

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.

View solution in original post

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Save image to sql.

Jump to solution

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.

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.