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);
}
}