Image handler page - asp.net

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.

Related

Captured Image & show it next page in Silverlight asp.net

I have an Web-application in which I capture Image using EdgeCamShots Sample Application
Saving Silverlight 4 Webcam
how can I use this capture image to be used in Next Page without saving Image.
A view model object reference (created from the application's MainPage) would be an object that both pages would have access to. Pass a reference to the pages on navigation changed event. That view model might make a good container for the stream and would give both pages access to the stream.
You can simply pass the stream (mentioned in the code example on the link you provided) to the next page using REST (adding the variable in the URL e.g. http://mysite/page2.aspx?stream=dstStream).
From there on, you can use the variable to show your picture.

Best way to do paging in a .NET page

Hope you are having a great day.
I have a page where I let users browse related items (photo albums) posted by other users.
I show the album thumbnail, title, author, ratings views and category for each albums.
Now the number of related items can get large say 500 photo albums is related to a album user is viewing. I just show first 20 and drop a link saying 'View All'
Once the user clicks View All I take him to a page where I display 50 items per page.
Option 1: using a repeater\grid control
When user clicks a page I get the right items (using sql) and bind the result to the grid
The page is refreshed and user sees new page. So one big request and user sees all thumbs.
Option 2:
when user clicks the pager I use Ajax to get the thumbnail file name, title, rating, author etc.
Then I build the grid manually and set the src attribute of element using javascript
Then I append the resulting grid to a div. User sees the new grid without page refresh.
My concerns:
I have the thumbnails and all image files in file system (not database)
In second approach javascript will send 50 separate request to web server to get the image files. This will cause a lot of requests on the web server. Large concurrent users will flood the server with image file requests.
What is the best & efficient way to do paging and storing user files?
Is my app going to die by putting the images in file system since the app should handle millions of photos?
If you aren't already using one, a CDN (content delivery network, like Amazon S3 etc) will help (so you can download from multiple places concurrently).
Also, instead of requesting one image, request a page from the server. Let the server decide how many images to return.
You could try some css sprite techniques.
Definetely you shouldn't use Option 1 since it would take a lot of time for the page to load
Aldso the second alternative id not so good either.
You can use your GridView/ListView control but use server side paging to get the data. This way you could load only the information that is needed per page.
You can see how to implement a stored procedure for server side paging here : http://www.sqlteam.com/article/server-side-paging-using-sql-server-2005
Then you can bind the individual page the PageIndexChanging event handler of your Gridview
so only the page that is needed is actually loaded.

An aspx page is called sometime and some time not

I am using a generateimage.aspx page which is used as an image source for an image.
When this page is called I am passing a querystring and then using a Session["abc"] var whose value is being returned as a jpg image.
The page is called as
Context.Items["display"]="img src=" + Context.Items["Ch_BaseSvr"] +
"GenerateImage.aspx?text=P"
Now the problem is that sometimes this page is called and sometimes not.
Thus, when the page is not called the image value that is being returned is that which is assigned to the var in previous Session.
Please let me know what might be the reason that the page is called sometimes and sometimes not.
Sounds like it may be cached. Try setting
Response.Cache.SetCacheability(HttpCacheability.NoCache)
on your generateimage.aspx page.

how to change cached picture from javascript

I have a asp image control. ImageUrl="images/avator.jpg". I'm changing this picture on the server and then setting imageUrl same as previous url on the client. but image don't changed. When i click refresh button, image changed.
How to change cached image won't changing url?
You can add random number as a paramentr to avoid caching.
<img src="http://someurl.com/avatar.png?version=0.01.135";
The best way is to add number of application build version. So every time you change something in aplpication everything will load again.

Display a Photo Gallery using Asp.Net and SQL

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]

Resources