wfdoublext 发表于 2013-1-27 04:46:17

读写图片

<%@ Import Namespace="System.Data.SqlClient" %> 
<%@ Import Namespace="System.Drawing" %> 
<%@ Import Namespace="System.Drawing.Imaging" %> 
<%@ Import Namespace="System.IO" %> 
<script language="C#" runat="server"> 
void Page_load(object Sender, EventArgs E) {
        MemoryStream stream = new MemoryStream();
        SqlConnection connection;
        connection = new SqlConnection("server=localhost;database=pubs;uid=sa;pwd=");
        try {
            connection.Open();
            SqlCommand command;
            command = new SqlCommand("select logo from pub_info where pub_id=\’0736\’", connection);
            byte[] image;
            image = command.ExecuteScalar();
            stream.Write(image, 0, image.Length);
            bitmap imgbitmap;
            imgbitmap = new Bitmap(stream);
            Response.ContentType = "image/gif";
            imgbitmap.Save(Response.OutputStream, ImageFormat.Gif);
        }
        finally {
            connection.Close();
            stream.Clse();
        }
    }

</script>
 
MemoryStream stream = new MemoryStream(); string constr = System.Configuration.ConfigurationSettings.AppSettings["SQLConnectionString"];string slct="select imageSrc from dbo.image_excise where id=1";SqlConnection scon = new SqlConnection(constr);//string slct = "select ThemeFileData from SPS_M_ThemeFile where ThemeFileCD=" + imgid;SqlCommand scmd = new SqlCommand(slct,scon);scon.Open();SqlDataReader sdr = scmd.ExecuteReader();Response.ContentType = "image/jpeg";if(sdr.Read()){if(((byte[])sdr.GetValue(0)).Length != 0){Response.BinaryWrite((byte[])sdr.GetValue(0));return;}}try{FileStream fs = new FileStream(Server.MapPath("Images/Image_NoPicture.gif"),FileMode.Open,FileAccess.Read);byte[] mydata = new byte;int Length = Convert.ToInt32(fs.Length);fs.Read(mydata,0,Length);fs.Close();this.Response.OutputStream.Write(mydata,0,Length);}catch(Exception ex){return;}sdr.Close();scon.Close();this.Response.End(); 

HtmlInputFile[] tempfile = new HtmlInputFile;tempfile =this.imageFile1;tempfile =this.imageFile2;tempfile =this.imageFile3;//16011for(int i=0;i<3;i++){byte[] buff = new byte.PostedFile.ContentLength];tempfile.PostedFile.InputStream.Read(buff,0,tempfile.PostedFile.ContentLength);try{System.Drawing.Image img = System.Drawing.Image.FromStream(new System.IO.MemoryStream(buff));}catch{return ;}SqlParameter[] spr = new SqlParameter;spr=new SqlParameter("@imageTitle",SqlDbType.NVarChar);spr.Value="test"+i.ToString();spr = new SqlParameter("@imageSrc",SqlDbType.Image);spr.Value = buff;SqlHelper.ExecuteNonQuery(SqlHelper.dsn,CommandType.StoredProcedure,"imageInsert",spr);} 
页: [1]
查看完整版本: 读写图片