Reputation: 1176
I didn't make the image into binary form, but want to get that image data from database now. So can you give so idea about that. I have done like below to insert image:
FileDialog dialog = new OpenFileDialog();
dialog.InitialDirectory = @":D\";
dialog.Filter = "(*.jpg;*.gif;*.jpeg;*.bmp)| *.jpg; *.gif; *.jpeg; *.bmp";
if (dialog.ShowDialog() == DialogResult.OK)
{
imagename = dialog.FileName;
pictureBox1.Image = Image.FromFile(imagename);
}
dialog = null;
Then it also store in database but now I have to retrive the image in next form how can I do?
Upvotes: 0
Views: 10191
Reputation: 49
using System.Data.SqlClient;
using System.Drawing;
using System.Data;
using System.IO;
using System.Drawing.Imaging;
public void Save_Image(Object sender, EventArgs e)
{
// Create a byte[] from the input file
int len = Upload.PostedFile.ContentLength;
byte[] pic = new byte[len];
Upload.PostedFile.InputStream.Read (pic, 0, len);
// Insert the image into the database
SqlConnection connection = new
SqlConnection (@"server=abc\.SQLEXPRESS;database=Storage;uid=sa;pwd=sa");
try
{
connection.Open ();
SqlCommand cmd = new SqlCommand ("insert into Image "
+ "(Picture) values (@pic)", connection);
cmd.Parameters.Add ("@pic", pic);
cmd.ExecuteNonQuery ();
}
finally
{
connection.Close ();
}
}
we are only storing byte-image into table name"Image" which contains only one column. Storing byte-image into database consume a lot of database-size for large image-storing and retrieving specific image from database using search takes longer time for processing.This causes low performance and storage problem.
Upvotes: 0
Reputation:
protected void butSubmit_Click(object sender, EventArgs e)
{
SqlConnection connection = null;
try
{
Byte[] imgByte = null;
if (FileUpload1.HasFile && FileUpload1.PostedFile != null)
{
HttpPostedFile File = FileUpload1.PostedFile;
imgByte = new Byte[File.ContentLength];
File.InputStream.Read(imgByte, 0, File.ContentLength);
}
connection = new SqlConnection(ConfigurationManager.ConnectionStrings
"ConnectionString"].ConnectionString.ToString());
connection.Open();
string sql = "INSERT INTO Table1(title,image) VALUES(@theTitle, @theImage) SELECT
@@IDENTITY";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.Parameters.AddWithValue("@theTitle", txtTitle.Text);
cmd.Parameters.AddWithValue("@theImage", imgByte);
int id = Convert.ToInt32(cmd.ExecuteScalar());
lblStatus.Text = String.Format("ID is {0}", id);
Image1.ImageUrl = "~/DisplayImg.ashx?id=" + id;
}
{
lblStatus.Text = "There was an error";
}
finally
{
connection.Close();
}
}
Upvotes: 2
Reputation: 1176
int O_id =Convert.ToInt32(textBox2.Text);
SqlConnection cn = new SqlConnection(strCn);
SqlCommand cmd = new SqlCommand("INSERT INTO BLOBTest (BLOBData, O_id) VALUES (@BLOBData,'"+O_id+"')", cn);
String strBLOBFilePath = textBox1.Text;//Modify this path as needed.
//Read jpg into file stream, and from there into Byte array.
FileStream fsBLOBFile = new FileStream(strBLOBFilePath, FileMode.Open, FileAccess.Read);
Byte[] bytBLOBData = new Byte[fsBLOBFile.Length];
fsBLOBFile.Read(bytBLOBData, 0, bytBLOBData.Length);
fsBLOBFile.Close();
//Create parameter for insert command and add to SqlCommand object.
SqlParameter prm = new SqlParameter("@BLOBData", SqlDbType.VarBinary, bytBLOBData.Length, ParameterDirection.Input, false,
0, 0, null, DataRowVersion.Current, bytBLOBData);
cmd.Parameters.Add(prm);
//Open connection, execute query, and close connection.
cn.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Picture has been uploaded");
cn.Close();
Upvotes: 1
Reputation: 56727
OK, assuming you stored the image as a BLOB field in your database, the following code retrieves the BLOB field data, creates a memory stream and loads a Bitmap
from the memory stream:
using (SqlConnection conn = ...)
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("SELECT Picture FROM <tableName> WHERE ...", conn)
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
byte[] picData= reader["Picture"] as byte[] ?? null;
if (picData!= null)
{
using (MemoryStream ms = new MemoryStream(picData))
{
// Load the image from the memory stream. How you do it depends
// on whether you're using Windows Forms or WPF.
// For Windows Forms you could write:
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(ms);
}
}
}
}
}
Upvotes: 0