I get full picture when I try to put picture in image control
I h开发者_如何学Pythonave this code to insert a picture from database into an ImageControl on my ASP.NET form:
strConnString = "server=" + Server + ";database=" + DataBase + ";UID=" + UID + ";password=" + PASS + ";";
SqlConnection MyConnection = new SqlConnection(strConnString);
SqlCommand MyCommand = new SqlCommand("SELECT Pic FROM MEN WHERE ID=5", MyConnection);
MyConnection.Open();
SqlDataReader MyReader = MyCommand.ExecuteReader();
if (MyReader.Read())
{
byte[] m_MyImage = (byte[])MyReader["Pic"];
Response.BinaryWrite(m_MyImage);
}
And I have this ImageControl
on the form:
<asp:Image Width="88" Height="100" Runat="server" ID="m_Image" NAME="m_Image" />
but when I run the code, I see the picture big on the screen and not in my ImageControl
.
i also put this in the form_load:
m_Image.ImageUrl = "MyPhoneBook.aspx?m_Image";
my control is: m_Image
my control ID is: m_Image
my namespace is MyPhoneBook
but still dont work
What am I doing wrong ?
Thanks
You are writing the image data directly to the response stream:
Response.BinaryWrite(m_MyImage);
This results in the browser just getting the image, not any HTML.
Additionally, the image control takes a URL to an image, not the binary content, so this approach wouldn't work.
To do what I think you want to achieve (get images from the DB to a page without doing any disk IO), you can do the following:
- Use the code you have above in a page of its own that will only serve images. Alternatively write an HTTP Handler for images that will do this.
- Set the image control
ImageUrl
to this page.
You can take this further by passing an image id to the image page/handler on the query string (something like image.aspx?img_id=1
)
How are you referencing the image on the page? The image control markup provided doesn't seem to have any kind of image reference in it, and the code provided it just writing the image directly to the response. What is the resulting HTML?
If I were to guess based on this code, I'd say that you're clobbering the HTML response with the binary image response, so you see only the latter. The two would need to be separated into their own requests/responses.
You need to make a separate ASHX handler that renders the image the way you're doing now, then set the <asp:Image>
's URL to point to that ASHX.
精彩评论