コード例 #1
0
        public static void read(PictureBox pictureBox)
        {
            //  strid 主键字段名  strzp 照片字段名  strtb 数据库表名  strpath  存放照片的本地路径
            byte[]           img    = new byte[0];
            string           sqlStr = string.Format(@"SELECT id,picture FROM Picture_Test where id = 1");
            OracleConnection conn   = new OracleConnection(OracleDaoHelper.conn_str);
            OracleDataReader rs     = OracleDaoHelper.getDR(sqlStr, out conn);

            while (rs.Read())
            {
                string idStr = rs["id"].ToString();

                /*
                 * string idStr = rs["id"].ToString();
                 * //img = (byte[])(rs["zp"]);
                 * img = System.Text.Encoding.GetEncoding(-0).GetBytes(rs["zp"].ToString());
                 * System.IO.File.WriteAllBytes("C:\\" + idStr + ".jpg", img);
                 */
                if (rs["picture"] != DBNull.Value)
                {                                                        //照片字段里有值才能进到方法体显示图片,否则清空pb{
                    MemoryStream ms        = new MemoryStream((byte[])rs["picture"]);
                    Image        imageBlob = Image.FromStream(ms, true); //用流创建Image
                    pictureBox.Image = imageBlob;
                    pictureBox.Image.Save("E:\\1.jpg", pictureBox.Image.RawFormat);
                }
                else  //照片字段里没值,清空pb
                {
                    pictureBox.Image = null;
                }
            }
            rs.Close();
            rs.Dispose();
            conn.Close();
            conn.Dispose();
        }
コード例 #2
0
        public static void getPictureByProductName(string productName, PictureBox pictureBox)
        {
            Image imageBlob = null;

            //  strid 主键字段名  strzp 照片字段名  strtb 数据库表名  strpath  存放照片的本地路径
            byte[]           img    = new byte[0];
            string           sqlStr = string.Format(@"SELECT picture FROM Products_Picture where product_name = '{0}'", productName);
            OracleConnection conn   = new OracleConnection(OracleDaoHelper.conn_str);
            OracleDataReader rs     = OracleDaoHelper.getDR(sqlStr, out conn);

            if (!rs.HasRows)
            {
                pictureBox.Image = null;
                rs.Close();
                rs.Dispose();
                conn.Close();
                conn.Dispose();
                return;
            }
            while (rs.Read())
            {
                /*
                 * string idStr = rs["id"].ToString();
                 * //img = (byte[])(rs["zp"]);
                 * img = System.Text.Encoding.GetEncoding(-0).GetBytes(rs["zp"].ToString());
                 * System.IO.File.WriteAllBytes("C:\\" + idStr + ".jpg", img);
                 */
                if (rs["picture"] != DBNull.Value)
                {                                                  //照片字段里有值才能进到方法体显示图片,否则清空pb{
                    MemoryStream ms = new MemoryStream((byte[])rs["picture"]);
                    imageBlob        = Image.FromStream(ms, true); //用流创建Image
                    pictureBox.Image = imageBlob;
                    try
                    {
                        pictureBox.Image.Save(string.Format(@"{0}\{1}.jpg",
                                                            Application.StartupPath + "\\pictures",
                                                            StringHelper.removeBlank(productName)
                                                            ),
                                              pictureBox.Image.RawFormat);;
                    }
                    catch (Exception ex) {
                        MessageBox.Show(ex.ToString());
                    }
                }
                else
                {//照片字段里没值,清空pb
                    pictureBox.Image = null;
                }
            }
            rs.Close();
            rs.Dispose();
            conn.Close();
            conn.Dispose();
        }