就是说假如我的一个图片被分成了三次读取,第一个块很正常,很清楚,但第二个快就不清楚了,失真了,第三个快就更厉害了
代码中我使用copyto把byte[]拼装起来的
我怀疑是不是16进制前面的0x收的影响string strID = Request.QueryString["id"].ToString();
SqlConnection conn = new SqlConnection("Server=localhost;UID=sa;Password=;database=DBTEST;Connection Reset=FALSE;");

conn.Open(); SqlCommand cmd = new SqlCommand("SELECT datalength(photo) from employees where employeeID=" + strID, conn);
int intLength = (int)cmd.ExecuteScalar(); SqlDataAdapter da2 = new SqlDataAdapter("SELECT photo from employees where employeeID=" + strID,conn);
DataSet ds2 = new DataSet();
da2.Fill(ds2);
byte[] b2 = (byte[])ds2.Tables[0].Rows[0][0];



byte[] b = new byte[intLength];
bool flag = true;
string strSQL = "Declare @ptrval varbinary(16);SELECT @ptrval = TEXTPTR(photo) from employees where employeeID=" + strID;
int i = 0;
int index = 0;

while(flag)
{
//count++;
//System.Threading.Thread.Sleep(0);
int l = 3200000;
int lt = l;
int j = i;
if(j+lt+1>=intLength)
{
l = intLength-j;
flag = false;
}
string strSQL2 = ";READTEXT employees.photo @ptrval " + i.ToString() + " " + l.ToString();

i=i+l+1;
SqlDataAdapter da = new SqlDataAdapter(strSQL + strSQL2,conn);
DataSet ds = new DataSet();
da.Fill(ds);
byte[] ptr = (byte[])ds.Tables[0].Rows[0]["photo"];
ptr.CopyTo(b,index);
index = index + ptr.Length;
//ptr = null;
}
Response.Clear();
Response.ContentType = "image/jpeg"; Response.BinaryWrite(b);



conn.Close();

解决方案 »

  1.   

    FYI:
    FileInfo objFileInfo = new FileInfo(@"c:\1.jpg");
            byte[] abContent = new byte[objFileInfo.Length];
            FileStream objFileStream = objFileInfo.OpenRead();
            
            Response.ContentType = "image/jpeg";
            int iRead;
            while ((iRead=objFileStream.Read(abContent, 0, (int)(objFileInfo.Length / 3))) > 0)
            {
                byte[] abBuffer = new byte[iRead];            for (int i = 0; i < iRead; i++)
                    abBuffer[i] = abContent[i];
                
                Response.BinaryWrite(abBuffer);            
            }