如何显示在asp.net中的图像控制MySQL的团块图像? [英] How to display mysql blob image in asp.net image control?
问题描述
我知道显示Windows窗体mysql的团块图像的方式。
I known the way to display the mysql blob image in Windows Forms.
try
{
MySqlConnection connection = new MySqlConnection(hp.myConnStr);
MySqlCommand command = connection.CreateCommand();
MySqlDataReader Reader;
command.CommandText = "select logo from mcs_institude where id = 1";
connection.Open();
Reader = command.ExecuteReader();
while (Reader.Read())
{
pictureBox1.Image = new Bitmap(new MemoryStream((byte[])Reader.GetValue(0)));
}
connection.Close();
}
catch(Exception ex)
{
MessageBox.Show("Error in Get_ImageFormDB"+ ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
但现在我做一个asp.net项目。在此图像中没有图像属性。
But now i doing a asp.net project. In this image not have the image property,.
command = connection.CreateCommand();
command.CommandText = "Select FO_Roomdet_Image from fo_roomtype where FO_Roomdet_Id=1";
connection.Open();
Reader = command.ExecuteReader();
while (Reader.Read())
{
Image1.ImageUrl = new MemoryStream((byte[])Reader.GetValue(0));
}
connection.Close();
当我尝试这个在asp.net中,它通过一个错误。
When i try this in asp.net, it through an error.
错误1无法隐式转换类型'System.IO.MemoryStream来
字符串
Error 1 Cannot implicitly convert type 'System.IO.MemoryStream' to 'string'
我该如何解决这个问题。并获得MySQL的团块图像刚刚在asp.net图像控件中显示。
How Can i Solve this issue. and get mysql blob image just display in asp.net image control.
请帮助我。
推荐答案
什么你正在试图做没有意义:在浏览器试图显示图像需要知道从哪里下载
What you're trying to do doesn't make sense: the browser trying to display your image will need to know where to download it from.
您应该设置一个特殊的aspx页面,专门用于图像生成,例如GetImage.aspx。
You should setup a special aspx page, dedicated to image generation, for example GetImage.aspx.
然后,您的主页将有指向该图像生成页面IMG HTML标签:
Your main page will then have img html tags pointing to this image generation page:
<img src="/GetImage.aspx?id=your_image_id"/>
然后,里面GetImage.aspx,您可以根据其ID(获取来自URL参数)检索数据库的形象。在code会是这样的:
Then, inside GetImage.aspx, you retrieve the image from DB according to its id (fetched from URL parameter). The code would be something like:
command = connection.CreateCommand();
command.CommandText = "Select FO_Roomdet_Image from fo_roomtype where FO_Roomdet_Id=1"; // or dynamically fetch id with Request.QueryString and properly escape it
connection.Open();
Reader = command.ExecuteReader();
while (Reader.Read())
{
Response.ContentType = "image/jpeg"; // if your image is a jpeg of course
Response.BinaryWrite((byte[])Reader.GetValue(0));
}
connection.Close();
这篇关于如何显示在asp.net中的图像控制MySQL的团块图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!