views:

3070

answers:

7

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();

}
A: 

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.

Mladen Mihajlovic
A: 

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.

asp316
+1  A: 

"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".

Brian
+1 For using an ashx. There are numerous examples on SO of image handlers as well.
Zhaph - Ben Duguid
+1  A: 

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

Eduardo Molteni
A: 

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

A: 

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]

A: 

evren yıldırım

this example not clearly for me if you give a sql database and end explain mybe better for me if you send me a example I 'am very be happy again thats all

evren