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

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » ADO.Net » Image Load Save 
32.56.3.Display image
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(17522);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(7523);
        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(3022);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(12620);
        this.textBox1.TabIndex = 1;
        // 
        // pictureBox1
        // 
        this.pictureBox1.Location = new System.Drawing.Point(3064);
        this.pictureBox1.Name = "pictureBox1";
        this.pictureBox1.Size = new System.Drawing.Size(220221);
        this.pictureBox1.TabIndex = 2;
        this.pictureBox1.TabStop = false;
        // 
        // DisplayImages
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292297);
        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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.