Display image : Image Load Save « ADO.Net « C# / CSharp Tutorial






using System;
using System.Collections.Generic;
using System.ComponentModel;

using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.IO;

public class DisplayImages : Form
{
    public DisplayImages()
    {
        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 button1_Click(object sender, 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;
        }

    }
    private void InitializeComponent()
    {
        this.button1 = new System.Windows.Forms.Button();
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.pictureBox1 = new System.Windows.Forms.PictureBox();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
        this.SuspendLayout();
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(175, 22);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(75, 23);
        this.button1.TabIndex = 0;
        this.button1.Text = "Show Image";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // textBox1
        // 
        this.textBox1.Location = new System.Drawing.Point(30, 22);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(126, 20);
        this.textBox1.TabIndex = 1;
        // 
        // pictureBox1
        // 
        this.pictureBox1.Location = new System.Drawing.Point(30, 64);
        this.pictureBox1.Name = "pictureBox1";
        this.pictureBox1.Size = new System.Drawing.Size(220, 221);
        this.pictureBox1.TabIndex = 2;
        this.pictureBox1.TabStop = false;
        // 
        // DisplayImages
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292, 297);
        this.Controls.Add(this.pictureBox1);
        this.Controls.Add(this.textBox1);
        this.Controls.Add(this.button1);
        this.Name = "DisplayImages";
        this.Text = "Display Images";
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.PictureBox pictureBox1;
    private Images images;


    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new DisplayImages());
    }

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

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

    // Constructor
    public Images()
    {
        imageConnection = new SqlConnection(@"data source = .\sqlexpress;integrated security = true;initial catalog = tempdb;");
        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 = (string)imageReader.GetValue(0);
            imageBytes = (byte[])imageReader.GetValue(1);

            return true;
        }
        else
        {
            return false;
        }
    }

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








32.56.Image Load Save
32.56.1.Load image to database
32.56.2.Read Bitmap from database
32.56.3.Display image
32.56.4.Writes binary data to a file
32.56.5.LoadImages.cs