Page.Response.BufferOutput = true;
Page.Response.ContentEncoding = Encoding.Default;
Page.Response.Charset = "GB2312";
Page.Response.ContentType = "image/jpeg";// 这里如果是图片是jpeg或jpg类型的这么写,如果是bmp的写成"image/bmp",gif的为"image/gif"
//下面是输出图片过程(这是我原来写过的一部分代码)
private void ShowImage(string id,string TableName) //ID表示图片在TableName表中的ID
{
int len = 4096;
string conn = "";//连接字符串
SqlConnection sqlconn = new SqlConnection(conn);
sqlconn.Open();
string sText="declare @id varchar(20);";
sText +="set @id='"+id+"';";
sText +="select @image=textptr(image),@datalen=datalength(image) from "+TableName+" where id=@id";
SqlCommand sqlcmd=new SqlCommand(sText,sqlconn);
SqlParameter ptrParam= sqlcmd.Parameters.Add("@image",SqlDbType.Binary,16);
ptrParam.Direction =ParameterDirection.Output;
SqlParameter datalenParam=sqlcmd.Parameters.Add("@datalen",SqlDbType.Int);
datalenParam.Direction=ParameterDirection.Output;
sqlcmd.ExecuteNonQuery();
int datalen=int.Parse(datalenParam.Value.ToString());
datalen -=3;//去除掉'0x0'
if(datalen<0)
{
Response.Write("<script>alert('文档数据不存在,请核查是否由于错误操作删除了文档数据!')</script>");
return;
}
if(datalen<len)
sText ="readtext "+TableName+".image @point @offset "+datalen;
else
sText ="readtext "+TableName+".image @point @offset "+len;
sqlcmd =new SqlCommand(sText,sqlconn);
SqlParameter ptrParam1=sqlcmd.Parameters.Add("@point",SqlDbType.Binary,16);
ptrParam1.Value =ptrParam.Value;
SqlParameter offParam=sqlcmd.Parameters.Add("@offset",SqlDbType.Int);
offParam.Value =0;
Stream outStream=Page.Response.OutputStream;
BinaryWriter bw=new BinaryWriter(outStream,Encoding.Default);
byte[] buffer=null;
if(datalen<len)
buffer =new byte[datalen];
else
buffer =new byte[len];
long retval=0;
int offset_ctr = 0;
SqlDataReader dr=sqlcmd.ExecuteReader(CommandBehavior.SequentialAccess);
try
{
dr.Read();
if(datalen<len)
retval =dr.GetBytes(0,0,buffer,0,datalen);
else
retval =dr.GetBytes(0,0,buffer,0,len);
while(retval>0)
{
if(datalen>2*len)
{
bw.Write(buffer);
bw.Flush();
offset_ctr +=len;
dr.Close();
offParam.Value =offset_ctr;
dr=sqlcmd.ExecuteReader(CommandBehavior.SequentialAccess);
dr.Read();
retval =dr.GetBytes(0,0,buffer,0,len);
datalen-=len;
}
else if(datalen >len)
{
bw.Write(buffer);
bw.Flush();
datalen -=len;
offset_ctr +=len;
buffer =new byte[datalen];
dr.Close();
sText ="readtext "+TableName+".image @point "+offset_ctr+" "+datalen;
sqlcmd=new SqlCommand(sText,sqlconn);
SqlParameter ptr=sqlcmd.Parameters.Add("@point",SqlDbType.Binary,16);
ptr.Value =ptrParam1.Value ;
dr=sqlcmd.ExecuteReader(CommandBehavior.SequentialAccess);
dr.Read();
retval =dr.GetBytes(0,0,buffer,0,datalen);
}
else
{
bw.Write(buffer);
bw.Flush();
offset_ctr +=datalen;
datalen =0;
buffer =new byte[datalen];
dr.Close();
sText ="readtext "+TableName+".image @point "+offset_ctr+" "+datalen;
sqlcmd=new SqlCommand(sText,sqlconn);
SqlParameter ptr=sqlcmd.Parameters.Add("@point",SqlDbType.Binary,16);
ptr.Value =ptrParam1.Value ;
offset_ctr +=datalen;
dr=sqlcmd.ExecuteReader(CommandBehavior.SequentialAccess);
dr.Read();
retval =dr.GetBytes(0,0,buffer,0,datalen);
}
}
Response.Flush();
bw.Close();
outStream.Close();
}
catch(Exception ex)
{
string ss=ex.Message ;
}
Response.End();
}
//注:上面的代码是对我原来的代码进行更改之后给你的,你在调试一下吧
Page.Response.ContentEncoding = Encoding.Default;
Page.Response.Charset = "GB2312";
Page.Response.ContentType = "image/jpeg";// 这里如果是图片是jpeg或jpg类型的这么写,如果是bmp的写成"image/bmp",gif的为"image/gif"
//下面是输出图片过程(这是我原来写过的一部分代码)
private void ShowImage(string id,string TableName) //ID表示图片在TableName表中的ID
{
int len = 4096;
string conn = "";//连接字符串
SqlConnection sqlconn = new SqlConnection(conn);
sqlconn.Open();
string sText="declare @id varchar(20);";
sText +="set @id='"+id+"';";
sText +="select @image=textptr(image),@datalen=datalength(image) from "+TableName+" where id=@id";
SqlCommand sqlcmd=new SqlCommand(sText,sqlconn);
SqlParameter ptrParam= sqlcmd.Parameters.Add("@image",SqlDbType.Binary,16);
ptrParam.Direction =ParameterDirection.Output;
SqlParameter datalenParam=sqlcmd.Parameters.Add("@datalen",SqlDbType.Int);
datalenParam.Direction=ParameterDirection.Output;
sqlcmd.ExecuteNonQuery();
int datalen=int.Parse(datalenParam.Value.ToString());
datalen -=3;//去除掉'0x0'
if(datalen<0)
{
Response.Write("<script>alert('文档数据不存在,请核查是否由于错误操作删除了文档数据!')</script>");
return;
}
if(datalen<len)
sText ="readtext "+TableName+".image @point @offset "+datalen;
else
sText ="readtext "+TableName+".image @point @offset "+len;
sqlcmd =new SqlCommand(sText,sqlconn);
SqlParameter ptrParam1=sqlcmd.Parameters.Add("@point",SqlDbType.Binary,16);
ptrParam1.Value =ptrParam.Value;
SqlParameter offParam=sqlcmd.Parameters.Add("@offset",SqlDbType.Int);
offParam.Value =0;
Stream outStream=Page.Response.OutputStream;
BinaryWriter bw=new BinaryWriter(outStream,Encoding.Default);
byte[] buffer=null;
if(datalen<len)
buffer =new byte[datalen];
else
buffer =new byte[len];
long retval=0;
int offset_ctr = 0;
SqlDataReader dr=sqlcmd.ExecuteReader(CommandBehavior.SequentialAccess);
try
{
dr.Read();
if(datalen<len)
retval =dr.GetBytes(0,0,buffer,0,datalen);
else
retval =dr.GetBytes(0,0,buffer,0,len);
while(retval>0)
{
if(datalen>2*len)
{
bw.Write(buffer);
bw.Flush();
offset_ctr +=len;
dr.Close();
offParam.Value =offset_ctr;
dr=sqlcmd.ExecuteReader(CommandBehavior.SequentialAccess);
dr.Read();
retval =dr.GetBytes(0,0,buffer,0,len);
datalen-=len;
}
else if(datalen >len)
{
bw.Write(buffer);
bw.Flush();
datalen -=len;
offset_ctr +=len;
buffer =new byte[datalen];
dr.Close();
sText ="readtext "+TableName+".image @point "+offset_ctr+" "+datalen;
sqlcmd=new SqlCommand(sText,sqlconn);
SqlParameter ptr=sqlcmd.Parameters.Add("@point",SqlDbType.Binary,16);
ptr.Value =ptrParam1.Value ;
dr=sqlcmd.ExecuteReader(CommandBehavior.SequentialAccess);
dr.Read();
retval =dr.GetBytes(0,0,buffer,0,datalen);
}
else
{
bw.Write(buffer);
bw.Flush();
offset_ctr +=datalen;
datalen =0;
buffer =new byte[datalen];
dr.Close();
sText ="readtext "+TableName+".image @point "+offset_ctr+" "+datalen;
sqlcmd=new SqlCommand(sText,sqlconn);
SqlParameter ptr=sqlcmd.Parameters.Add("@point",SqlDbType.Binary,16);
ptr.Value =ptrParam1.Value ;
offset_ctr +=datalen;
dr=sqlcmd.ExecuteReader(CommandBehavior.SequentialAccess);
dr.Read();
retval =dr.GetBytes(0,0,buffer,0,datalen);
}
}
Response.Flush();
bw.Close();
outStream.Close();
}
catch(Exception ex)
{
string ss=ex.Message ;
}
Response.End();
}
//注:上面的代码是对我原来的代码进行更改之后给你的,你在调试一下吧