5,136,421 members and growing! (12,077 online)
Email Password   helpLost your password?
Languages » C# » Windows Forms     Beginner License: The Code Project Open License (CPOL)

Sending/Recieving PictureBox Image in C# To/From Microsoft SQL SERVER

By Morteza Naeiamabadi

this article is written for those stucking in such a hell problem
C# (C# 2.0, C#), .NET (.NET, .NET 2.0), WinForms, Design, SysAdmin, CEO, Arch, DBA, Dev, QA

Posted: 10 May 2008
Updated: 10 May 2008
Views: 1,889
Announcements



Search    
Advanced Search
Sitemap
11 votes for this Article.
Popularity: 3.74 Rating: 3.59 out of 5
1 vote, 9.1%
1
3 votes, 27.3%
2
0 votes, 0.0%
3
4 votes, 36.4%
4
3 votes, 27.3%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

This Article is intended to fullfill the lack of clarity in Retrieving and sending PictureBox image

or lets say any image in win application to database and vice versa.So guys don't Panic,Relax

because the code is so simple.

Background

I will advice those guys who are not still familiar with SQL Language and Broadly Speaking not having of database interaction knowledge in win application in .NET to leave this page and go after those mentioned items.

Using the code

the full code with comment in each line is showned in below,enjoy it!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

//author:Morteza Naeimabadi  Location=IRAN ,Yazd
//written in 2008/03/12


namespace SampleOfMorteza
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //we are using SQl express.
            //My database name is "PictureDb".
            SqlConnection con = new SqlConnection("Server=.;database=PictureDb;integrated security=true");
            //i have used a table named "tblUsers" and fill their fields
            SqlCommand com = new SqlCommand("insert into tblUsers(fldCode,fldPic) values(1,@Pic)", con);
            
            //in here i have to save the picturebox image to tblUsers
            //because you couldn't able to send the PictureBox1.Image to field "fldPic" that is of kind 
            //"image" in SQL SERVER EXPRESS ,then you should do somthing else and that is converting
            //your picture box image to array of bytes and tehn sending that array to database.
            //in below we have used a stream which will be used to contain our picturebox image bytes.
            MemoryStream stream=new MemoryStream();
            //through the instruction below we does save the image to byte in the object "stream".
            pictureBox1.Image.Save(stream,System.Drawing.Imaging.ImageFormat.Jpeg);

            //the below is the most important part,actually you are transferring the bytes of array 
            //to the pic which is also of kind byte[]
            byte[] pic=stream.ToArray();

            com.Parameters.AddWithValue("@Pic", pic);
            try
            {
                con.Open();
                com.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection connect = new SqlConnection("Server=.;database=PictureDb;integrated security=true");
            SqlCommand command = new SqlCommand("select fldPic from tblUsers where fldCode=1", connect);
            //for retrieving the image field in SQL SERVER EXPRESS Database you should first bring 
            //that image in DataList or  DataTable
            //then add the content to the byte[] array.
            //That's ALL!
            SqlDataAdapter dp = new SqlDataAdapter(command);
            DataSet ds = new DataSet("MyImages");

            byte[] MyData = new byte[0];

            dp.Fill(ds, "MyImages");
            DataRow myRow;
            myRow = ds.Tables["MyImages"].Rows[0];

            MyData = (byte[])myRow["fldPic"];


            MemoryStream stream = new MemoryStream(MyData);
            //With below you are in fact converting the byte array of image to the real image.
            pictureBox2.Image = Image.FromStream(stream);
            
        }
    }
}
         

the coding image is:

Points of Interest

Working with RFID Technology in .NET,Serial communication in C# win app,all win app related topics.

History

I am Morteza Naeimabadi,Born in IRAN.I was been raised in India and also IRAN.I do programming about 3 to 4 years.22 Years Old.Married.Love my Wife(Sindokht).Crazy in learning new things.especially new things in .NET.

License

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

About the Author

Morteza Naeiamabadi


C# Win and Web Developer working in IRAN,YAZD,PishGamman Company.
Owned a Programming Company named: KAHROBA


Occupation: Software Developer (Senior)
Company: Kahroba
Location: Iran, Islamic Republic Of Iran, Islamic Republic Of

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
  (Refresh) 
Subject  Author Date 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 10 May 2008
Editor:
Copyright 2008 by Morteza Naeiamabadi
Everything else Copyright © CodeProject, 1999-2008
Web13 | Advertise on the Code Project