Hi shaneko,
Check this example. Now please take its reference and correct your code.
Database
For this sample I have created a simple table with the following structure.
You can download the database table SQL by clicking the download link below.
Download SQL file
HTML
<div>
<asp:FileUpload ID="fuImage" runat="server" />
<asp:Button Text="Save" ID="btnSave" runat="server" OnClick="Save" />
<br />
<asp:Image ID="Image1" runat="server" Visible="false" />
</div>
Namespaces
C#
using System.IO;
using System.Configuration;
using System.Data.SqlClient;
using System.Drawing.Imaging;
using System.Data;
VB.Net
Imports System.IO
Imports System.Drawing.Imaging
Imports System.Data.SqlClient
Imports System.Data
Code
CS
protected void Save(object sender, EventArgs e)
{
if (fuImage.HasFile)
{
Byte[] bytes;
string contentType = fuImage.PostedFile.ContentType;
string fileName = Path.GetFileName(fuImage.FileName);
string filePath = fuImage.PostedFile.FileName;
System.Drawing.Image image = System.Drawing.Image.FromFile(filePath);
// Resize image
using (System.Drawing.Image thumbnail = image.GetThumbnailImage(130, 170, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero))
{
using (MemoryStream memoryStream = new MemoryStream())
{
thumbnail.Save(memoryStream, ImageFormat.Png);
bytes = new Byte[memoryStream.Length];
memoryStream.Position = 0;
memoryStream.Read(bytes, 0, (int)bytes.Length);
}
}
// Insert uploaded image to Database
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "INSERT INTO tblFiles1 VALUES (@Name, @ContentType, @Data)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Name", fileName);
cmd.Parameters.AddWithValue("@ContentType", contentType);
cmd.Parameters.AddWithValue("@Data", bytes);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
// Display image after upload to Database
Image1.Visible = true;
byte[] byteData = (byte[])GetData("SELECT Data FROM tblFiles1").Rows[0]["Data"];
string base64String = Convert.ToBase64String(byteData, 0, byteData.Length);
Image1.ImageUrl = "data:image/png;base64," + base64String;
}
private DataTable GetData(string query)
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
return dt;
}
}
public bool ThumbnailCallback()
{
return false;
}
VB.Net
Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
If fuImage.HasFile Then
Dim bytes As Byte()
Dim contentType As String = fuImage.PostedFile.ContentType
Dim fileName As String = Path.GetFileName(fuImage.FileName)
Dim filePath As String = fuImage.PostedFile.FileName
Dim image As System.Drawing.Image = System.Drawing.Image.FromFile(filePath)
' Resize image
Using thumbnail As System.Drawing.Image = image.GetThumbnailImage(130, 170, New System.Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback), IntPtr.Zero)
Using memoryStream As MemoryStream = New MemoryStream()
thumbnail.Save(memoryStream, ImageFormat.Png)
bytes = New Byte(memoryStream.Length - 1) {}
memoryStream.Position = 0
memoryStream.Read(bytes, 0, CInt(bytes.Length))
End Using
End Using
' Insert uploaded image to Database
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Dim query As String = "INSERT INTO tblFiles1 VALUES (@Name, @ContentType, @Data)"
Using cmd As SqlCommand = New SqlCommand(query)
cmd.Connection = con
cmd.Parameters.AddWithValue("@Name", fileName)
cmd.Parameters.AddWithValue("@ContentType", contentType)
cmd.Parameters.AddWithValue("@Data", bytes)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End If
' Display image after upload to Database
Image1.Visible = True
Dim byteData As Byte() = CType(GetData("SELECT Data FROM tblFiles1").Rows(0)("Data"), Byte())
Dim base64String As String = Convert.ToBase64String(byteData, 0, byteData.Length)
Image1.ImageUrl = "data:image/png;base64," & base64String
End Sub
Private Function GetData(ByVal query As String) As DataTable
Dim dt As DataTable = New DataTable()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.CommandType = CommandType.Text
cmd.Connection = con
sda.SelectCommand = cmd
sda.Fill(dt)
End Using
End Using
Return dt
End Using
End Function
Public Function ThumbnailCallback() As Boolean
Return False
End Function
Screenshot