Retrieving OLE Object (Image) from Access

本文介绍了一种从Access数据库中检索存储为OLEObject类型的图片的方法,并详细展示了如何使用C#处理这些图片数据以去除头部信息,使得图片能够正常显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Retrieving OLE Object (Image) from Access

 

以下方法仅用于C#  WINForm , ACCESS 数据库, 查询 OLE Object 类型 存储的图片

 

且 ACCESS  OLE Object 类型 头部信息 并不完全了解.

 

请高手指点.

 

显示图片需要去掉头部信息. 一般是去掉前78个,有的76个.

 

 

 /// 2010-02-26 Forever

 

private void ReadLogo()
        {
            try
            {
                /// design
                // 此方法本身并不要求在这里定义一个控件,WinForm中,有一个PictureBox就可以了.

 

                PictureBox pictureBox1 = new PictureBox();

                pictureBox1.Location = new System.Drawing.Point(0, 0);
                pictureBox1.Name = "pictureBox1";
                pictureBox1.Size = new System.Drawing.Size(143, 66);
                pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
                pictureBox1.TabIndex = 0;
                pictureBox1.TabStop = false;

 

                /*
                 * *********************************************************************************
                 * *********************************************************************************
                 */


                // 连接ACCESS数据库,执行查询,得到结果, 关闭数据库.  具体根据需要.
                //
                string strDBFile = Environment.CurrentDirectory + @"/Test.mdb";
                string connstr = "Jet OLEDB:Database Password=;Data Source='" + strDBFile + "';Password=;Provider='Microsoft.Jet.OLEDB.4.0'";

                OleDbConnection conn = new OleDbConnection(connstr);
                DataTable tbl = new DataTable();

                try
                {
                    conn.Open();

                    String strCmd = "Select top 1 [ID],[Logo] from [Admin] where [Logo] is not null order by [ID] Desc";
                    OleDbCommand cmd = new OleDbCommand(strCmd, conn);

                    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                    da.Fill(tbl);
                }
                catch (Exception exConnDB)
                {
                    MessageBox.Show(exConnDB.Message);
                }
                finally
                {
                    conn.Close();
                } 

 

                ///
                ///
                ///

 

                // 利用 pictureBox 显示图片

                Byte[] byPicture;
                if(tbl.Rows.Count > 0)
                {
                    // 读出数据
                    byPicture = (byte[])tbl.Rows[0]["Logo"];

                    try
                    {
                        // 在我的数据库中, 只有21 和 255 开始的数据,并不代表所有, 望高手指点 OLE Object 类型 头部信息
                        if (Convert.ToInt32(byPicture[0]) == 21) 
                        {
                            /*
                             * byPicture[0] == 21
                             * byPicture[1] == 28
                             * byPicture[2] == 47 或 45 (Exception中, 减 78 和 76 的根据)
                             * ......
                            */


                            int intOffset = 0;
                            intOffset = Convert.ToInt32(byPicture[1].ToString()) + Convert.ToInt32(byPicture[2].ToString()) + 3;
                            // +3 指 要减去前3个 byPicture[0,1,2]

                            MemoryStream ms = new MemoryStream();
                            ms.Write(byPicture, intOffset, byPicture.Length - intOffset);

                            Bitmap bm;
                            bm = new Bitmap(ms);

                            //显示图片
                            pictureBox1.Image = bm;
                        }
                        else
                        {
                            // 处理 Convert.ToInt32(byPicture[0]) == 255
                            // 可以直接显示

                            /*
                             * byPicture[0] == 255
                             * ......
                            */

 

                            MemoryStream s = new MemoryStream(byPicture);

                            //显示图片
                            pictureBox1.Image = Image.FromStream(s);
                        }
                    }
                    catch (Exception)
                    {
                        #region

 

                        // 我不太确定,在ACCESS 中, OLE Object 存储的图片是否还有其他类型的头部信息,
                        // 所以, 仅在此捕捉异常.
                        // 请高手指点.

 

                        //MessageBox.Show("Logo ID:" + tbl.Rows[0]["ID"].ToString() + " , Can Not Load!");

 

                        ///
                        ///
                        ///

 

                        #region

 

                        //MessageBox.Show("ID:" + tbl.Rows[0]["ID"].ToString());
                        try
                        {
                            int intOffset = 0;
                            intOffset = Convert.ToInt32(byPicture[1].ToString()) + Convert.ToInt32(byPicture[2].ToString()) + 3;

                            MemoryStream ms = new MemoryStream();
                            ms.Write(byPicture, intOffset, byPicture.Length - intOffset);

                            Bitmap bm;
                            bm = new Bitmap(ms);
                            pictureBox1.Image = bm;
                        }
                        catch (Exception)
                        {
                            //MessageBox.Show("ID:" + tbl.Rows[0]["ID"].ToString());
                            try
                            {
                                MemoryStream ms = new MemoryStream();
                                ms.Write(byPicture, 78, byPicture.Length - 78);
                                Bitmap bm;
                                bm = new Bitmap(ms);
                                pictureBox1.Image = bm;
                            }
                            catch (Exception)
                            {
                                //MessageBox.Show("ID:" + tbl.Rows[0]["ID"].ToString());
                                try
                                {
                                    MemoryStream ms = new MemoryStream();
                                    ms.Write(byPicture, 76, byPicture.Length - 76);
                                    Bitmap bm;
                                    bm = new Bitmap(ms);
                                    pictureBox1.Image = bm;
                                }
                                catch (Exception)
                                {
                                    //MessageBox.Show("ID:" + tbl.Rows[0]["ID"].ToString());

                                    try
                                    {
                                        MemoryStream s = new MemoryStream(byPicture);
                                        pictureBox1.Image = Image.FromStream(s);
                                    }
                                    catch (Exception)
                                    {
                                        MessageBox.Show("Logo ID:" + tbl.Rows[0]["ID"].ToString() + " , Can Not Load!");
                                    }
                                }
                            }
                        }
                        #endregion

                        #endregion
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值