2007年10月6日星期六

C# 存取数据库中的图像 | 站长资讯

Ò»¡¢Êý¾Ý¿âÖеÄͼÏñ´æÈ¡·½·¨

1. ¶ÁÈ¡imageÀàÐ͵ÄÊý¾Ý

¶ÁÈ¡imageÀàÐÍÊý¾ÝµÄ·½·¨¿É·ÖΪÒÔϼ¸²½£º

1) ÏÈʹÓÃÎÞ·ûºÅ×Ö½ÚÊý×é´æ·ÅÊý¾Ý¿â¶ÔÓ¦µÄÊý¾Ý¼¯ÖбíµÄimageÀàÐÍ×ֶεÄÖµ¡£ÀýÈ磺

byte[] bytes= (byte[]) imageÀàÐÍ×Ö¶ÎÖµ

2) ʹÓÃMemoryStreamÀ࣬¸ÃÀà´´½¨Ö§³Ö´æ´¢ÇøÎªÄÚ´æµÄÁ÷¡£¼´MemoryStreamÀà´´½¨µÄÁ÷ÒÔÄÚ´æ¶ø²»ÊÇ´ÅÅÌ»òÍøÂçÁ¬½Ó×÷Ϊ֧³Ö´æ´¢Çø¡£Æä¹¹Ô캯ÊýΪ£º

public MemoryStream(byte[] buffer);

3) ʹÓÃBitmapÀ࣬¸ÃÀà·â×°ÁËGDI+λͼ£¬´ËλͼÓÉͼÐÎͼÏñ¼°ÆäÊôÐÔµÄÏñËØÊý¾Ý×é³É¡£Bitmap¶ÔÏóÊÇÓÃÓÚ´¦ÀíÓÉÏñËØÊý¾Ý¶¨ÒåµÄͼÏñµÄ¶ÔÏ󡣯乹Ô캯ÊýΪ£º

public Bitmap(Stream stream);

4) ÔÚ´°ÌåÖÐÀûÓÃPictureBox¿Ø¼þ¶ÔÏóÏÔʾͼÏñ¡£

2. ±£´æimageÀàÐ͵ÄÊý¾Ý

±£´æimageÀàÐÍÊý¾ÝµÄ·½·¨Ò²·ÖΪÒÔϼ¸²½£º

1) ʹÓÃStreamÀ࣬Ê×ÏÈ´ÓͼÏñÎļþÖлñÈ¡Á÷¶ÔÏó£¬ÔÙÀûÓøÃÀàµÄRead·½·¨´ÓͼÏñÎļþÖжÁÈ¡¶þ½øÖÆÊý¾Ý´æÈë×Ö½ÚÊý×éÖС£Read·½·¨Îª£º

public abstract int Read([In, Out] byte[] buffer, int offset, int count);

2) ½«×Ö½ÚÊý×éÖеÄÖµ´æÈëÊý¾Ý¿â¶ÔÓ¦µÄÊý¾Ý¼¯ÖбíµÄimage×ֶΡ£¸ñʽΪ£º

imageÀàÐÍ×Ö¶Î= bytes;

3) ¸üÐÂÊý¾Ý¿â£¬¾Í¿ÉÒÔÍê³É±£´æÍ¼ÏñÊý¾ÝµÄ¹¦ÄÜ¡£

¶þ¡¢ Êý¾Ý¿âÖеÄͼÏñ´æÈ¡Ê¾Àý

ÏÂÃæÍ¨¹ýÒ»¸öÀý×Ó˵Ã÷ÈçºÎ´æÈ¡SQL ServerÊý¾Ý¿âÖеÄͼÏñ¡£

£¨1£© ´´½¨Ò»¸öWindowsÓ¦ÓóÌÐò£¬Éè¼Æ´°Ìå½çÃæÈçͼËùʾ¡£


¢Æ Ìí¼ÓÃû³Æ¿Õ¼äÒýÓÃ

using System.Data;

using System.Data.SqlClient;

using System.IO;

¢Ç Ìí¼Ó×Ö¶ÎÉùÃ÷

private string connString=%26quot;server=localhost; integrated security=sspi; database=pubs%26quot;;

SqlConnection conn;

SqlDataAdapter adapter;

DataSet dataset;

¢È ÔÚ¹¹Ô캯ÊýÖÐÌí¼Ó´úÂë

string sqlstr=%26quot;select * from pub_info%26quot;;

conn=new SqlConnection(connString);

adapter=new SqlDataAdapter(sqlstr,conn);

SqlCommandBuilder builder=new SqlCommandBuilder(adapter);

adapter.UpdateCommand=builder.GetUpdateCommand();

dataset=new DataSet();

adapter.Fill(dataset,%26quot;pub_info%26quot;);

//½«text1Box1µÄTextÊôÐ԰󶨵½datasetÖеÄpub_info±íµÄpr_info×Ö¶Î

this.textBox1.DataBindings.Add(new Binding(%26quot;Text%26quot;,dataset,%26quot;pub_info.pr_info%26quot;));

for(int i=0;i%26lt;dataset.Tables[0].Rows.Count;i++)

{

this.listBox1.Items.Add(dataset.Tables[0].Rows[i][0]);

}

¢É Ìí¼Óµ÷Óõķ½·¨

private void ShowImage()

{

byte[] bytes= (byte[])dataset.Tables[0].Rows[this.listBox1.SelectedIndex][1];

MemoryStream memStream=new MemoryStream(bytes);

try

{

Bitmap myImage = new Bitmap(memStream);

this.pictureBox1.Image= myImage;

}

catch

{

this.pictureBox1.Image=null;

}

}

¢Ê Ìí¼Ó¡°¸ü»»Í¼Æ¬¡±µÄClickʼþ´úÂë

private void buttonUpdateImage_Click(object sender, System.EventArgs e)

{

OpenFileDialog openFileDialog1=new OpenFileDialog();

openFileDialog1.ShowDialog();

if (openFileDialog1.FileName.Trim()!=%26quot;%26quot;)

{

Stream myStream = openFileDialog1.OpenFile();

int length=(int)myStream.Length;

byte[] bytes=new byte[length];

myStream.Read(bytes,0,length);

myStream.Close();

dataset.Tables[0].Rows[this.listBox1.SelectedIndex][1] =bytes;

ShowImage();

}

}

¢Ë Ìí¼Ó¡°ÒƳýͼƬ¡±µÄClickʼþ´úÂë

private void buttonMoveImage_Click(object sender, System.EventArgs e)

{

byte[] bytes= System.Text.Encoding.Unicode.GetBytes(%26quot;%26quot;);

dataset.Tables[0].Rows[this.listBox1.SelectedIndex][1]=

bytes;

ShowImage();

}

¢Ì Ìí¼Ó¡°±£´æ¸ü¸Ä¡±µÄClickʼþ´úÂë

private void buttonSave_Click(object sender, System.EventArgs e)

{

adapter.Update(dataset,%26quot;pub_info%26quot;);

MessageBox.Show(%26quot;±£´æ³É¹¦%26quot;);

}

¢Í Ìí¼ÓlistBox1µÄSelectedIndexChangedʼþ´úÂë

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)

{

ShowImage();

this.BindingContext[dataset,%26quot;pub_info%26quot;].Position

=this.listBox1.SelectedIndex;

}

(10) ÔËÐС£

¿ÉÒÔ¸ü»»Í¼Æ¬£¬Ò²¿ÉÒÔÖ±½ÓÐÞ¸ÄtextBox1ÖеÄÄÚÈÝ¡£

没有评论: