473,403 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Display new image on web page

14
Hello,

I'm uploading images to a folder and I want to display each new image on a web page every time it is saved in the folder.

I can display the initial image, which is already in the folder, and I can also detect when a new image is saved into the folder but I don't know how to display the new images.

I'm new to web development. Can somebody help me?

Here's the code:

Expand|Select|Wrap|Line Numbers
  1. public partial class _Default : System.Web.UI.Page
  2. {
  3.     string DirectoryPath = "C:\\Users\\Desktop\\PhotoUpload\\Uploads\\";
  4.  
  5.     protected void Page_Load(object sender, EventArgs e)
  6.     {
  7.         FileSystemWatcher watcher = new FileSystemWatcher();
  8.  
  9.         try
  10.         {
  11.             watcher.Path = DirectoryPath;
  12.             watcher.Created += new FileSystemEventHandler(watcher_Created);
  13.             watcher.EnableRaisingEvents = true;
  14.  
  15.             Image image = Image.FromFile(DirectoryPath + "inicial.jpg");
  16.             MemoryStream ms = new MemoryStream();
  17.             image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
  18.             byte[] im = ms.ToArray();
  19.  
  20.             Context.Response.ContentType = "Image/JPG";
  21.             Context.Response.BinaryWrite(im);
  22.         }
  23.         catch (Exception ex)
  24.         {
  25.         }
  26.     }
  27.  
  28.     void watcher_Created(object sender, FileSystemEventArgs e)
  29.     {
  30.         Console.WriteLine("File Created: Name: " + e.Name);
  31.  
  32.         try
  33.         {
  34.           //How to display new image?  
  35.         }
  36.         catch (Exception ex)
  37.         {
  38.         }
  39.     }
  40. }
Mar 10 '10 #1
5 3355
Frinavale
9,735 Expert Mod 8TB
You need to periodically post back to the server to check if there are any new photos in the folder. The FileSystemWatcher is going to be useless to you. Even though it can watch the folder, it only does so on the server. The web browser needs to know when there is a new image available and the only way that it can do that is to preform a postback.

Instead of using the FileSystemWatcher, use a DirectoryInfo to figure out what files are in the folder.

You will have to do this every postback, so it would be a good idea to place the code that displays the images into the method that Page Load or Page PreRender event.

It would also be a good idea to use a Repeater or a GridView to display the images. Place an ASP.NET Image control in the Repeater template (or GridView ItemTemplate) ...then create the DataSource for the control based on the files in the folder and bind the image URLs appropriately.

Cheers!

-Frinny
Mar 10 '10 #2
RuiT
14
I'm sorry for my lack of experience or if I didn't explain correctly but I don't understand how FileSystemWatcher is going to be useless to me. You said that it only watches the folder on the server. But I want the solution to be server-side only. The only thing user needs to do is to open the URL on a web browser. The server should replace the old image by the new image every time it is saved on the folder and the page should refresh without any action being performed by the user, so we can watch the sequence of images like a slideshow. Is this possible? Thanks for your time!
Mar 11 '10 #3
Frinavale
9,735 Expert Mod 8TB
You are developing an ASP.NET application.

ASP.NET works in a stateless, disconnected environment.

The following must take place for it to work (in this order):
  • The web browser makes a request to the server for an ASPX page
  • The web server executes the code for the ASPX page.
  • The ASPX page (code) dynamically creates HTML
  • The HTML is sent to the browser
  • The browser receives the HTML and displays it

There is no connection maintained between the web browser and the server (this is what I mean when I say ASP.NET works in a stateless, disconnected environment). Once the HTML is sent to the browser all of the Objects used to generate the HTML are destroyed (hence the stateless part of my statement). The server waits for the next page request...all of the Objects required for the execution are recreated and then destroyed one the response has been sent to the browser.

So if there's no connection open between the server and the browser, how is the browser supposed to know when the server has new images to display?

The browser must make a request to the server to do this.

This can be done automatically without the end user noticing it happening. There is an Ajax control called a Timer that will "tick" and each tick will cause a postback to the server. If you place your images in an UpdatePanel then this will be very smooth and the end user doesn't have to know what's going on (unless you want to indicate something's happening).

As for the slide show thing...you can have the web browser download all of the images but use CSS to hide them (display:none)...and then use JavaScript to show them after some time has passed....

Or you could use the Timer control to post to the server and have the server return the new image....

-Frinny
Mar 11 '10 #4
RuiT
14
Ok, I understand now. Thanks for your clear explanation.
Mar 11 '10 #5
Frinavale
9,735 Expert Mod 8TB
If you have any questions regarding the implementation feel free to ask and I'll give you a hand.

Happy coding!

-Frinny
Mar 11 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Tyrone Slothrop | last post by:
I am coding a site which involves uploading images. All of the PHP and display is OK but the client wants to be able to display the image thumbnail on the upload page and show the full image on...
5
by: Martin Moser | last post by:
Does somebody knows, if theres a way to display any file (tiff, doc, pdf, .....) "inline" on an asp.net site? What I want to do is, to display a file, which is stored in the DB, and some...
5
by: Peter Lapic | last post by:
I have to create a image web service that when it receives an imageid parameter it will return a gif image from a file that has been stored on the server. The client will be an asp.net web page...
4
by: DoomedLung | last post by:
I have been developing an image gallery which needed to be dynamic as possible. so I have placed the gallery in a div plus a div containing the large image to be displayed. It works by changing...
3
by: briand | last post by:
I am writting a control that paints an image to a graphic (created from a bmp). I want to be able to display the painted image directly without having to save to file and link to the saved image....
3
by: c676228 | last post by:
Hi everyone, I have a piece of code in sales.aspx.vb like this: Protected WithEvents Message As System.Web.UI.WebControls.Label Try ... ChartImage.ImageUrl = "ChartGenerator.aspx?" + DataStr +...
6
by: yk | last post by:
Hi, Is it a technique available in html/javascript in order to display same image many many times on a same page? Because of a large page loading I am looking for a way not to have same...
1
by: pkamil83 | last post by:
Hello I've got a table on SQL Server 2005 with columns: ID int Text XML Image varbinary(max) In this table I'm having SVG images. All I have to do is to display those images on ASP.NET 2...
1
by: amritranjan | last post by:
How to retrive image file from MS access database and display this in another JSPpage -------------------------------------------------------------------------------- This is my Jsp code for...
3
by: =?Utf-8?B?QmlsbHkgWmhhbmc=?= | last post by:
I have a asp.net app, in the page there is a scan activex which could scan and save a jpg file in client harddisk. How could we access and display this jpg file on the fly using js in the client...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.