Creating a network album with ASP. NET to save good time

Source: Internet
Author: User

In our daily life, digital photo devices are already common. We often take some photos for reference. As digital photos increase, we often need to manage these photos well, for better reference. Now there are many electronic albums on the Internet, which can be well implemented. Can we create our own albums by ourselves? Of course. In this article, we will use asp.net to create a simple online album to add our photos to favorites.

First, let's take a look at the functions of this album. In this album, we must first put the photos taken in advance into a directory, and then we can use the "previous, next" link on the Internet to view them one by one.

The following describes how to obtain images in a folder. We can use the DirectoryInfo class in the System. IO namespace. Pass the path of the folder as a parameter to the constructor of the class and declare an instance of the DirectoryInfo class. The DirectoryInfo class has a GetFiles () method, which returns an array of FileInfo objects. Each FileInfo instance contains the file details in the specified path. The following code snippet illustrates the process:

Reference content is as follows:
Sub Page_Load (sender as Object, e as EventArgs)
'Get list of images
Dim dirInfo as New DirectoryInfo (Server. MapPath (""))
Dim images () as FileInfo = FilterForImages (dirInfo. GetFiles ())
...

End Sub

Use Server. mappath to obtain the path of the current directory, and dirinfo. getfiles () will return all files in this directory. Because we only need to see JPG, BMP, GIF, and other image files in the photo album, we can implement it through a program and only load these types of files, this is achieved through a custom process FilterForImages, which will only return images in the specified folder. The Code is as follows:

Reference content is as follows:

Function FilterForImages (images () as FileInfo ()
Dim newImages as New ArrayList (images. Length)

Dim I as Integer
For I = 0 to images. Length-1
If Path. GetExtension (images (I). Name) = ". jpg" OrElse _
Path. GetExtension (images (I). Name) = ". jpeg" OrElse _
Path. GetExtension (images (I). Name) = ". png" OrElse _
Path. GetExtension (images (I). Name) = ". gif" then
NewImages. Add (images (I ))
End If
Next

Return CType (newImages. ToArray (GetType (FileInfo), FileInfo ())
End Function

In this process, the passed FileInfo parameter array is traversed to break the suffix of the file in the folder. If it belongs to an image file, it is added to the newimages array, and return in the form of arraylist.

Next, let's take a look at how to display each image and display it with "previous and next. To know which images are currently browsed, you can use the method of passing parameters. First, add an image control and text box to the form. The program code is as follows:

Reference content is as follows:

Sub Page_Load (sender as Object, e as EventArgs)
...

'Dim imgIndex as Integer = 0
If Not Request. QueryString ("N") is Nothing AndAlso _
IsNumeric (Request. QueryString ("N") then
ImgIndex = CInt (Request. QueryString ("N "))
End If
 
CurrentImgTitle. Text = "You are Viewing :"&_
Path. GetFileNameWithoutExtension (images (imgIndex). Name )&_
"(" & ImgIndex + 1 & "of" & images. Length &")"
CurrentImg. ImageUrl = Path. GetFileName (images (imgIndex). Name)
...
End Sub

HTML code

Reference content is as follows:
<Asp: Label runat = "server" id = "currentImgTitle"/> <br/>
<Asp: Image runat = "server" id = "currentImg"/>

In the above Code, the imgindex variable is used to indicate the number of images currently browsed. At the beginning, N = 0, the first variable in the images array is obtained, that is, the first image. Each time you read the variable value, you can know which image you are browsing.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.