save images in a database

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 24836 Views - Last Post: 17 February 2014 - 05:47 AM Rate Topic: -----

#1 moevet07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-November 07

save images in a database

Post icon  Posted 28 November 2007 - 02:15 PM

Hi

I am trying to store images on a database using the picturebox object to retreve them but with no luck. anyone can help out with a code snippet?

thanks
Is This A Good Question/Topic? 0
  • +

Replies To: save images in a database

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: save images in a database

Posted 28 November 2007 - 11:18 PM

Well Ill tell you it is very inefficient to store images in a database. Its ok to store the path to the file, the name of the file, even the name to display for the file, but store the images in a directory on the users computer.

If this make sense to you and you wish to research it further let me know what version of VB you're using, VB6 or VB.Net (VB2005, VB2003 VB 2005 Express Edition all fall into VB.Net) and Ill try and guide you the best I can :)

This post has been edited by PsychoCoder: 28 November 2007 - 11:18 PM

Was This Post Helpful? 0
  • +
  • -

#3 Guest_dtekind*


Reputation:

Re: save images in a database

Posted 03 May 2009 - 05:48 AM

thanks psychocoder for ur info . for me ,i'm using vb.net 2005, and i have the same problem i had saved image into database as binary format but i get prblm when i want get the image from data to my picturebox. can u help me .here code is.

	Public Const MAX_IMAGE_SIZE As Long = 32768 ' 32 Ko
	Dim picture as image
	Dim ms As New System.IO.MemoryStream
	Dim arrPicture() As Byte

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Try
			connect()
		Catch ex As Exception

		End Try
	End Sub

	Private Sub saveimage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'i have just one field in my database called image
		myCmd = New SqlCommand("insert into images values (@image)", cn)
		If Not IsNothing(picture) Then
			picture.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
			If ms.Length > MAX_IMAGE_SIZE Then
				MsgBox("image trop grosse")
			End If
			arrPicture = ms.GetBuffer()
			ms.Flush()
			myCmd.Parameters.Add("@image", SqlDbType.Image).Value = Me.PictureBox1.Image 
		Else
			myCmd.Parameters.Add("@image", SqlDbType.Image).Value = DBNull.Value
		End If
		myCmd.ExecuteNonQuery()

	End Sub

	Private Sub getimage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		Dim myCmd As SqlCommand
		Dim reader As SqlDataReader


		myCmd = New SqlCommand("select * from images", cn)
		reader = myCmd.ExecuteReader()
		While reader.Read
			' Traitement des autres champs ... puis :
			If Not reader.IsDBNull(0) Then
				Dim arrPicture(MAX_IMAGE_SIZE) As Byte
				reader.GetBytes(2, 0, arrPicture, 0, MAX_IMAGE_SIZE)
				Dim ms As New System.IO.MemoryStream(arrPicture)
				picture = [Image].FromStream(ms)
				Me.PictureBox2.Image = picture
			End If
		End While
	End Sub




Mod Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)
Was This Post Helpful? 1

#4 firebolt  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 92
  • View blog
  • Posts: 5,561
  • Joined: 20-February 09

Re: save images in a database

Posted 03 May 2009 - 11:01 PM

VB2005 belongs in the VB.NET section. I'll get a mod to move it.
Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: save images in a database

Posted 04 May 2009 - 07:01 AM

Moved to VB.NET forum :)
Was This Post Helpful? 0
  • +
  • -

#6 sraptor  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 22-April 09

Re: save images in a database

Posted 04 May 2009 - 09:03 AM

PsychoCoder, how you do it in vb2008?
Was This Post Helpful? 0
  • +
  • -

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: save images in a database

Posted 04 May 2009 - 09:22 AM

This is how I would do it (using SQL Server, not Access)

Public Function GetImageFromDB() As Bitmap
	 Dim con As New System.Data.SqlClient.SqlConnection("YourConnectionStringHere")
	 con.Open()
	 Dim cmd As New System.Data.SqlClient.SqlCommand("select * from images")
	 cmd.Connection = con
	 cmd.CommandType = CommandType.Text
	 Dim da As New System.Data.SqlClient.SqlDataAdapter(cmd)
	 Dim ds As New DataSet()
	 da.Fill(ds)
	 Dim imgBytes As Byte() = CType(ds.Tables(0).Rows(0).Item(0), Byte())
	 Dim stream As New MemoryStream(imgBytes)
	 Dim bmp As New Bitmap(stream)
	 Return bmp
End Function



In that code change YourConnectionStringHere with your own connection string. This function returns a Bitmap object so you could do

Me.PictureBox2.Image = GetImageFromDB()



This code is designed to retrieve a single image, but can be modified to retrieve multiple images in a loop.

Hope this helps :)
Was This Post Helpful? 0
  • +
  • -

#8 sraptor  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 22-April 09

Re: save images in a database

Posted 04 May 2009 - 10:06 AM

How about inserting image in sql?
Was This Post Helpful? 0
  • +
  • -

#9 sraptor  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 22-April 09

Re: save images in a database

Posted 07 May 2009 - 10:46 AM

PsychoCoder you told me about retrive but how I insert it to sql database?
I was trying wity outluck, please can you tell me how?

Thankx
Was This Post Helpful? 0
  • +
  • -

#10 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: save images in a database

Posted 07 May 2009 - 11:17 AM

I'm in the process of putting a tutorial together on this very topic. I'll let you know when it's finished :)
Was This Post Helpful? 0
  • +
  • -

#11 sraptor  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 22-April 09

Re: save images in a database

Posted 07 May 2009 - 01:38 PM

View PostPsychoCoder, on 7 May, 2009 - 10:17 AM, said:

I'm in the process of putting a tutorial together on this very topic. I'll let you know when it's finished :)


That Will be great, not tutorial is made yet for sql and vb 2008 for images? A lot people will be using it

Thanks
Was This Post Helpful? 0
  • +
  • -

#12 Pit Ufo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-October 08

Re: save images in a database

Posted 09 May 2009 - 01:48 PM

Try with this code.

Imports System.IO
.
.
.
'Pic is a PictureBox
Private Sub SaveImage()
		Try
			Dim nR As DataRow = Dataset.Tables(0).NewRow
			Dim MS As New MemoryStream()
			Pic.Image.Save(MS, Pic.Image.RawFormat)
			Dim myImage() As Byte = MS.GetBuffer
			nR("IMAGE") = myImage
			Dataset.Tables(0).Rows.Add(nR)
			TableAdapter.Update(Dataset)
		Catch ex As Exception
			Throw ex
		End Try
	End Sub


Was This Post Helpful? 0
  • +
  • -

#13 sraptor  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 22-April 09

Re: save images in a database

Posted 10 May 2009 - 02:04 PM

There dont say about how insert from sql
Was This Post Helpful? 1
  • +
  • -

#14 yangma  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 03-May 09

Re: save images in a database

Posted 10 May 2009 - 10:33 PM

okay...can anyone show how to store image into sql....using vs 2005
Was This Post Helpful? 0
  • +
  • -

#15 sraptor  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 22-April 09

Re: save images in a database

Posted 12 May 2009 - 06:40 AM

I can not understand the code from pit ufo, no SQL insert in the code
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2