How to image insert to database

Mittaikariss
Posted by Mittaikariss under C# category on | Points: 40 | Views : 2412
1.first add to button this button name is browse and
add to picture box this name is pcimg

2.now brwose the image and load to image in picture box...coding is
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";

if (open.ShowDialog() == DialogResult.OK)
{
Image img = new Bitmap(open.FileName);
pcimg.Image = img.GetThumbnailImage(340, 165, null, new IntPtr());

open.RestoreDirectory = true;
Image thumbimage = pcimg.Image;
pcimg.Image = thumbimage.GetThumbnailImage(100, 100, null, new IntPtr());
}

3.now image convert to the byte.we can using filestream or MemoryStream..memory stream is better than filestream .......coding is
  byte[] emp_photo;                           
MemoryStream ms = new MemoryStream();
pcimg.Image.Save(ms, ImageFormat.Jpeg);
emp_photo = new byte[ms.Length];
ms.Position = 0;
ms.Read(emp_photo, 0, emp_photo.Length);

4.now add image to sql data base....image sql datatype is image
coding is


SqlParameter pm = new SqlParameter("@logo", SqlDbType.Image);
pm.Value = emp_photo;
cmd.Parameters.Add(pm
);
cmd.ExecuteNonQuery();

5.now image stored image in binary format

Invom Technology
http://www.invomtech.com/

Comments or Responses

Posted by: CS1401 on: 9/17/2011 Level:Starter | Status: [Member] | Points: 10
MemoryStream ms = new MemoryStream();
pcimg.Image.Save(ms, ImageFormat.Jpeg);

Hi, here what is the use of memorystrem. can u explain for me?
Posted by: Mittaikariss on: 10/19/2011 Level:Bronze | Status: [Member] | Points: 10
reaer this link for memory stream........
http://www.dotnetperls.com/memorystream
Posted by: Mittaikariss on: 10/19/2011 Level:Bronze | Status: [Member] | Points: 10
appadium unakku purialia vettukku va......solren..........i dont know how to say...........
Posted by: CS1401 on: 10/20/2011 Level:Starter | Status: [Member] | Points: 10
hey how i come india.. i am in nz man..
Posted by: Mittaikariss on: 10/20/2011 Level:Bronze | Status: [Member] | Points: 10
ha ha ha ha ha OK OK OK i forget that... OK how do you know Tamil.....
Posted by: CS1401 on: 10/20/2011 Level:Starter | Status: [Member] | Points: 10
my grandma know thoda thoda tamil..

Login to post response