Chinaunix首页 | 论坛 | 博客
  • 博客访问: 223940
  • 博文数量: 45
  • 博客积分: 3010
  • 博客等级: 中校
  • 技术积分: 915
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-18 16:03
文章分类

全部博文(45)

文章存档

2011年(1)

2008年(44)

我的朋友

分类:

2008-07-18 14:08:49

WinForm添加图片到access数据库(1)这篇文章中是写入图片到数据库中
以下是从数据库显示图片到窗体中
代码如下:
 

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

namespace _7788Demo
{
    ///



    /// Form2 的摘要说明。

    ///


    public class frmShowImage : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.PictureBox imgPicture;
        ///

        /// 必需的设计器变量。

        ///


        private System.ComponentModel.Container components = null;
        private byte[] image = null;
        public frmShowImage()
        {
        
            //

            // Windows 窗体设计器支持所必需的

            //

            InitializeComponent();        
            

            //

            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码

            //

            
        }

        ///

        /// 清理所有正在使用的资源。

        ///


        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows 窗体设计器生成的代码
        ///

        /// 设计器支持所需的方法 - 不要使用代码编辑器修改

        /// 此方法的内容。

        ///


        private void InitializeComponent()
        {
            System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmShowImage));
            this.imgPicture = new System.Windows.Forms.PictureBox();
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            //

            // imgPicture

            //

            this.imgPicture.Image = ((System.Drawing.Image)(resources.GetObject("imgPicture.Image")));
            this.imgPicture.Location = new System.Drawing.Point(32, 24);
            this.imgPicture.Name = "imgPicture";
            this.imgPicture.Size = new System.Drawing.Size(176, 136);
            this.imgPicture.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            this.imgPicture.TabIndex = 0;
            this.imgPicture.TabStop = false;
            //

            // label1

            //

            this.label1.Location = new System.Drawing.Point(48, 192);
            this.label1.Name = "label1";
            this.label1.TabIndex = 1;
            this.label1.Text = "显示图片";
            //

            // frmShowImage

            //

            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(392, 238);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.imgPicture);
            this.Name = "frmShowImage";
            this.Text = "显示图片";
            this.Load += new System.EventHandler(this.frmShowImage_Load);
            this.ResumeLayout(false);

        }
        #endregion

        private void frmShowImage_Load(object sender, System.EventArgs e)
        {
            try
            {
                //数据库应为程序生成exe文件同目录

                string path = Application.StartupPath + "\\ImageDB.mdb";//取得数据库所在目录

                OleDbConnection oConn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='"+path+"'");
                string strSql="select imageAAA from imageTable where id=8";            
                OleDbCommand cmd = new OleDbCommand(strSql,oConn);
                //判断连接状态是否关闭,则打开连接

                if(oConn.State == ConnectionState.Closed)
                {
                    oConn.Open();             
                }
                OleDbDataReader reader=cmd.ExecuteReader();         
                while(reader.Read())
                {
                    image = (byte[])reader["imageAAA"]; //强制转换成byte[]数组

                    MemoryStream ms = new MemoryStream(image);
                    ms.Write(image,0,image.Length);
                    this.imgPicture.Image = Image.FromStream(ms);    //显示图片                

                }
                oConn.Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(),"错误提示!");
            }
        }
    }
}

阅读(1710) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~