Click here to Skip to main content
15,905,563 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DigiAlert1
{
    public partial class converting : Form
    {
        SqlConnection con = new SqlConnection("server=RND3-12\\MSSQLEXPDB;user id=sa;password=prayoglabs@123;database=mlrit");
        SqlDataAdapter da;
        DataSet ds;
        string s;
        MemoryStream ms;
        FileStream fs;
        Byte[] b;
        string fname;
        public converting()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            pictureBox1.ImageLocation = openFileDialog1.FileName;
            s = openFileDialog1.FileName;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            //FileInfo file = new FileInfo(s);
            //FileStream fs = new FileStream(s, FileMode.Open, FileAccess.Read);
            //BinaryReader br = new BinaryReader(fs);
            //byte[] image = br.ReadBytes((int)fs.Length);
            //br.Close();
            //fs.Close();
            //da = new SqlDataAdapter("insert into img values('" + image + "')", con);
            //ds = new DataSet();
            //da.Fill(ds, "img");
            updatedata();
        }
        private void updatedata()
        {
            //use filestream object to read the image.
            //read to the full length of image to a byte array.
            //add this byte as an oracle parameter and insert it into database.
            try
            {
                //proceed only when the image has a valid path
                if (s != "")
                {
                    FileStream fs;
                    fs = new FileStream(s, FileMode.Open, FileAccess.Read);
                    //a byte array to read the image
                    byte[] picbyte = new byte[fs.Length];
                    fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length));
                    fs.Close();
                    da = new SqlDataAdapter("insert into img values('" + picbyte + "')", con);
                    ds = new DataSet();
                    da.Fill(ds, "img");
                    MessageBox.Show("Image Added");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        //private void button3_Click(object sender, EventArgs e)
        //{
        //    da = new SqlDataAdapter("select * from img where id=107", con);
        //    ds = new DataSet();
        //    da.Fill(ds, "img");
        //    byte[] data = (byte[])ds.Tables["img"].Rows[0][1];
        //    ms = new MemoryStream(data, true);
        //    var i = ms;
        //    // textBox2.Text = ms.Length.ToString();
        //    pictureBox2.Image = Image.FromStream(ms);
        //    ms.Write(data, 0, Convert.ToInt32(data.Length));
        //    // ms.Write(data, 0, data.Length);
        //    // tempImg = Image.FromStream(ms);
        //    Image timg = pictureBox2.Image;
        //    pictureBox2.Image = timg;
        //    pictureBox2.Refresh();
        // }

        private void button3_Click(object sender, EventArgs e)
        {
            da = new SqlDataAdapter("select * from img where id=104", con);
            ds = new DataSet();
            da.Fill(ds, "img");
            FileStream FS1 = new FileStream("image.jpg", FileMode.Create);
            byte[] blob = (byte[])ds.Tables["img"].Rows[0][1];
            FS1.Write(blob, 0, blob.Length);
            FS1.Close();
            FS1 = null;
            pictureBox2.Image = Image.FromFile("image.jpg");
            pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox2.Refresh();
        }
            //private void button2_Click(object sender, EventArgs e)
            //{
            //    ms = new MemoryStream();
            //    fs = new FileStream(fname, FileMode.Open, FileAccess.Read);
            //    b = new Byte[fs.Length];
            //    fs.Read(b, 0, Convert.ToInt32(fs.Length));
            //    fs.Close();
            //    pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
            //    byte[] data = new byte[ms.Length];
            //    ms.Position = 0;
            //    var i = ms.Read(data, 0, Convert.ToInt32(ms.Length));
            //    da = new SqlDataAdapter("insert into img values('" + i + "')", con);
            //    ds = new DataSet();
            //    da.Fill(ds, "img");
            //    MessageBox.Show("success");
            //}

    }
}
Posted
Comments
OriginalGriff 25-Apr-11 2:48am    
And your question is?
Ankit Rajput 25-Apr-11 2:55am    
I think, He forgets what is the problem?
thatraja 25-Apr-11 4:23am    
where is your question? update that...
Manfred Rudolf Bihy 25-Apr-11 5:16am    
Just to join in with the others:
Care to add a question to your "not yet quite a question" post.
velvet7 25-Apr-11 7:19am    
Thanks for sharing your code with us.

1 solution

its easy...

public byte[] ReadFile(string ImagePath)
       {
           byte[] Image_Binary_data = null;
           FileInfo FInfo = new FileInfo(ImagePath);
           long numBytes = FInfo.Length;
           FileStream FStream = new FileStream(ImagePath, FileMode.Open, FileAccess.Read);
           BinaryReader BR = new BinaryReader(FStream);
           Image_Binary_data = BR.ReadBytes((int)numBytes);
           return Image_Binary_data;
       }



below code dispalys the picture into picture box...

byte[] ImageData = binary_data_stored_into_database;

            Image NewImage;
            using (MemoryStream MS = new MemoryStream(ImageData, 0, ImageData.Length))
            {
                MS.Write(ImageData, 0, ImageData.Length);
                NewImage = Image.FromStream(MS, true);
            }

            pic_image.Image = NewImage;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900