Hi smile,
Check this example. Now please take its reference and correct your code.
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO
Code
C#
private void btnDownload_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=dbFiles;uid=sa;pwd=12345;");
SqlCommand cmd = new SqlCommand(@"SELECT Data FROM tblFiles WHERE Id = 1", con);
cmd.CommandType = CommandType.Text;
con.Open();
byte[] byteArray = (byte[])cmd.ExecuteScalar();
using (Image img = Image.FromStream(new MemoryStream(byteArray)))
{
FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
DialogResult dialogResult = folderBrowserDialog.ShowDialog();
if (dialogResult == DialogResult.OK)
{
string path = folderBrowserDialog.SelectedPath + "\\Test.jpg";
img.Save(path, ImageFormat.Jpeg); // Or Png
MessageBox.Show("Image saved in folder " + folderBrowserDialog.SelectedPath, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
con.Close();
}
VB.Net
Private Sub btnDownload_Click(sender As System.Object, e As System.EventArgs) Handles btnDownload.Click
Dim con As SqlConnection = New SqlConnection("Data Source=.;Initial Catalog=dbFiles;uid=sa;pwd=12345;")
Dim cmd As SqlCommand = New SqlCommand("SELECT Data FROM tblFiles WHERE Id = 1", con)
cmd.CommandType = CommandType.Text
con.Open()
Dim byteArray As Byte() = CType(cmd.ExecuteScalar(), Byte())
Using img As Image = Image.FromStream(New MemoryStream(byteArray))
Dim folderBrowserDialog As FolderBrowserDialog = New FolderBrowserDialog()
Dim dialogResult As DialogResult = folderBrowserDialog.ShowDialog()
If dialogResult = dialogResult.OK Then
Dim path As String = folderBrowserDialog.SelectedPath & "\Test.jpg"
img.Save(path, ImageFormat.Jpeg)
MessageBox.Show("Image saved in folder " & folderBrowserDialog.SelectedPath, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Using
con.Close()
End Sub
Screenshot