简体   繁体   English

如何显示从ASP.NET中的数据库中获取的具有固定大小的图像

[英]How to display an Image with a fixed size, fetched from the Database in ASP.NET

My Table in the Database contains a column with a type : image. 数据库中的我的表包含一个类型为:image的列。

I have managed to upload the image to the database. 我已设法将图像上传到数据库。

But almost all tutorials out there, shows us how to give the Image Controller a URL which contains the name of the page, plus a parameter that has the ID of the selected row in the Database 但几乎所有教程都向我们展示了如何为Image Controller提供一个包含页面名称的URL,以及一个具有数据库中所选行ID的参数

Example: 例:

//Database codes here... (query..etc..)
SqlDataReader myReader = myCommand.ExecuteReader();
if (myReader.HasRows == true)
{
    myReader.Read();
    Response.ContentType = "image/jpg";
    Response.BinaryWrite((byte[])myReader[0]);
    Image1.ImageUrl="User.aspx?imgid=1";
}

So when I click on a specific button which should fetch this image from the database, then give that URL to the image, the image opens in full screen.. But I don't want that, let's say I have empty space in my page for an Image of 50px width and 50px height, how can I show the image inside this space? 所以,当我点击一个特定的按钮,该按钮应从数据库中获取此图像,然后将该URL提供给图像,图像将全屏打开..但我不想这样,让我说我的页面中有空格对于50像素宽和50像素高的图像,如何在此空间内显示图像? and keeping all the other contents? 并保留所有其他内容? just like a user page, a profile page. 就像用户页面,个人资料页面一样。

You can control the image's display using an <img> tag. 您可以使用<img>标签控制图像的显示。 So say your ASP.NET page that gets and displays the image is called ShowImage.aspx?ID=xxx . 所以说你获取并显示图像的ASP.NET页面叫做ShowImage.aspx?ID=xxx

Create a new page named ShowImageAt50px.aspx and in that page add the following markup: 创建一个名为ShowImageAt50px.aspx的新页面,并在该页面中添加以下标记:

<img src="ShowImage.aspx?ID=xxx" style="width:50px;" alt="" />

Now when you visit ShowImageAt50px.aspx it will show the image constrained at 50 pixels wide. 现在,当您访问ShowImageAt50px.aspx ,它将显示约50像素宽的图像。

It's usually far easier and less error-prone to store your images in the file-system and refer to your images in the database using the filenames of your images. 将图像存储在文件系统中并使用图像的文件名引用数据库中的图像通常会更容易,也更不容易出错。

I've never come across a good reason to store images in a database. 我从未想过将图像存储在数据库中的充分理由。

Also, when you want to edit your image that's stored in the database, then you are going to have to export it out of your database and then reimport it back in. 此外,当您想要编辑存储在数据库中的图像时,您将不得不将其从数据库中导出,然后重新导入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM