Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Tested Code for Store and Retrieve Images using MySQL and VB.NET</p> <pre class="lang-vb prettyprint-override"><code>Public Class FMImage Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If OpenFileDialog1.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then TextBox1.Text = OpenFileDialog1.FileName Dim filename As String = TextBox1.Text Dim FileSize As UInt32 Dim Conn As MySql.Data.MySqlClient.MySqlConnection Conn = New MySql.Data.MySqlClient.MySqlConnection Try If Conn.State = ConnectionState.Open Then Conn.Close() Conn.ConnectionString = MySQLConnectionString Conn.Open() Catch ex As Exception MessageBox.Show(ex.ToString, "Connect") End Try Dim mstream As System.IO.MemoryStream = ConvertImageFiletoMemoryStream(filename) PbPicture.Image.Save(mstream, Drawing.Imaging.ImageFormat.Jpeg) Dim arrImage() As Byte = ConvertImageFiletoBytes(filename) FileSize = mstream.Length Dim sqlcmd As New MySql.Data.MySqlClient.MySqlCommand Dim sql As String mstream.Close() 'CREATE TABLE `actors` ( `actor_pic` longblob,`filesize` bigint(20) default NULL,`filename` varchar(150) default NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1; sql = "insert into actors (actor_pic, filesize, filename) VALUES(@File, @FileName, @FileSize)" Try With sqlcmd .CommandText = sql .Connection = Conn .Parameters.AddWithValue("@FileName", filename) .Parameters.AddWithValue("@FileSize", FileSize) .Parameters.AddWithValue("@File", arrImage) .ExecuteNonQuery() End With Catch ex As Exception MsgBox(ex.Message) End Try Dim adapter As New MySql.Data.MySqlClient.MySqlDataAdapter adapter.SelectCommand = New MySql.Data.MySqlClient.MySqlCommand("SELECT actor_pic, filesize, filename FROM actors", Conn) Dim Data As New DataTable 'adapter = New MySql.Data.MySqlClient.MySqlDataAdapter("select picture from [yourtable]", Conn) Dim commandbuild As New MySql.Data.MySqlClient.MySqlCommandBuilder(adapter) adapter.Fill(Data) MsgBox(Data.Rows.Count) Dim lb() As Byte = Data.Rows(Data.Rows.Count - 1).Item("actor_pic") Dim lstr As New System.IO.MemoryStream(lb) PbPicture.Image = Image.FromStream(lstr) PbPicture.SizeMode = PictureBoxSizeMode.StretchImage lstr.Close() End If End Sub Public Function ConvertImageFiletoBytes(ByVal ImageFilePath As String) As Byte() Dim _tempByte() As Byte = Nothing If String.IsNullOrEmpty(ImageFilePath) = True Then Throw New ArgumentNullException("Image File Name Cannot be Null or Empty", "ImageFilePath") Return Nothing End If Try Dim _fileInfo As New IO.FileInfo(ImageFilePath) Dim _NumBytes As Long = _fileInfo.Length Dim _FStream As New IO.FileStream(ImageFilePath, IO.FileMode.Open, IO.FileAccess.Read) Dim _BinaryReader As New IO.BinaryReader(_FStream) _tempByte = _BinaryReader.ReadBytes(Convert.ToInt32(_NumBytes)) _fileInfo = Nothing _NumBytes = 0 _FStream.Close() _FStream.Dispose() _BinaryReader.Close() Return _tempByte Catch ex As Exception Return Nothing End Try End Function Public Function ConvertBytesToMemoryStream(ByVal ImageData As Byte()) As IO.MemoryStream Try If IsNothing(ImageData) = True Then Return Nothing 'Throw New ArgumentNullException("Image Binary Data Cannot be Null or Empty", "ImageData") End If Return New System.IO.MemoryStream(ImageData) Catch ex As Exception Return Nothing End Try End Function Public Function ConvertImageFiletoMemoryStream(ByVal ImageFilePath As String) As IO.MemoryStream If String.IsNullOrEmpty(ImageFilePath) = True Then Return Nothing ' Throw New ArgumentNullException("Image File Name Cannot be Null or Empty", "ImageFilePath") End If Return ConvertBytesToMemoryStream(ConvertImageFiletoBytes(ImageFilePath)) End Function End Class </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload