[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How can i create local file thumbnail with ASP.net?

Posted on 2008-11-09
4
Medium Priority
?
337 Views
Last Modified: 2013-11-07
I am creating a tool that will allow a user to upload an image and it will be added to a database.  I would like to create a file based thumbnail based on the original file.

Normally I would do this on the fly, but I expect a fair amount of traffic on the thumbnails and in past (classic ASP) sites it was a huge hit on the processor scaling 3000px+ images down to 150px or so.

See the pseudo code below for an example:


protected int createthumb( string strPath, string strFilename )
{
     // create thumbnail of 'strFilename'
     // save thumbnail as 'tn_' + strFilename
 
     return (error_code) 
}

Open in new window

0
Comment
Question by:swabeui
  • 2
  • 2
4 Comments
 
LVL 20

Accepted Solution

by:
Juan_Barrera earned 2000 total points
ID: 22919722
Hi swabeui,
Try the following code, I don't think you should have performance issues with this, but, of course, it depends on your horsepower and usage scenario.

Using image As Drawing.Image = Drawing.Image.FromFile(strPath)
   Using thumbnail As Drawing.Image = image.GetThumbnailImage(150, 150, Nothing, System.IntPtr.Zero)
       thumbnail.Save("tn" & strFileName)
   End Using
End Using

Open in new window

0
 

Author Comment

by:swabeui
ID: 22936898
Hi Juan,

Sorry for the delay.  I was able to use this to create a thumbnail but realized I wasn't specific enough in my first post.  When the thumbnail is created, it should keep the aspect ratio of the image intact.  So if an image is 1000x500 the resulting image would be 150x75.  Could you tweak the example above to calculate the dimensions?

Thanks,
John
0
 
LVL 20

Expert Comment

by:Juan_Barrera
ID: 22937752
Hi John,
Here is a simple example of a ratio 10:1, you can tweak it to suit your needs:

 Using image As Drawing.Image = Drawing.Image.FromFile(strPath)
            Dim ratio As Integer = 10
            Using thumbnail As Drawing.Image = image.GetThumbnailImage(image.Size.Width / ratio, image.Size.Height / ratio, Nothing, System.IntPtr.Zero)
                thumbnail.Save("tn" & strFIleName)
            End Using
        End Using

Open in new window

0
 

Author Comment

by:swabeui
ID: 22947134
Thanks for the help, I was able to get everything working how I needed it by calculating the ratio based off the dimensions of the existing image.  

John
int thumbX = 150;
int thumbY = 150;
float imgWidth = bigimage.Width;
float imgHeight = bigimage.Height;
 
// calculate aspect ratio thumbnail size
if (bigimage.Width > bigimage.Height)
{
    // thumbX alredy set to 150, calculate Y size
    thumbY = (int)((150 / imgWidth) * imgHeight);
} else if (bigimage.Width < bigimage.Height)   {
    // thumbY already set to 150, calculate X size
    thumbX = (int)((150 / imgHeight) * imgWidth);
}

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question