1. 程式人生 > >讀excel圖片到資料庫和上傳圖片到資料庫

讀excel圖片到資料庫和上傳圖片到資料庫

//引用Excel名稱空間

using Excel;

//......

//下面從test.xls中的2,2格複製圖片到剪貼簿,然後從剪貼簿讀取圖片並顯示到pictureBox1中。

private void btnGetImageFromExcel_Click(object sender, EventArgs e)

{

//初始化excel物件

Excel.Application excel = new Excel.Application();

//開啟xls檔案(注意:後面的引數都用Type.Missing填充,表示使用引數的預設值)

excel.Workbooks.Open(@"D:/Documents and Settings/xrwang/

桌面/test.xls", System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);

//選定到第2行第2列所在的單元格

Range r = (Range)excel.Cells[2, 2];

r.Select();

//將單元格複製到剪貼簿中

r.CopyPicture(XlPictureAppearance.xlScreen, XlCopyPictureFormat.xlBitmap);

//退出excel

excel.Quit();

//判斷剪貼簿中是否存在圖片,如果存在,則將圖片顯示到pictureBox1

if (Clipboard.ContainsImage())

{

Image image=Clipboard.GetImage();

pictureBox1.Image = image;

}

}

Image轉成Byte[]的問題把我這個入門級的Asp.Net使用者快要搞暈了

,費了很大的勁,查了很多的文章,終於搞定了,把方法寫出來,希望能幫助遇到同樣問題的人.

程式目的是做一個可以上傳照片,然後生成縮圖,把縮圖儲存到資料庫image欄位中,供顯示照片時動態顯示輸出.

我把關鍵程式碼列出來:

//下面是根據上傳的檔案,產生縮圖,然後儲存到資料庫image表中(為將問題簡單化,這裡把表的欄位做了減少).

string fstr = FileUpload1.PostedFile.FileName; ;//上傳的檔名
        string fmime = FileUpload1.PostedFile.ContentType;//
檔案的MIME型別
        string sql;
        int fsize = FileUpload1.PostedFile.ContentLength; //
檔案大小
       
        //
利用Bitmap類獲得影象的尺寸
        Bitmap bmp = new Bitmap(fstr);
        int imgH = bmp.Height;
        int imgW = bmp.Width;
        //
縮圖保持原圖片比例,計算出大小
        float simg1,simg2;
        if (imgW > imgH)
        {
            //
橫向圖片
            simg1 = 100;
            simg2 = simg1/imgW * imgH;
        }
        else
        {
            //
縱向圖片
            simg2 = 100;
            simg1 = simg2/imgH * imgW;
        }

        //生成縮圖
        System.Drawing.Image smallimg = bmp.GetThumbnailImage((int)simg1, (int)simg2, null, IntPtr.Zero);
        MemoryStream ms = new MemoryStream();
        smallimg.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);//
將影象儲存到記憶體流中
        byte[] data ={ }; //
建立一個位元組陣列,用於儲存影象資料
        data=ms.ToArray();//
把流的資料儲存到陣列
        fsize = (int)ms.Length;//
流資料大小

        //生成SQL語句,因為使用了位元組陣列,必須使用帶引數的SQL,否則無法執行,@1就是引數
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString;
        conn.Open();
        sql = "insert into [image] (itype,imagedata,isize) values('" + fmime + "',@1," + fsize + ")";
        SqlCommand cmd = new SqlCommand(sql, conn);
        cmd.Parameters.Add(new SqlParameter("@1", SqlDbType.Image));
        cmd.Parameters["@1"].Value = data;
        int count = cmd.ExecuteNonQuery();

//下面是動態顯示影象資料

SqlConnection conn = new SqlConnection();
        conn.ConnectionString = ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString;
        conn.Open();
        string sql = "select * from [image] where id=1"; //
這裡id使用的是1,實際可以根據需要傳一個值
        SqlCommand cmd = new SqlCommand(sql, conn);
        cmd.CommandTimeout = 60;
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            Response.ContentType = dr["itype"].ToString().Trim();
            //
輸出圖片檔案二進位制資料
            Response.OutputStream.Write((byte[])dr["imagedata"], 0, (int)dr["isize"]);
            Response.End();
        }

/*補充說明:

*連線資料庫部分的程式碼可以做成一個類,供多次引用,而不必要每次重複地寫出來.

*如果要將動態顯示出來的影象用一個Image控制元件顯示出來的話,可以把動態顯示資料的程式碼做成一個單獨的檔案例

*"showimage.aspx",然後將Imagesrc地址寫為"showimage.aspx",如果需要動態顯示多個影象,可以給"showimage.aspx"傳引數進

*,改變id的值即可.