1 Replies - 906 Views - Last Post: 12 December 2014 - 05:38 PM

#1 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3824
  • View blog
  • Posts: 13,556
  • Joined: 12-December 12

Save and load image from OLE Object field in MS Access

Posted 04 July 2014 - 09:51 AM

This code uses a PictureBox to display the image, it could be another control-type.

The image location is hard-coded, but an OpenFileDialog could be used.
Imports System.Data.OleDb

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" & _
                                              "Data Source=C:\Users\Andrew\Documents\Pictures.accdb;" & _
                                              "Persist Security Info=False;")
        Dim cmd As New OleDbCommand("INSERT INTO tblPictures (Picture, Description) VALUES (?, 'testing')", conn)

        Using picture As Image = Image.FromFile("C:\Users\Andrew\Pictures\coyote.jpg")
            Using stream As New IO.MemoryStream
                picture.Save(stream, Imaging.ImageFormat.Jpeg)
                cmd.Parameters.Add("Picture", OleDbType.VarBinary).Value = stream.GetBuffer()
            End Using
        End Using

        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" & _
                                              "Data Source=C:\Users\Andrew\Documents\Pictures.accdb;" & _
                                              "Persist Security Info=False;")
        Dim cmd As New OleDbCommand("SELECT Picture FROM tblPictures WHERE Description='testing'", conn)

        conn.Open()

        Dim pictureData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())

        conn.Close()

        Dim picture As Image = Nothing

        'Create a stream in memory containing the bytes that comprise the image.
        Using stream As New IO.MemoryStream(pictureData)
            'Read the stream and create an Image object from the data.
            picture = Image.FromStream(stream)
            PictureBox1.Image = picture
        End Using
    End Sub
End Class


This uses ExecuteScalar() because I am only retrieving a single picture (and a single field).

This post has been edited by andrewsw: 04 July 2014 - 01:54 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Save and load image from OLE Object field in MS Access

#2 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3824
  • View blog
  • Posts: 13,556
  • Joined: 12-December 12

Re: Save and load image from OLE Object field in MS Access

Posted 12 December 2014 - 05:38 PM

Running the above code through this C# converter produces this code:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Data.OleDb;

public class Form1
{

	private void Button1_Click(object sender, EventArgs e)
	{
		OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=C:\\Users\\Andrew\\Documents\\Pictures.accdb;" + "Persist Security Info=False;");
		OleDbCommand cmd = new OleDbCommand("INSERT INTO tblPictures (Picture, Description) VALUES (?, 'testing')", conn);

		using (Image picture = Image.FromFile("C:\\Users\\Andrew\\Pictures\\coyote.jpg")) {
			using (System.IO.MemoryStream stream = new System.IO.MemoryStream()) {
				picture.Save(stream, Imaging.ImageFormat.Jpeg);
				cmd.Parameters.Add("Picture", OleDbType.VarBinary).Value = stream.GetBuffer();
			}
		}

		conn.Open();
		cmd.ExecuteNonQuery();
		conn.Close();

	}

	private void Button2_Click(object sender, EventArgs e)
	{
		OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=C:\\Users\\Andrew\\Documents\\Pictures.accdb;" + "Persist Security Info=False;");
		OleDbCommand cmd = new OleDbCommand("SELECT Picture FROM tblPictures WHERE Description='testing'", conn);

		conn.Open();

		byte[] pictureData = (byte[])cmd.ExecuteScalar();

		conn.Close();

		Image picture = null;

		//Create a stream in memory containing the bytes that comprise the image.
		using (System.IO.MemoryStream stream = new System.IO.MemoryStream(pictureData)) {
			//Read the stream and create an Image object from the data.
			picture = Image.FromStream(stream);
			PictureBox1.Image = picture;
		}
	}
}

Quote

“Debugging is anticipated with distaste, performed with reluctance, and bragged about forever.” - Dan Kaminsky

This post has been edited by andrewsw: 12 December 2014 - 05:43 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1