views:

230

answers:

3

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.

+1  A: 

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
Mr. Smith
This would be it.
Jason N. Gaylord
Im getting this error (I want to use it in VB.net)Compiler Error Message: BC30369: Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.Source Error:Line 25: myimg.Save(Server.MapPath("/images/") + "Thumb_" + fileName, myimg.RawFormat)Source File: G:\wwwroot\srilancars\createThumbs.aspx.vb Line: 25
Kush
The error is talking about "Server" <--
Kush
Remove the (C#: static, VB.NET: shared) in the function definition to solve the problem, i.e: public void createThumbNails() instead of public static void createThumbnails(). VB.NET: Public Sub createThumbnails() instead of Public Shared Sub. Updated my answer.
Mr. Smith
Now I am getting this error:Exception Details: System.OutOfMemoryException: Out of memory.Source Error:Line 24: myimg = System.Drawing.Image.FromFile(fileName)Source File: G:\wwwroot\srilancars\createThumbs.aspx.vb Line: 24
Kush
How many images are you trying to resize and what size are they? Can your machine handle it all?
Mr. Smith
these images are basically between 200 - 400 k. and i am testing with few images. I have a windows 2003 server running with QuadCore Processor, 4 gb ram.
Kush
A: 

do you have a compiled version of this?source code?

mike
A: 

Sorry, misread question.

Moo