使用 Asp.Net 和 SQL 显示照片库
我最近将照片添加到我的 SQL 数据库中,并使用 Asp:Image 将它们显示在 *.aspx 页面上。 该控件的 ImageUrl 存储在单独的 *.aspx 页面中。 它非常适合个人资料图片。
我手头有一个新问题。 我需要每个用户都能够拥有自己的照片库页面。 我希望将照片存储在sql数据库中。 存储照片并不困难。 问题是显示照片。 我希望照片以缩略图网格的方式存储,当用户单击照片时,它应该在单独的页面上显示照片。
做这个的最好方式是什么。 显然不是使用Asp:Image。 我很好奇是否应该使用 Gridview。 如果是这样,我该怎么做?它们应该是存储在数据库中的缩略图大小吗?
单击图片后,单击其他页面的外观,以便显示正确的图像。 我认为通过 url 发送 photoId 是不正确的。
下面是我用来显示个人资料图片的页面中的代码。
protected void Page_Load(对象发送者,EventArgs e) {
string sql = "SELECT [ProfileImage] FROM [UserProfile] WHERE [UserId] = '" + User.Identity.Name.ToString() + "'";
string strCon = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["SocialSiteConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(strCon);
SqlCommand comm = new SqlCommand(sql, conn);
conn.Open();
Response.ContentType = "image/jpeg";
Response.BinaryWrite((byte[])comm.ExecuteScalar());
conn.Close();
}
I have recently added photos to my SQL database and have displayed them on an *.aspx page using Asp:Image. The ImageUrl for this control stored in a separate *.aspx page. It works great for profile pictures.
I have a new issue at hand. I need each user to be able to have their own photo gallery page. I want the photos to be stored in the sql database. Storing the photos is not difficult. The issue is displaying the photos. I want the photos to be stored in a thumbnail grid fashion, when the user clicks on the photo, it should bring up the photo on a separate page.
What is the best way to do this. Obviously it is not to use Asp:Image. I am curious if I should use a Gridview. If so, how do I do that and should their be a thumbnail size stored in the database for this?
Once the picture is click on how does the other page look so that it displays the correct image. I would think it is not correct to send the photoId through the url.
Below is code from the page I use to display profile pictures.
protected void Page_Load(object sender, EventArgs e)
{
string sql = "SELECT [ProfileImage] FROM [UserProfile] WHERE [UserId] = '" + User.Identity.Name.ToString() + "'";
string strCon = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["SocialSiteConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(strCon);
SqlCommand comm = new SqlCommand(sql, conn);
conn.Open();
Response.ContentType = "image/jpeg";
Response.BinaryWrite((byte[])comm.ExecuteScalar());
conn.Close();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
“此控件的 ImageUrl 存储在单独的 *.aspx 页面中。它非常适合个人资料图片。” - 为什么不使用通用 ASP.NET 处理程序 (.ashx) 而不是 .aspx 页面? 它们比 .aspx 更轻一些。 只需搜索“ASP.NET Generic Handler”,您就会找到许多示例。 它基本上是您现在在 .aspx 页面后面的代码,但没有所有页面初始化/渲染开销。
“我希望照片以缩略图网格的方式存储,当用户单击照片时,它应该在单独的页面上显示照片。” - 我认为任何支持项目元素模板化的 ASP.NET 可重复控件(例如 ASP.NET 3.5 中的 DataGrid、GridView、Repeater、ListView 等)都应该可以满足您的要求。 只需根据缩略图设置图像或 asp:Image 的高度和宽度即可。 将您在 HTML 锚点中使用的任何标记包含到以“全尺寸”显示图像的页面的 href 中。
"The ImageUrl for this control stored in a separate *.aspx page. It works great for profile pictures." - rather than an .aspx page, why not a generic ASP.NET handler (.ashx) instead? They are a little more lightweight than an .aspx. Just search "ASP.NET Generic Handler" and you'll find a number of samples. It's basically the code you have now behind your .aspx page, but without all the page initialization/rendering overhead.
"I want the photos to be stored in a thumbnail grid fashion, when the user clicks on the photo, it should bring up the photo on a separate page." - I would think any ASP.NET repeatable control that supports templating of the item element (such as DataGrid, GridView, Repeater, ListView in ASP.NET 3.5, etc) should do the trick for you here. Just set the image or asp:Image height and width as appropriate for your thumbnails. Wrap which ever tag you use in an HTML anchor with an href to your page that displays the image at "full size".
让我们解构您的问题:
它们应该是为此存储在数据库中的缩略图大小吗?
是的。 第一次请求照片缩略图时生成缩略图并将其缓存在数据库中以供将来访问
显然不是使用Asp:Image
使用Asp:Image没有问题。 你会没事的
我很好奇我是否应该使用 Gridview
也许 Repeater 更好,但是如果您熟悉 gridview,您会觉得没问题
我认为通过 url 发送 photoId 是不正确的。
这是正确的,但您应该检查照片是否属于当前用户(不要信任该 URL。
正在生成缩略图
您将了解到 .net 生成的调整大小的图像质量很差。 您应该使用一些 GDI 功夫来获得高质量的图像。
请参阅这篇文章 http://codebetter.com/blogs/brendan.tompkins/archive/2004/01/26/use-gdi-to-save-crystal-clear-gif-images-with- net.aspx 了解更多信息
Let's deconstruct your question:
Should their be a thumbnail size stored in the database for this?
Yes. Generate the thumbnail the first time the photo thumbnail is requested and cache it in the DB for future access
Obviously it is not to use Asp:Image
There is no problem using Asp:Image. You will be fine with it
I am curious if I should use a Gridview
Maybe a Repeater is better, but you will be fine with a gridview if you are familiar with it
I would think it is not correct to send the photoId through the url.
It is correct, but you should check if the photo belongs to the current user (don't trust the URL.
Generating the thumbnail
You will learn that the resized image generated by .net are poor quality. You should use some GDI kung-fu to get quality pictures.
Refer to this post http://codebetter.com/blogs/brendan.tompkins/archive/2004/01/26/use-gdi-to-save-crystal-clear-gif-images-with-net.aspx to learn more
您可以尝试这个
或此。 他们应该帮助您开始网格方面的工作。 正确发送 url 中的 imageid。 查看会话变量或隐藏控件并发布表单并在下一页上读入。
我会查看 gridview 并结合使用模板列和 gridview 中的 html 图像标签。 在上传时创建缩略图并将其存储在数据库中。 从那里,您可以轻松检索它。 这种方式显示图像的速度更快,并且在显示时无需进行缩略图转换。
You can try this
or this. They should get you started on the grid side. Correct on sending the imageid from the url. Look at a session variable or hidden control and post the form and read in on the next page.
I would look at a gridview and use a template column in combination with the html image tag in the gridview . Create the thumbnail as they upload it and store them both in the database. From there, you can easily retrieve it. This way displaying the image is quicker and there is no thumbnail conversions necessary at display time.
好吧,您将有一个仅显示照片的页面(如上面所示),但您的标准会有所不同。 您不会使用
WHERE [UserId] = '" + User.Identity.Name
但必须发送数据库中照片 ID 的查询字符串 id。然后当您想要将图片放入在您的 aspx 页面上,您必须放置一个图像 html 标记
如何在页面上放置 html 图像标记并不重要。
Well you would have a page for just displaying a photo (like you have above) but your criteria would be different. You would not use
WHERE [UserId] = '" + User.Identity.Name
but would have to send a query string id for the id of the photo in the database.Then when you want the pic in your aspx page you would have to put an image html tag
<image src="photo.aspx?id=2" />
How you put the html image tags on the page is not important.
RbmBinaryImage 控件将帮助您直接显示数据库中的图像。 您可以将 Image 字段直接绑定到 ImageContent 属性,还可以指定是否希望显示为缩略图并提供缩略图大小。
要获取它,请访问 http://www.ramymostafa.com/?p=187
The RbmBinaryImage control will help you display images directly from your database. You could bind the Image field directly to the ImageContent property, also you could specify whether you want the display to be as a thumbnail or not and provide the thumbnail size.
to get it go to http://www.ramymostafa.com/?p=187
如果您不想重新发明轮子,您可以将像 Gallery Server Pro (www.galleryserverpro.com) 这样的开源图库集成到您的站点中。 它是用 C# / ASP.NET 编写的,可以完成您想要的所有操作。
Roger
[免责声明] 我是 Gallery Server Pro 的创建者和首席开发人员 [/免责声明]
If you don't want to reinvent the wheel you can integrate an open source gallery like Gallery Server Pro (www.galleryserverpro.com) into your site. It is written in C# / ASP.NET and does all the things you want.
Roger
[disclaimer] I am the Creator and Lead Developer of Gallery Server Pro [/disclaimer]