SQL Server中的Image資料類型的操作

來源:互聯網
上載者:User

準備工作,在庫Im_Test中建立一張表Im_Info,此表中有兩個欄位,分別為Pr_Id (INT),Pr_Info (IMAGE),用來儲存圖形編號及圖形資訊。其文法如下:
CREATE TEALE Im_Info (
Pr_Id INT NULL ,
Pr_Info IMAGE NULL

第一步: 往表中插入一條記錄,並初始化PR_INFO欄位。其文法如下:
INSERT INTO Im_Info VALUES (1 ,0xFFFFFFFF)

第二步往表中寫入圖形資訊。其文法如下:
DECLARE @@ptrval varbinary(16)
SELECT @@ptrval = TEXTPTR(Pr_Info)
FROM Im_Info
WHERE Pr_Id = 1
WRITETEXT Im_Text.Im_Info
@@ptrval 0x624fd543fd…..

其中0x624fd543fd….. 為圖形的十六進位資料,可以通過C 、Java等工具獲得。

 

注意在寫入圖形資訊前必須先將此資料庫的 'select into/bulkcopy' 屬性設定為 True ,其文法如下:
use master
exec sp_dboption Im_Test ,'select into/bulkcopy' ,True

 

C#讀取Image資料類型:

 

(1)控制台應用程式下示範插入圖片

public void InsertIMG()
        {

            //將需要儲存的圖片讀取為資料流
            FileStream fs = new FileStream(@"E:\c.jpg", FileMode.Open,FileAccess.Read);
            Byte[] btye2 = new byte[fs.Length];
            fs.Read(btye2 , 0, Convert.ToInt32(fs.Length));
            fs.Close();
           
            using (SqlConnection conn = new SqlConnection(sqlconnstr))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = "insert into T_Img(imgfile) values(@imgfile)";
                SqlParameter par = new SqlParameter("@imgfile", SqlDbType.Image);
                par.Value = bt;
                cmd.Parameters.Add(par);

                int t=(int)(cmd.ExecuteNonQuery());
                if (t > 0)
                {
                    Console.WriteLine("插入成功");
                }
                conn.Close();
            }
        }

(2)控制台應用程式下讀出並產生圖片到物理位置

public void Read()
        {
            byte[] MyData = new byte[0];
            using (SqlConnection conn = new SqlConnection(sqlconnstr))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = "select * from T_img";
                SqlDataReader sdr = cmd.ExecuteReader();
                sdr.Read();
                MyData = (byte[])sdr["ImgFile"];//讀取第一個圖片的位流
                int ArraySize= MyData.GetUpperBound(0);//獲得資料庫中儲存的位流數組的維度上限,用作讀取流的上限

                FileStream fs = new FileStream(@"c:\00.jpg", FileMode.OpenOrCreate, FileAccess.Write);
                fs.Write(MyData, 0, ArraySize);
                fs.Close();   //-- 寫入到c:\00.jpg。
                conn.Close();
                Console.WriteLine("讀取成功");//查看硬碟上的檔案
            }
        }

(3)Web下picshow.aspx頁將圖片讀取出來並寫入到瀏覽器上呈現

    public void Read()
    {
        byte[] MyData = new byte[0];
        using (SqlConnection conn = new SqlConnection(sqlconnstr))
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "select * from T_img";
            SqlDataReader sdr = cmd.ExecuteReader();
            sdr.Read();
            MyData = (byte[])sdr["ImgFile"];
            Response.ContentType = "image/gif";
            Response.BinaryWrite(MyData);
            conn.Close();
            Response.Write("讀取成功");
        }

(4)在web中可以如上picshow.aspx頁面讀取並顯示圖片,而真正引用該圖片時如下樣本

<img src="picshow.aspx" width="500" height="300" />
 (5)Winform下將圖片寫入到sql資料庫image類型欄位中的方法和以上方法基本一致,僅區別於可以利用多個對話方塊來協助選取儲存圖片等,各個屬性可以方便的利用上

(6)Winform下讀取圖片在picturebox控制項中顯示出來

方法一:利用MemoryStream 和System.Drawing.Image

public void Read()
        {
            byte[] MyData = new byte[0];
            using (SqlConnection conn = new SqlConnection(sqlconnstr))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = "select * from T_img";
                SqlDataReader sdr = cmd.ExecuteReader();
                sdr.Read();
                MyData = (byte[])sdr["ImgFile"];

                MemoryStream mystream = new MemoryStream(MyData);
                //用指定的資料流來建立一個image圖片
                System.Drawing.Image img = System.Drawing.Image.FromStream(mystream, true);
               
                System.Windows.Forms.PictureBox picbox = new PictureBox();
                picbox.Image = img;
                picbox.Left = 30;
                picbox.Top = 80;
                picbox.Width = 800;
                picbox.Height = 500;
                this.Controls.Add(picbox);

                mystream.Close();
                conn.Close();
            }
        }

   方法二:將流直接讀取成圖片並寫入到物理位置,然後再行利用該圖片呈現

void Read()
        {
            using (SqlConnection conn = new SqlConnection(sqlconnstr))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = "select * from T_img";
                SqlDataReader sdr = cmd.ExecuteReader();
                sdr.Read();

                byte[] Image_img = (byte[])sdr["ImgFile"];
                if (Image_img.Length == 0)
                {
                    return;
                }
                int filelength = Image_img.Length;
                string imageName = "1.jpg";
                string myUrl = Environment.CurrentDirectory + "\\" + imageName;
                FileStream fs = new FileStream(myUrl, FileMode.OpenOrCreate,FileAccess.Write);
                BinaryWriter BW = new BinaryWriter(fs);
                BW.BaseStream.Write(Image_img, 0, filelength);
                BW.Flush();
                BW.Close();
                System.Windows.Forms.PictureBox picbox = new PictureBox();
               
                //為picbox添加圖片方法一
                //picbox.ImageLocation = myUrl;
                //picbox.Width = 800;
                //picbox.Height = 300;

 

                //為picbox添加圖片方法二
                Bitmap bitmap = new Bitmap(myUrl);
                picbox.Width = 100;//bitmap.Width;
                picbox.Height = 80;//bitmap.Height;
                picbox.Image = (Image)bitmap;
                picbox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
                picbox.Left = 20;
                picbox.Top = 30;

                this.Controls.Add(picbox);
                conn.Close();
               
            }
        }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.