what is control that i should use to save image into my database

Dani AI

Generated

There is no special "image control" you must use to save images; you read the image into a byte array and store those bytes in the database. In SQL Server, prefer a varbinary(max) column (the old image type is deprecated) and consider FILESTREAM for very large files. On the UI side, use a FileUpload control in ASP.NET or an OpenFileDialog in WinForms to get the file, and a PictureBox to display it. See Microsoft docs on varbinary(max) and the deprecation notice for image.

Example schema and insert with VB.NET that avoids AddWithValue and sets the correct type/size:

CREATE TABLE dbo.Photos
(
  PhotoId int IDENTITY PRIMARY KEY,
  Content varbinary(max) NOT NULL,
  ContentType nvarchar(100) NULL,
  FileName nvarchar(260) NULL
);
Using conn As New SqlClient.SqlConnection(connString), _
      cmd As New SqlClient.SqlCommand("INSERT INTO dbo.Photos (Content, ContentType, FileName) VALUES (@b, @ct, @fn)", conn)

    cmd.Parameters.Add("@b", SqlDbType.VarBinary, -1).Value = imageBytes ' -1 for MAX
    cmd.Parameters.Add("@ct", SqlDbType.NVarChar, 100).Value = contentType  ' e.g., image/png
    cmd.Parameters.Add("@fn", SqlDbType.NVarChar, 260).Value = fileName

    conn.Open()
    cmd.ExecuteNonQuery()
End Using

To display:

  • WinForms: create an Image from the bytes with Image.FromStream(new MemoryStream(bytes)) and assign to PictureBox.Image. Dispose streams/images to avoid leaks.
  • ASP.NET: return the bytes from a handler/controller with the correct Content-Type and write the binary response.

Practical tips: validate that the uploaded file is an image (check MIME/signature), store width/height and a thumbnail, and consider storing only file paths if you do not need transactional storage. For serving large BLOBs efficiently, review SQL Server FILESTREAM guidance.

Recommended Answers

All 10 Replies

what is control that i should use to save image into my database

I have attached a small demo project for reading and writing a image in a database. It was developed in VS 2003. Have a look at it and modify it according to your own needs.

More on thread
I want information on how to save images into the database and retrive the images into their various picture boxes using visual basic code in windows form.

can you assist me ?
I want to use vb.net to write a code that will save data into an sql server database and retrive it to a picturebox.
thanks

hi
i think u can't save apicture as apictur in database but u can save the link in database and u can call the picture by acommand or any call sentance


ex
if u save apicture in c:\test\p1.jpg
and
u save this path in database

u can
create acommand to Retriev the path
and change the url of image to the path
like
image.url="path"

i hope help u

Really thanks very much for the sample program.i really happy with the program i just learn the SQL server. like these program is very very useful to me.

Member Avatar for Member #857553
'Save an Image to a memory stream so you can get the bytes
        Dim sampleImage As Bitmap = New Bitmap(100, 100)
        Dim mStream As New System.IO.MemoryStream
        Dim ImageBytes As Byte()

        sampleImage.Save(mStream, Imaging.ImageFormat.Png)
        ImageBytes = mStream.ToArray

        'Sample Insert image command
        'Save the bytes from the image into a image or varbinary column
        Dim com As New SqlClient.SqlCommand("Insert Into MyTable" & vbCrLf & _
                                            "(MyImageColumn)" & vbCrLf & _
                                            "Values(@MyImage)")

        'an image column or varbinary column
        com.Parameters.Add("@MyImage", SqlDbType.Image)
        com.Parameters("@MyImage").Value = ImageBytes


        'Sample Read Image Command
        'Read the bytes from the table and create a new memory stream from them
        com.CommandText = "Select MyImage From MyTable"
        Dim rdr As System.Data.SqlClient.SqlDataReader

        rdr = com.ExecuteReader
        If rdr.Read Then
            Dim newMstream As New System.IO.MemoryStream(CType(rdr.Item("MyImage"), Byte()))
            'Create a new image from the bytes from the memory
            Dim ImageFromDB As New Bitmap(newMstream)

        End If

Friends! Do not resurrect old threads. This thread is quite old (3 years).

Thread closed.

Table:

 CREATE TABLE [dbo].[imgtable](
      [id] [int] NULL,
      [img] [image] NULL
  ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

Code :

        Dim cmd As New SqlCommand("insert into imgtable (id,img) values ( @id,@img)", cnn)
        cmd.Parameters.AddWithValue("@id", id)
        cmd.Parameters.AddWithValue("@img", content)
        cmd.ExecuteNonQuery()

source: Store image in database - vb.net

winston

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.