Read image data from database and display that image : Load Image « Database ADO.net « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Database ADO.net » Load ImageScreenshots 
Read image data from database and display that image

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.IO;
 
 
public class Form1 : System.Windows.Forms.Form {
   private System.Windows.Forms.Button button1;
   private System.Windows.Forms.TextBox textBox1;
   private System.Windows.Forms.PictureBox pictureBox1;
   private Images images;

   private System.ComponentModel.Container components = null;

   public Form1() {
      InitializeComponent();

      images = new Images();

      if (images.GetRow()) {
            this.textBox1.Text = images.GetFilename();
            this.pictureBox1.Image = (Image)images.GetImage();
      else {
            this.textBox1.Text = "DONE";
            this.pictureBox1.Image = null;
      }
   }

   private void InitializeComponent() {
      this.button1 = new System.Windows.Forms.Button();
      this.textBox1 = new System.Windows.Forms.TextBox();
      this.pictureBox1 = new System.Windows.Forms.PictureBox();
      this.SuspendLayout();

      this.button1.Location = new System.Drawing.Point(2008);
      this.button1.Name = "button1";
      this.button1.TabIndex = 0;
      this.button1.Text = "Next";
      this.button1.Click += new System.EventHandler(this.button1_Click);

      this.textBox1.Location = new System.Drawing.Point(248);
      this.textBox1.Name = "textBox1";
      this.textBox1.Size = new System.Drawing.Size(14420);
      this.textBox1.TabIndex = 1;
      this.textBox1.Text = "";

      this.pictureBox1.Location = new System.Drawing.Point(848);
      this.pictureBox1.Name = "pictureBox1";
      this.pictureBox1.Size = new System.Drawing.Size(280208);
      this.pictureBox1.TabIndex = 2;
      this.pictureBox1.TabStop = false;

      this.AutoScaleBaseSize = new System.Drawing.Size(513);
      this.ClientSize = new System.Drawing.Size(292272);
      this.Controls.Add(this.pictureBox1);
      this.Controls.Add(this.textBox1);
      this.Controls.Add(this.button1);
      this.Name = "Form1";
      this.Text = "Display Images";
      this.ResumeLayout(false);
   }

   static void Main() {
      Application.Run(new Form1());
   }

   private void button1_Click(object sender, System.EventArgs e) {
      if (images.GetRow()) {
         this.textBox1.Text = images.GetFilename();
         this.pictureBox1.Image = (Image)images.GetImage();
      else {
         this.textBox1.Text = "DONE";
         this.pictureBox1.Image = null;
      }
   }
}


public class Images{
   string imageFilename = null;
   byte[] imageBytes = null;

   SqlConnection imageConnection = null;
   SqlCommand imageCommand = null;
   SqlDataReader imageReader = null;

   public Images() {
      imageConnection = new SqlConnection("server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI");
      imageCommand = new SqlCommand(@"select imagefile, imagedata from imagetable", imageConnection);

      imageConnection.Open();
      imageReader = imageCommand.ExecuteReader();
   }

   public Bitmap GetImage() {
      MemoryStream ms = new MemoryStream(imageBytes);
      Bitmap bmap = new Bitmap(ms);

      return bmap;
   }

   public string GetFilename() {
      return imageFilename;
   }

   public bool GetRow() {
      if (imageReader.Read()) {
        imageFilename = (stringimageReader.GetValue(0);
        imageBytes = (byte[]) imageReader.GetValue(1);
        return true;
      }else {
        return false;
      }
   }

   public void EndImages() {
      imageReader.Close();
      imageConnection.Close();
   }
}




           
       
Related examples in the same category
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.