Kush
Kush

Reputation: 133

Create function to create thambnails of available images in a folder

I have create a CMS to upload all image to a folder using ed all my images to a folder using,

file.SaveAs(Server.MapPath("../images/") + advertID.ToString + "_" + i.ToString + fileExt)

Now, all images are saved and i forgot it create thumbnails. :(

I need to read all images at once and create thumbnails,

myimg = System.Drawing.Image.FromFile(imgFileName)
myimg = myimg.GetThumbnailImage(154, 94, Nothing, IntPtr.Zero)
myimg.Save(Server.MapPath("../Content/") + "Thumb_" + imgFileName, myimg.RawFormat)

I need this to function fast. I don't seems to know how to read these image names one by one.

Thanks in advance.

Upvotes: 0

Views: 659

Answers (1)

Mr. Smith
Mr. Smith

Reputation: 5558

Slightly modified version of this:

public void createThumbnails(string sourceDir) 
{
    //Process the list of files found in the directory. 
    string [] fileEntries = Directory.GetFiles(sourceDir);

    foreach(string fileName in fileEntries)
    {
        //Do resizing here.
        myimg = System.Drawing.Image.FromFile(fileName);
        myimg = myimg.GetThumbnailImage(154, 94, Nothing, IntPtr.Zero);
        myimg.Save(Server.MapPath("../Content/") + "Thumb_" + fileName, myimg.RawFormat);
    }
}

Called like this:

createThumbnails("C:\Path\To\Images");

*Edit: VB.NET equivalent using converter tool:

Public Sub createThumbnails(ByVal sourceDir As String)
    Dim fileEntries As String() = Directory.GetFiles(sourceDir)

    For Each fileName As String In fileEntries
        myimg = System.Drawing.Image.FromFile(fileName)
        myimg = myimg.GetThumbnailImage(154, 94, [Nothing], IntPtr.Zero)
        myimg.Save((Server.MapPath("../Content/") & "Thumb_") + fileName, myimg.RawFormat)
    Next
End Sub

Upvotes: 1

Related Questions