Display a Photo Gallery using Asp.Net and SQL - asp.net

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

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

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

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.

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.

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

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]

Related

Display an image from a Byte Array from SQL into an ASPX page that is currently routed

I have the most convoluted and headache of a way to attempt to load am image on a page. I am using VB.Net, my page is routed from a Routes.XML document, I have a SQL database with images stored as varbinary(MAX) fields, I need to load said images from the page into an control, and everything that i use to use will not work with the routing.
If you need to see code examples let me know, but I was wondering if anyone knew of a way to display an image from bytes in this fashion.
use this code:
Dim Ph As Byte()
Ph = DirectCast(YourImageFromDB), Byte())
Dim img As Image = Nothing
Dim stream = New MemoryStream(Ph)
img = Image.FromStream(stream)
Ended up having a friend show me how he routed with images. What basically happens is that I needed to create an ASHX file to create the image, and put the full path from the image.ashx that I create as the URL I need. Worked like a charm. First time ever working with a handler file so i had no idea how to use it at first.

Fetch the image url from SQL Server database to display an image

I have a column in my database that stores the url of the user's profile picture. In the home page' page_load event handler, I need to determine the image url, but I don't know how to do that using SQL commands.
From the images below, how can I give the query result to the image's ImageUrl property?
You can use Server.mappath("~/profilepicture/profilepicture.jpg")
OdbcDataReader reader=new OdbcDataReader();
reader=command.executereader;
Profilephotobutton.imageur=server.mappath(" & reader.item(profilepicture) & ");
hope this helps

Winform browser control "attachment" button

We have a winform app that has a browser control on it. Previously these files (always very small 10kb etc.) were stored at a unc location. We would generate some html and load the html into the browser. If we wanted to make one of these small files available we would include in the HTML an anchor tag () WHen the html was displayed in the browser control so would be the link. The user could click on the link and the file save as dialog would appear.
We are now storing these files in the db as varbinary and thus there is no longer a physical location for the anchor tag to point to. I have several thoughts but would like the members of SO who are way smarter than me to chime in.
Option 1 in my mind would be to have an image button, anchor tag, something in the html to click on. I would handle the "onclick" either in javascript or as a postback. This seems doable for my level of knowledge EXCEPT I do not know how to get the byte[] to translate into the save as dialog for the user....do I render it to disk first?
The other idea I had was to have a button that is NOT in the browser control. This button would be hidden / visible if the biz rules said to show a file. Clicking on the button would then generate the byte[] which is easily turned into a file and the save as shown dialog shown in the winform app.
So any thought or all together different suggestions welcome
TIA
JB
I understand that you are in control of the ASP.NET web page shown in the windows forms web browser control so you can edit that page and build it the way you want.
if that is true, behavior in hosted web browser or in normal IE session is the same and I would suggest to create a bunch of hyper links or buttons in the asp.net web form page each one which a specific ID, like the ID of the file to download. then you can create an handler or a button_click event handler where you get the byte[] of the file by the clicked button/link associated file Id, or from query string if you initiated an handler call, and then you start streaming down to the browser the file content, the browser will do all what is required for you.
for example, just as a starting point, a bit of code taken from here: http://social.msdn.microsoft.com/Forums/en-US/silverlightnet/thread/d6a5087f-43b1-4782-95f1-d1376130d2c8
shows you a possible way to do this from a page load, the trick is that the call to GetDocument gets the proper file content for you (in this case from the query string, imagine like if we are inside an handler processing method) and returns a class DocumentInfo which contains the bytes. you do nor need this DocumentInfo, you can just have a method which returns byte[] by File Id for example...
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string queryString = this.Request.QueryString.ToString();
if (string.IsNullOrEmpty(queryString)) return;
DocumentInfo documentInfo = GetDocument(queryString);
if (!documentInfo.HasValue) return;
Response.ClearHeaders();
Response.ClearContent();
Response.AppendHeader("Content-Length", documentInfo.Value.Content.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment; filename=Test.doc");
Response.BinaryWrite(documentInfo.Value.Content);
Response.End();
}
}

How to create ASPX Page Dynamically

i have two controls on asp.net page , textbox and button and i want to write new page into textbox then when i hit the button i want to create new asp.net web page with textbox text. what do you want me to prefer to do this? is there any step by step tutorial or any code you have done before? thanks for asnwers.
ok more detail about what i want.
textbox control text is "contact" and i click my button control. button take textbox text "contact" and create new web form page which name is "contact.aspx and its code page contact.cs" . thats it. just create new web form page under root directory with button click.
maybe there is a single code line like
Page pg =new Page();
pg.create();
You can use File.WriteAllText to write the contents of the textbox to any location that the application pool user has permissions to write to.
From your edit it appears that you are looking for a CMS/wiki kind of functionality. There is nothing like that built into the .NET framework.
I suggest you look for wiki software.
No need to worry about big codes etc
You can create it with 2 simple steps explained in my blog
Create one empty page then copy it using Files concept. Save it as what ever name you want.
Try to change the content inside newly created page to Run smoothly
Please visit my blog for example solution
Click here for Solution Post
You can use the Page.ParseControl method.
Beware, it does not support everything you can do with a real ASPX or ASHX file, but it works for simple things. An example is available here: Using Page.ParseControl to add new control from Control Tag
There is very simple way to do that. It is working on principe to Copy one your Page, you should put Page with controls that you want to use on your other pages. So first you need using System.IO; then you need this code
protected void Button1_Click(object sender, EventArgs e)
{
try
{
File.Copy(Server.MapPath("") + "\\Submit.aspx", (Server.MapPath("") + "\\" + TextBox1.Text + ".aspx"));
File.Copy(Server.MapPath("") + "\\Submit.aspx.cs", (Server.MapPath("")+ "\\" + TextBox1.Text + ".aspx.cs"));
Response.Redirect(TextBox1.Text + ".aspx");
}
catch
{
Response.Write("<script>window.alert('This page is taken. Please change name!')</script>");
}
}
As you can see, The page you are coping is called Submit, and new Name of your Page is called like textbox1.text.
I hope that this helped you and anyone else.
enter link description here
Before some time I saw a logical question in a web site in this a person give the problem as like this
“I have one page called First.aspx in my web application, in this page; I have a simple asp.net button control. And I want is, on the click event of that button control, create (render) a new dynamic "page" that can be opened in a new windows or tab”
.
Here we give An Example here to creating a new .aspx page at run time. We also give the option to give page name. The user can give as he/she like. Like Google blogging we make new page at runtime.The new page is not in the website, this page create needs to be created at runtime and needs to be dynamic.

Image handler page

I created an image handler page which retrieves the physical path of an image on the local machine and then using filestream, resizes and displays it - using a integer (record id) passed as querystring.
What is happening is that, when the routine in pageload cannot find an image relating to the record id, it displays random images (from other records).
This only ocurrs when the related record id has no image. The routine assigns a default image if no image exists for the record, but instead of displaying the default image, the page is displaying images from other records. If I keep refreshing the page it displays different images from other records.
In my page load event, before doing anything else I have put:
Response.Cache.SetCacheability(HttpCacheability.NoCache)
But this has not changed the behaviour.
Any help appreciated.
thanks,
KS
You could handle the case in code so when no image is present, you display a static blank image instead.

Resources