在ASP.NET中进行图像转换的字节

时间:2013-08-06 03:32:46

标签: c# asp.net

我有以下代码可以很好地检索图像,但有时不清楚图像是否意味着为所有其他图像加载了以前的图像,如果发生这种情况,则不会加载特定对象的正确图像。 / p>

处理程序中的代码:

public void ProcessRequest(HttpContext context)
{
    string ID;

    if (context.Request.QueryString["id"] != null)
    {
        ID = Convert.ToString(context.Request.QueryString["id"]);
    }

    else
    {
        throw new NotImplementedException();

    }

    context.Response.ContentType = "image/jpeg";

    //context.Response.Buffer = false;
    Stream strm = ShowEmpImage(ID);

    //byte[] buffer = new byte[4096];
    byte[] buffer = new byte[4095];
    int byteSeq = strm.Read(buffer, 0, buffer.Length);

    while ((byteSeq > 0))
    {
        context.Response.OutputStream.Write(buffer, 0, byteSeq);
        //context.Response.OutputStream.Write(buffer, 0, byteSeq);
        byteSeq = strm.Read(buffer, 0, 4095);
    }
    context.Response.Flush();
}

private Stream ShowEmpImage(string ID)
{
    DB_Connection();
    string sql = "SELECT Visitor_Image FROM Visitor_Master WHERE Visitor_Id = '" + ID + "'";
    SqlCommand cmd = new SqlCommand(sql, con);
    cmd.CommandType = CommandType.Text;
    Object img = cmd.ExecuteScalar();
    try
    {
        return new MemoryStream((byte[])img);
    }
    catch
    {
        return null;
    }
    finally
    {
        con.Close();
    }
}

0 个答案:

没有答案