[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Creating Thumbnails - A generic error occurred in GDI+.

Posted on 2005-04-24
3
Medium Priority
?
416 Views
Last Modified: 2010-04-07
Hi,
I am trying to get this code to work,
What I am trying to do is go through all image files in a directory. and create a thumbnail with the same image name and extension except adding _th to the name so for each file...

image1.gif = image1_th.gif
image2.jpg = image2_th.jpg

and so on.

I get this stupid generic error message, which gives me no clue.

here is an example of the code I am using.

Private Sub CreateThumbs()
        Dim oImage As Image
        Dim othumb As Image
        Dim s As String
        Dim tmpFile As String
        Dim oExt As String

        Try
            For Each s In Directory.GetFiles(T1.ImagePath)
                oExt = GetExtension(s)
                Select Case oExt

                    Case "gif"
                        tmpFile = GetFileName(s) & "_th." & oExt
                        oImage = Image.FromFile(s)
                        othumb = oImage.GetThumbnailImage(200, 200, Nothing, System.IntPtr.Zero)
                        othumb.Save("productimages/" & tmpFile & oExt, Imaging.ImageFormat.Jpeg)
                    Case "jpg"
                        tmpFile = GetFileName(s) & "_th." & oExt
                        oImage = Image.FromFile(s)
                        othumb = oImage.GetThumbnailImage(200, 200, Nothing, System.IntPtr.Zero)
                        othumb.Save("productimages/" & tmpFile & oExt, Imaging.ImageFormat.Jpeg)
                    Case "bmp"
                        tmpFile = GetFileName(s) & "_th." & oExt
                        oImage = Image.FromFile(s)
                        othumb = oImage.GetThumbnailImage(200, 200, Nothing, System.IntPtr.Zero)
                        othumb.Save("productimages/" & tmpFile & oExt, Imaging.ImageFormat.Jpeg)
                    Case Else
                        'do nothing
                End Select
            Next
            Label1.Text = "Thumbs Created"
        Catch ex As Exception
            Label1.Text = ex.Message.ToString
        Finally
            oImage.Dispose()
            othumb.Dispose()
        End Try
    End Sub

    Private Function GetExtension(ByVal fName As String) As String
        Try
            Dim s() As String
            Dim i As Integer
            s = Split(fName, ".", , CompareMethod.Text)
            i = UBound(s)
            Return s(i)
        Catch ex As Exception
            lblMssg.Text = "Get Ext " & ex.Message.ToString
        End Try
    End Function

    Private Function GetFileName(ByVal fName As String) As String
        Try
            Dim s() As String
            Dim tmp() As String
            Dim i As Integer
            s = Split(fName, "\", , CompareMethod.Text)
            i = UBound(s)
            tmp = Split(s(i), ".", , CompareMethod.Text)

            Return tmp(0)

        Catch ex As Exception
            lblMssg.Text = "Get Ext " & ex.Message.ToString
        End Try
    End Function

Thanks ahead of time!

0
Comment
Question by:flosoft
  • 2
3 Comments
 
LVL 6

Expert Comment

by:sansoftura
ID: 13856097
1)One problem i see is you have given the fine extention 2 times
...
  Case "gif"
                        tmpFile = GetFileName(s) & "_th." & oExt <---------------------- .gif
                        oImage = Image.FromFile(s)
                        othumb = oImage.GetThumbnailImage(200, 200, Nothing, System.IntPtr.Zero)
                        othumb.Save("productimages/" & tmpFile & oExt, Imaging.ImageFormat.Jpeg) <--------- gif
...
This makes the image name image1_th.gifgif

2) I am sure you are including Imports System.Drawing class

If you have any specific error post it.
0
 

Author Comment

by:flosoft
ID: 13856133
Hi,
Yes I am importing system drawing.

I have tried to change this a billion different ways to no avail including the code you just posted, no luck same error... I love the nice discriptive error message so that one can figure out what a possible error may be, this is just b.s.

ridiculous that an error would say that, I can only imagine if the stuff I wrote threw errors like that... come on ms.net guys, stop being so damn lazy.
0
 
LVL 6

Accepted Solution

by:
sansoftura earned 500 total points
ID: 13856180
Well i copied and pasted your code in asp.NET app. I did the following changes to your code
1. Hardcoded the source image path instead of T1.ImagePath
2. Hardcoded destination path instead of "productimages/"
3. Removed the aditional file extention that you have given 2 times.

It works perfectly fine.
===========================
May be the destination folder doesnot have permision? Did you checked that? Can you put breakpoint and figureout where the program breaks?
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

829 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