473,796 Members | 2,512 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Display new image on web page

14 New Member
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 3378
Frinavale
9,735 Recognized Expert Moderator Expert
You need to periodically post back to the server to check if there are any new photos in the folder. The FileSystemWatch er 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 FileSystemWatch er, 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 New Member
I'm sorry for my lack of experience or if I didn't explain correctly but I don't understand how FileSystemWatch er 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 Recognized Expert Moderator Expert
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 New Member
Ok, I understand now. Thanks for your clear explanation.
Mar 11 '10 #5
Frinavale
9,735 Recognized Expert Moderator Expert
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
6774
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 mouseOver instead of the popup I coded. The section of code below works perfectly in Firefox but not at all in IE (why am I not surprised?). The page is nearly 100% dynamic, so this is extracted from the source of a test page. The code in...
5
1991
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 information around it. If you know a way, or a third party control, please let me know. tia Martin
5
5044
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 that calls the web service to render a vertical strip of images. After doing some research I am unable to find some vb.net code that can assist in what I want to achieve. The closest thing I found was
4
2369
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 the display property of the gallery div to 'none' and the largeImage div to 'block' which has an affect of switching pages but obviously saving time developing a single page for each large Image, but (cliche') when I refresh the page the with the...
3
1605
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. I have been told this can now be done in .NET 2.0. Is this so? How? -- Thanks Brian
3
2251
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 + "&ChartType=" + drpChartType.SelectedItem.Value.ToLower() + "&Print=" + printVersion.ToString() ... Catch e As Exception
6
9385
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 image many times in a page load, but rather once. Yefim
1
2990
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 web site (C#),
1
3380
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 image upload in database: -----------Upload.jsp---------------- <html> <head> <title>Account Details </title>
3
2847
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 IE? Thanks, -Billy zhang
0
10452
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10221
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10169
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.