Dim writer As New XmlTextWriter(Server.MapPath("~/myXml.xml"), Encoding.UTF8)
writer.WriteStartDocument()
writer.WriteStartElement("gallery")
writer.WriteStartElement("menu")
Dim dirInfo As New DirectoryInfo(MapPath("~/image_gallery/images"))
For Each filInfo As FileInfo In dirInfo.GetFiles()
writer.WriteStartElement("option")
writer.WriteStartElement("imageSize")
Dim tempImage As New Bitmap(filInfo.FullName)
writer.WriteAttributeString("imageWidth", tempImage.Width.ToString())
writer.WriteAttributeString("imageHeight", tempImage.Height.ToString())
tempImage.Dispose()
writer.WriteEndElement()
writer.WriteStartElement("image")
writer.WriteValue("image_gallery/images/" & filInfo.Name)
writer.WriteEndElement()
writer.WriteStartElement("thumb")
writer.WriteValue("image_gallery/images/thumb/" & filInfo.Name)
writer.WriteEndElement()
writer.WriteEndElement()
Next
writer.WriteEndElement()
writer.WriteEndElement()
writer.Close()
Hi Kipo, thank you very much for your reply. It solved my issue. However when the code is checking wirting the files to XML Document it is throwing an error message if there is a thumbs.db file. Could you please let me know "How to over come this situation"?
Please remember to click “Mark as Answer” on the post that helps you.
This can be beneficial to other community members reading the thread
Dim writer As New XmlTextWriter(Server.MapPath("~/myXml.xml"), Encoding.UTF8)
writer.WriteStartDocument()
writer.WriteStartElement("gallery")
writer.WriteStartElement("menu")
Dim dirInfo As New DirectoryInfo(MapPath("~/image_gallery/images"))
For Each filInfo As FileInfo In dirInfo.GetFiles() If filInfo.Extension = ".jpg" OrElse filInfo.Extension = ".gif" OrElse filInfo.Extension = ".png" OrElse filInfo.Extension = ".bmp" Then
writer.WriteStartElement("option")
writer.WriteStartElement("imageSize")
Dim tempImage As New Bitmap(filInfo.FullName)
writer.WriteAttributeString("imageWidth", tempImage.Width.ToString())
writer.WriteAttributeString("imageHeight", tempImage.Height.ToString())
tempImage.Dispose()
writer.WriteEndElement()
writer.WriteStartElement("image")
writer.WriteValue("image_gallery/images/" & filInfo.Name)
writer.WriteEndElement()
writer.WriteStartElement("thumb")
writer.WriteValue("image_gallery/images/thumb/" & filInfo.Name)
writer.WriteEndElement()
writer.WriteEndElement() End If
Next
writer.WriteEndElement()
writer.WriteEndElement()
writer.Close()
Dim writer As New XmlTextWriter(Server.MapPath("~/myXml.xml"), Encoding.UTF8)
writer.Formatting = Formatting.Indented
writer.WriteStartDocument()
writer.WriteStartElement("gallery")
writer.WriteStartElement("menu")
Dim dirInfo As New DirectoryInfo(MapPath("~/image_gallery/images"))
For Each filInfo As FileInfo In dirInfo.GetFiles()
If filInfo.Extension = ".jpg" OrElse filInfo.Extension = ".gif" OrElse filInfo.Extension = ".png" OrElse filInfo.Extension = ".bmp" Then
writer.WriteStartElement("option")
writer.WriteStartElement("imageSize")
Dim tempImage As New Bitmap(filInfo.FullName)
writer.WriteAttributeString("imageWidth", tempImage.Width.ToString())
writer.WriteAttributeString("imageHeight", tempImage.Height.ToString())
tempImage.Dispose()
writer.WriteEndElement()
writer.WriteStartElement("image")
writer.WriteValue("image_gallery/images/" & filInfo.Name)
writer.WriteEndElement()
writer.WriteStartElement("thumb")
writer.WriteValue("image_gallery/images/thumb/" & filInfo.Name)
writer.WriteEndElement()
writer.WriteEndElement()
End If
Next
writer.WriteEndElement()
writer.WriteEndElement()
writer.Close()
I have a doubt. As I said earlier I am creating a photo gallery for my website. I need to generate thumbnails for the images that I was uploading to the server. And that particular thumbnails should be stored in a particular folder. Could you please let
me know "How do I do so"?
Here's the code that I am using to create a Folder with the subfolder named "Thumbs" and to upload files.
Partial Class FolderCreation
Inherits System.Web.UI.Page
Dim currentDir As String
Dim imgThumb As Image = Nothing
Dim directorySeparatorChar As Char = Path.DirectorySeparatorChar
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
foldersList()
End If
End Sub
Protected Sub btnCreate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim root As String = Server.MapPath("~/" + RadioButtonList1.SelectedItem.Text)
Dim thisPage As String = Request.Path
currentDir = Request.Params("dir")
If currentDir Is Nothing Then
currentDir = root
End If
If Not currentDir.StartsWith(root) Then
currentDir = root
End If
doesFileExist(uploadedFile.FileName)
End Sub
Sub DoUpload()
If Not (uploadedFile.PostedFile Is Nothing) Then
Try
message.Text = ""
Dim postedFile = uploadedFile.PostedFile
Dim filename As String = Path.GetFileName(postedFile.FileName)
Dim contentType As String = postedFile.ContentType
Dim contentLength As Integer = postedFile.ContentLength
postedFile.SaveAs(currentDir & _
directorySeparatorChar.ToString() & filename)
lblStatus.Text = String.Format( _
"Uploaded file: {0}<br/>" & _
"File size (in bytes): {1:N0}<br />" & _
"Content-type: {2}", _
uploadedFile.FileName, _
uploadedFile.FileBytes.Length, _
uploadedFile.PostedFile.ContentType)
Catch ex As Exception
message.Text = "Failed uploading file"
End Try
End If
End Sub
Public Sub doesFileExist(ByVal searchString As String)
If uploadedFile.FileName <> "" Then
Dim filename As String
filename = Server.MapPath(RadioButtonList1.SelectedItem.Text + "\") & searchString.ToString()
If File.Exists(filename) Then
message.Text = "The File which you are trying to upload <b><u>" & searchString & "</b></u> already exists in folder."
lblStatus.Text = ""
Else
DoUpload()
End If
Else
message.Text = ""
End If
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
doesFolderExist(TextBox1.Text)
End Sub
Public Sub doesFolderExist(ByVal searchString As String)
If TextBox1.Text <> "" Then
Dim foldername As String
Dim DirInfo As System.IO.DirectoryInfo
foldername = Server.MapPath("~\" + TextBox1.Text)
If Directory.Exists(foldername) Then
message.Text = "The Event which you are trying to create <b><u>" & searchString & "</b></u> already exists"
lblStatus.Text = ""
Else
DirInfo = System.IO.Directory.CreateDirectory(MapPath("~/" + TextBox1.Text))
DirInfo.CreateSubdirectory("thumbs")
message.Text = "Event " & searchString & " created successfully."
End If
Else
message.Text = ""
End If
foldersList()
End Sub
Sub foldersList()
Dim dirInfo As DirectoryInfo = New DirectoryInfo(MapPath("~/"))
Dim excludedDirs As String() = {"App_Data", "image_gallery", "images", "thumbs", "MAIN", "Background", "backgrounds"}
RadioButtonList1.DataSource = dirInfo.GetDirectories("*", SearchOption.AllDirectories)
RadioButtonList1.DataBind()
For i As Integer = RadioButtonList1.Items.Count - 1 To 0 Step -1
For Each excludedDir As String In excludedDirs
If RadioButtonList1.Items(i).Text = excludedDir Then
RadioButtonList1.Items.RemoveAt(i)
Exit For
End If
Next
Next
End Sub
Protected Sub RadioButtonList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButtonList2.SelectedIndexChanged
If RadioButtonList2.SelectedItem.Text = "Create an Event" Then
MultiView1.ActiveViewIndex = 0
message.Text = ""
lblStatus.Text = ""
ElseIf RadioButtonList2.SelectedItem.Text = "Upload Photos to an Event" Then
MultiView1.ActiveViewIndex = 1
lblStatus.Text = ""
message.Text = ""
End If
End Sub
End Class
Could you please let me know "How do I write code for generating a thumbnail after uploading an image to a particular folder in server"
Note: The generated thumb nail should be stored in "thumbs" folder.
Thanks and Regards
Sandeep.
Please remember to click “Mark as Answer” on the post that helps you.
This can be beneficial to other community members reading the thread
This is the code for creating thumbnails, you will have to mmodify it to suit your needs:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
FileUpload1.SaveAs(MapPath("~/images/") + FileUpload1.FileName)
ResizeImageAndUpload(FileUpload1.PostedFile.InputStream, "images/thumb/" & FileUpload1.FileName, 200, 300)
End Sub
Private Sub ResizeImageAndUpload(ByVal newFile As Stream, ByVal folderPathAndFilename As String, ByVal maxHeight As Double, ByVal maxWidth As Double)
Dim ratio As Single
Dim thisImage As System.Drawing.Image = System.Drawing.Image.FromStream(newFile)
Dim width As Integer = CInt(thisImage.Width)
Dim height As Integer = CInt(thisImage.Height)
If width > maxWidth Then
ratio = CSng(width) / CSng(maxWidth)
width = CInt((width / ratio))
height = CInt((height / ratio))
End If
If height > maxHeight Then
ratio = CSng(height) / CSng(maxHeight)
height = CInt((height / ratio))
width = CInt((width / ratio))
End If
Dim outImage As New Bitmap(width, height)
Dim outGraphics As Graphics = Graphics.FromImage(outImage)
Dim sb As New SolidBrush(System.Drawing.Color.White)
outGraphics.FillRectangle(sb, 0, 0, outImage.Width, outImage.Height)
outGraphics.DrawImage(thisImage, 0, 0, outImage.Width, outImage.Height)
sb.Dispose()
outGraphics.Dispose()
thisImage.Dispose()
outImage.Save(Server.MapPath(folderPathAndFilename), System.Drawing.Imaging.ImageFormat.Jpeg)
outImage.Dispose()
End Sub
Dim dirInfo As DirectoryInfo = New DirectoryInfo(MapPath("~/"))
GridView1.DataSource = dirInfo.GetFiles("*.*", SearchOption.AllDirectories)
GridView1.DataBind()
I am getting an error message when I am using the code below:
Dim writer As New XmlTextWriter(Server.MapPath("~/MAIN/main_menu.xml"), Encoding.UTF8)
writer.Formatting = Formatting.Indented
writer.WriteStartDocument()
writer.WriteStartElement("main")
writer.WriteStartElement("myMenu")
Dim dirInfo As New DirectoryInfo(MapPath("~/"))
For Each filInfo1 As FileInfo In dirInfo.GetFiles("*", SearchOption.AllDirectories)
If filInfo1.Extension = ".xml" OrElse filInfo1.Extension = ".XML" Then
Could you please go through the above and let me know "Where I am going wrong"? That could help me to fix my issue. The code is getting executed without having any issues "However when I try to view the main_menu.xml page which was created at runtime I am
getting the error message saying that the file is in process. Please help me out with this.
Thanks and Regards
Sandeep
Please remember to click “Mark as Answer” on the post that helps you.
This can be beneficial to other community members reading the thread
kipo
All-Star
16475 Points
2811 Posts
Re: How to create a forum?
Feb 03, 2009 08:56 AM|LINK
Here it is:
babji.sunny@...
Participant
1216 Points
359 Posts
Re: How to create a forum?
Feb 03, 2009 09:37 AM|LINK
Hi Kipo, thank you very much for your reply. It solved my issue. However when the code is checking wirting the files to XML Document it is throwing an error message if there is a thumbs.db file. Could you please let me know "How to over come this situation"?
This can be beneficial to other community members reading the thread
kipo
All-Star
16475 Points
2811 Posts
Re: How to create a forum?
Feb 03, 2009 09:51 AM|LINK
Try with this:
babji.sunny@...
Participant
1216 Points
359 Posts
Re: How to create a forum?
Feb 03, 2009 10:01 AM|LINK
And how would I make these things come in a new line?
I mean to say
<option>
<image>
like this instead of <option><image>
This can be beneficial to other community members reading the thread
kipo
All-Star
16475 Points
2811 Posts
Re: How to create a forum?
Feb 03, 2009 01:58 PM|LINK
Try with this:
babji.sunny@...
Participant
1216 Points
359 Posts
Re: How to create a forum?
Feb 04, 2009 07:42 AM|LINK
Hi Kipo, thanks for your quick reply.
I have a doubt. As I said earlier I am creating a photo gallery for my website. I need to generate thumbnails for the images that I was uploading to the server. And that particular thumbnails should be stored in a particular folder. Could you please let me know "How do I do so"?
Here's the code that I am using to create a Folder with the subfolder named "Thumbs" and to upload files.
Imports System.IO
Imports System.IO.Directory
Imports System.Drawing
Partial Class FolderCreation
Inherits System.Web.UI.Page
Dim currentDir As String
Dim imgThumb As Image = Nothing
Dim directorySeparatorChar As Char = Path.DirectorySeparatorChar
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
foldersList()
End If
End Sub
Protected Sub btnCreate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim root As String = Server.MapPath("~/" + RadioButtonList1.SelectedItem.Text)
Dim thisPage As String = Request.Path
currentDir = Request.Params("dir")
If currentDir Is Nothing Then
currentDir = root
End If
If Not currentDir.StartsWith(root) Then
currentDir = root
End If
doesFileExist(uploadedFile.FileName)
End Sub
Sub DoUpload()
If Not (uploadedFile.PostedFile Is Nothing) Then
Try
message.Text = ""
Dim postedFile = uploadedFile.PostedFile
Dim filename As String = Path.GetFileName(postedFile.FileName)
Dim contentType As String = postedFile.ContentType
Dim contentLength As Integer = postedFile.ContentLength
postedFile.SaveAs(currentDir & _
directorySeparatorChar.ToString() & filename)
lblStatus.Text = String.Format( _
"Uploaded file: {0}<br/>" & _
"File size (in bytes): {1:N0}<br />" & _
"Content-type: {2}", _
uploadedFile.FileName, _
uploadedFile.FileBytes.Length, _
uploadedFile.PostedFile.ContentType)
Catch ex As Exception
message.Text = "Failed uploading file"
End Try
End If
End Sub
Public Sub doesFileExist(ByVal searchString As String)
If uploadedFile.FileName <> "" Then
Dim filename As String
filename = Server.MapPath(RadioButtonList1.SelectedItem.Text + "\") & searchString.ToString()
If File.Exists(filename) Then
message.Text = "The File which you are trying to upload <b><u>" & searchString & "</b></u> already exists in folder."
lblStatus.Text = ""
Else
DoUpload()
End If
Else
message.Text = ""
End If
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
doesFolderExist(TextBox1.Text)
End Sub
Public Sub doesFolderExist(ByVal searchString As String)
If TextBox1.Text <> "" Then
Dim foldername As String
Dim DirInfo As System.IO.DirectoryInfo
foldername = Server.MapPath("~\" + TextBox1.Text)
If Directory.Exists(foldername) Then
message.Text = "The Event which you are trying to create <b><u>" & searchString & "</b></u> already exists"
lblStatus.Text = ""
Else
DirInfo = System.IO.Directory.CreateDirectory(MapPath("~/" + TextBox1.Text))
DirInfo.CreateSubdirectory("thumbs")
message.Text = "Event " & searchString & " created successfully."
End If
Else
message.Text = ""
End If
foldersList()
End Sub
Sub foldersList()
Dim dirInfo As DirectoryInfo = New DirectoryInfo(MapPath("~/"))
Dim excludedDirs As String() = {"App_Data", "image_gallery", "images", "thumbs", "MAIN", "Background", "backgrounds"}
RadioButtonList1.DataSource = dirInfo.GetDirectories("*", SearchOption.AllDirectories)
RadioButtonList1.DataBind()
For i As Integer = RadioButtonList1.Items.Count - 1 To 0 Step -1
For Each excludedDir As String In excludedDirs
If RadioButtonList1.Items(i).Text = excludedDir Then
RadioButtonList1.Items.RemoveAt(i)
Exit For
End If
Next
Next
End Sub
Protected Sub RadioButtonList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButtonList2.SelectedIndexChanged
If RadioButtonList2.SelectedItem.Text = "Create an Event" Then
MultiView1.ActiveViewIndex = 0
message.Text = ""
lblStatus.Text = ""
ElseIf RadioButtonList2.SelectedItem.Text = "Upload Photos to an Event" Then
MultiView1.ActiveViewIndex = 1
lblStatus.Text = ""
message.Text = ""
End If
End Sub
End Class
Could you please let me know "How do I write code for generating a thumbnail after uploading an image to a particular folder in server"
Note: The generated thumb nail should be stored in "thumbs" folder.
Thanks and Regards
Sandeep.
This can be beneficial to other community members reading the thread
kipo
All-Star
16475 Points
2811 Posts
Re: How to create a forum?
Feb 04, 2009 08:12 AM|LINK
This is the code for creating thumbnails, you will have to mmodify it to suit your needs:
babji.sunny@...
Participant
1216 Points
359 Posts
Re: How to get the list of files present in the folders and subfolders?
Feb 04, 2009 09:33 AM|LINK
How do we get the list of files present in the root folder and its subfolders?
This can be beneficial to other community members reading the thread
kipo
All-Star
16475 Points
2811 Posts
Re: How to get the list of files present in the folders and subfolders?
Feb 04, 2009 09:40 AM|LINK
Try with this:
babji.sunny@...
Participant
1216 Points
359 Posts
Re: How to get the list of files present in the folders and subfolders?
Feb 04, 2009 04:22 PM|LINK
Hi Kipo,
I am getting an error message when I am using the code below:
Dim writer As New XmlTextWriter(Server.MapPath("~/MAIN/main_menu.xml"), Encoding.UTF8)
writer.Formatting = Formatting.Indented
writer.WriteStartDocument()
writer.WriteStartElement("main")
writer.WriteStartElement("myMenu")
Dim dirInfo As New DirectoryInfo(MapPath("~/"))
For Each filInfo1 As FileInfo In dirInfo.GetFiles("*", SearchOption.AllDirectories)
If filInfo1.Extension = ".xml" OrElse filInfo1.Extension = ".XML" Then
writer.WriteStartElement("menu")
writer.WriteStartElement("name")
writer.WriteValue("ALBUM")
writer.WriteEndElement()
writer.WriteStartElement("movie")
writer.WriteValue("Image_Gallery_module.swf")
writer.WriteEndElement()
writer.WriteStartElement("loadXML")
writer.WriteValue("\" & filInfo1.Name)
writer.WriteEndElement()
writer.WriteEndElement()
End If
Next
Could you please go through the above and let me know "Where I am going wrong"? That could help me to fix my issue. The code is getting executed without having any issues "However when I try to view the main_menu.xml page which was created at runtime I am getting the error message saying that the file is in process. Please help me out with this.
Thanks and Regards
Sandeep
This can be beneficial to other community members reading the thread