In this tutorial i will teach you about image retrieving and saving using vb.net and MySql. To do this in C# is so simple. Remember that C# and Vb.net are all running in the same framework, only the syntax may vary. 

In this scenario i am using MySql , Visual Studio 2010 and Infragistics. To know more about infragistics follow this link http://www.infragistics.com/ . Infragistics let's you as developer make things so easy and hassle free. I am using Infragistic 10.2 for this. Anyway you can use any infragistic winform all you have to do is to upgrade this project. 

To start this project, I'd created a database 

sample  CREATE TABLE `sample` (                 
          `idno` VARCHAR(15) NOT NULL,          
          `userimg` LONGBLOB,                   
          PRIMARY KEY (`idno`)                  
        ) ENGINE=INNODB DEFAULT CHARSET=latin1  


This simple exercise will be like this :


This would be the first thing to do. As you can see i have a grid with the images on it, upload and save button and 2 picture boxes which will hold the previews upon upload and grid preview. 
Picture
The 2nd phase is this. Upon clicking on the upload button, and opendialog box should appear. 















The open dialog box should be filtered into images only and this is how to do that. 
OpenFileDialog.Filter = "JPEG (*.jpg;*.jpeg)|*.jpg|PNG (*.png)|*.png" . If you want to filter all files, etc, all you have to do is to change the filter string. 



Picture
Saving image into MySql is easy. All you  have to do is to have an long blob datatype for the DB and then convert the image into byte. 

  
















    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            Dim cmd As New MySqlCommand
            Dim SQL As String
            Dim rawData() As Byte
            Dim fs As FileStream

            fs = New FileStream(fullPath, FileMode.Open, FileAccess.Read)
            rawData = New Byte(fs.Length) {}
            fs.Read(rawData, 0, fs.Length)
            fs.Close()
            con.Open()

            SQL = "INSERT INTO user.sample(idno, userimg) VALUES('" & txtID.Text & "', ?bin_data)"

            cmd.Connection = con
            cmd.CommandText = SQL
            'cmd.Parameters.Add("@bin_data", rawData)
            cmd.Parameters.Add(New MySqlParameter("@bin_data", rawData))
            cmd.ExecuteNonQuery()
            MsgBox("Successfully saved!")
            pBox.BackgroundImage = Nothing
            grd.DataSource = previewme()
            con.Close()
        Catch ex As Exception
            con.Close()
            MsgBox(ex)
        End Try
    End Sub



This is where infragistics makes your project easy. In the grid_initializelayout event all you have to do is to set the "image" column into style = image:                        Case "userimg"
                            .Header.Caption = "User Image"
                            .Width = 50
                            .Style = Infragistics.Win.UltraWinGrid.ColumnStyle.Image


On the preview button part, i had set a 'preview' field on my query and set the field column into button in grd_initializelayout. 

//query

        "SELECT 'Preview' as action, idno,userimg FROM user.sample"

//layout
                        Case "action"
                            .Header.Caption = "Action"
                            .Width = 80
                            .Style = Infragistics.Win.UltraWinGrid.ColumnStyle.Button
                            .CellAppearance.TextHAlign = Infragistics.Win.HAlign.Center
                            .CellAppearance.TextVAlign = Infragistics.Win.HAlign.Center
                            .CellAppearance.ForeColor = Color.Blue


Download the whole source code here:

imagesaving.rar
File Size: 118 kb
File Type: rar
Download File

If this exercise helped you, don't forget to share, comment and like :) 

HAPPY CODING :D Cheers!!! 
jeffrey
10/2/2012 06:33:05 pm

nice tutorial sir, tnx

Reply
11/6/2012 08:19:33 am

pagsure jejepoy.. thanks a lot sir jeffrey .... ^_^

Reply
yul
11/7/2012 11:28:58 am

the best tutorial ive ever seen. thanks a lot men!

Reply
saito najumi
2/5/2013 11:44:43 am

Nice tutorial sir, I wonder if that works on barcode after scanning using webcam?

Reply
2/5/2013 01:12:44 pm

I think this would work on barcode. What you need is a unique key for every image and you should know the trigger event of the barcode upon scanning. :)

Reply
saito
3/6/2013 03:01:54 pm

thank you sir!!! do you have tutorial for that?

saito
3/6/2013 03:03:00 pm

thank you sir!!! do you have tutorial for that matter.

sunil walia
6/13/2013 09:02:25 pm

but i need same code in c#

Reply
jham
9/29/2013 10:23:02 am

Email me so that i could send you the code in C# :)

Reply
zee haak
9/27/2013 04:41:41 am

thanks friend...
live long.......

Reply
jham
9/29/2013 10:23:40 am

ur welcome buddy..

Live long...

Reply
ken
1/3/2014 11:40:39 am

file is currupted

Reply
Jham
11/19/2014 09:38:10 am

You can buzz me up on my email for this buddy through contact form.

Thanks

Reply
zeehaak
2/17/2014 04:47:57 pm

thanks bro...

Reply
Jham
11/19/2014 09:39:56 am

Ur Welcome Buddy. Happy coding :)

Reply
Dadeai Aps
1/22/2015 10:03:30 pm

Thanks sir for that code

Reply
Jham
1/26/2016 06:58:40 pm

Thanks as well buddy :) happy coding

Reply
LostMagician
2/24/2015 09:18:17 am

Sir the file is corrupted. Please send me one.

Reply
3/5/2015 01:52:10 pm

fuck it

Reply
11/21/2015 04:32:15 am

Thank you sir for your tutorials... its most usefull for me..

Reply
Jham
1/26/2016 06:59:38 pm

Thanks as well buddy :)

Reply
3/21/2016 05:12:30 am

mga tanga kayo

Reply
efci
8/4/2017 11:21:49 pm

haha

Reply
Rufus Igbayemi
11/1/2016 05:38:05 am

Thanks for the code, pls can i see the code for retrieving image back to vb.net form?

Reply



Leave a Reply.