C# ASP.NET Thumbnail Generator issue

D

David

Greetings all,

I am having a problem with an asp.net application using C# which is a
thumbnail generator. Here is the code used...

http://www.eggheadcafe.com/articles/20030515.asp

It works fine for .jpg images but for some reason it doesn't work with .gif
images. The author of the article says it works fine for him but I can't get
it to work. I was wondering if anyone here can shed some light?

I have my permissions set for read, write, etc.. and as I said it works fine
for jpg's.

Thanks, David
 
K

Kevin Spencer

The problem is, you used code you didn't understand. My advice would be to
study it, keep the .Net SDK handy, study it until you own it, and when you
understand it all of your problems will go away. In addition, the next time
you run into a similar (but not the same) problem, you will be able to write
your own solution straight out of your head.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 
D

David

Thanks for the reply it does help but I do understand the code more or less.
Yes, I am new to .Net and I have studied the documentation on the Microsoft
site and can not find anything amiss in the code. Also, I'm not a C#
programmer, I am a Javascript programmer and there are times I even need
help with the js code but not very often. Granted the languages 'are'
different but mostly in the syntax.

I will take your suggestion though and do some more research of course. Just
hoping someone here that is more profecient in the .Net framework could lend
a hand or spot something obvious. This is what the forum is for no?

David
 
K

Kevin Spencer

You're on the right track, David. Unfortunately, you didn't post your code.
You only posted the link to the article and its code. I don't have the time
to debug the article's code, and make sure that the problem isn't there. So
I gave you a methodology that has always worked for me.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 
D

David

I gave a link to the original article because it supposedly works, but not
for me. Here is a link to my code which is simpler.

http://mysite.verizon.net/res8xvny/temp2/theCcode.htm

As I said, it works wonderfully for .jpg's but not for .gif's.

I don't disagree with your methodology as it works for me too, but after too
many hours one seeks a fresh eyeball on the problem. Hence my post. If you
don't have the time to take a look, I understand, no problem. I'm just
asking here on the forum and if anyone has an idea.

David
 
K

Kevin Spencer

Hi David,

I have the time for anyone who sticks with me, and show the kind of
discipline that you do! And I did take a look.

I may be a bit confused, as there are 2 functions on the page, so maybe I'm
misunderstanding the code you provided. But it looks to me like the Page
code you posted performs one task and then Redirects to another page.

My suspicion is that this other page somehow includes the second function in
your code page, since you say that your app "works" with GIFs but not with
JPGs. So, apparently there is some of the puzzle missing. But I do follow
the basic logic of the code you did post. And I compared it to the tutorial
you found. Looks like you followed it well. It also looks like the tutorial
was written by a VB developer who learned C#. You just can't trust the
language any more these days as an indicator of the skill level of the
developer.

The fellow who wrote the tutorial may not have had trouble because of the
types of JPGs he is using. JPGs can be a bit tricky, and his code will NOT
work for all of them.

I had a similar problem a couple of years ago when developing an Imaging
class called "ImageMaker." I created a base class (ImageMakerBase) that
would handle the common functionality, such as saving the image to a file,
or writing it out to an ASPX page.

Probably the easiest thing for me to do at this point would be to simply
post the code that saves the image, and the relevant enum and namespaces
that it uses. I'm positive that you can adapt this code as well, and let me
know if you have any trouble using it. It has performed well for me for the
past 2 years.

Note also that "_ImageObject" is a Bitmap, which I used in this class to
store and manipulate the Image. The "HandleError()" method is simply a
method that logs the exception, and you can replace it with anything you
like. _QualityPercentage is simply a quality factor that you will probably
use 100 for. Also, the additional "cloning" of the image is for the purpose
that the _ImageObject remains in the class after saving.

using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;

....

public enum ImageTypesEnum
{
BMP = 0,
JPG,
GIF,
PNG,
BMP_TRANSP
}
....

/// <summary>
/// Saves the Image to a file
/// </summary>
/// <param name="ImageType">Type of image file to save to</param>
/// <param name="strPath">Path to save image to</param>
public virtual void SaveToFile(string strPath, bool blnOverWrite)
{
EncoderParameters objEncoderParameters;
Bitmap b = null;
Graphics g = null;
long l = 24L;

ImageCodecInfo objImageCodecInfo;
try
{
if (File.Exists(strPath))
{
if (!blnOverWrite)
throw new Exception("File '" + strPath + "' exists, and blnOverWrite
is False. Unable to OverWrite.");
File.Delete(strPath);
}
switch (_ImageType)
{
default:
objImageCodecInfo = GetEncoderInfo("image/jpeg");
break;
case ImageTypesEnum.GIF:
objImageCodecInfo = GetEncoderInfo("image/gif");
break;
case ImageTypesEnum.BMP_TRANSP:
case ImageTypesEnum.BMP:
objImageCodecInfo = GetEncoderInfo("image/bmp");
_QualityPercentage = 100;
break;
case ImageTypesEnum.PNG:
objImageCodecInfo = GetEncoderInfo("image/png");
break;
}
if (_ImageType == ImageTypesEnum.BMP_TRANSP)
l = 32L;
objEncoderParameters = new EncoderParameters(3);
objEncoderParameters.Param[0] = new
EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionNone);
objEncoderParameters.Param[1] = new EncoderParameter(Encoder.Quality,
(long)_QualityPercentage);
objEncoderParameters.Param[2] = new EncoderParameter(Encoder.ColorDepth,
l);

if (_ImageType == ImageTypesEnum.BMP_TRANSP)
b = new Bitmap(_ImageObject.Width, _ImageObject.Height,
PixelFormat.Format32bppArgb);
else
b = new Bitmap(_ImageObject.Width, _ImageObject.Height,
PixelFormat.Format24bppRgb);
g = Graphics.FromImage(b);
g.DrawImage(_ImageObject, 0,0);
b.Save(strPath, objImageCodecInfo, objEncoderParameters);
}
catch (Exception E)
{
HandleError(E);
}
finally
{
if (g != null) g.Dispose();
if (b != null) b.Dispose();
}
}

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 
D

David

Kevin,

Thanks for the explanation. I modified the original code to suit my
spplication. Basically the C# code ( aspx page ) just creates thumbnails
from the originals, creates and places the thumbs inside of thier own
folder. When it is done, it redirects to an asp page where some VB and JS
code processes it into an image gallery with all the bells and whistles.

There is no problem in the latter, of processing the images and thumbs into
the gallery and displaying them on the page. I hand wrote all of that.

Thanks for the code you posted. I'll need some time to wrap my head around
what you have done and adapt it to my purpose. I will post back with how I
make out.

Thanks again for your time and help.

David





Kevin Spencer said:
Hi David,

I have the time for anyone who sticks with me, and show the kind of
discipline that you do! And I did take a look.

I may be a bit confused, as there are 2 functions on the page, so maybe I'm
misunderstanding the code you provided. But it looks to me like the Page
code you posted performs one task and then Redirects to another page.

My suspicion is that this other page somehow includes the second function in
your code page, since you say that your app "works" with GIFs but not with
JPGs. So, apparently there is some of the puzzle missing. But I do follow
the basic logic of the code you did post. And I compared it to the tutorial
you found. Looks like you followed it well. It also looks like the tutorial
was written by a VB developer who learned C#. You just can't trust the
language any more these days as an indicator of the skill level of the
developer.

The fellow who wrote the tutorial may not have had trouble because of the
types of JPGs he is using. JPGs can be a bit tricky, and his code will NOT
work for all of them.

I had a similar problem a couple of years ago when developing an Imaging
class called "ImageMaker." I created a base class (ImageMakerBase) that
would handle the common functionality, such as saving the image to a file,
or writing it out to an ASPX page.

Probably the easiest thing for me to do at this point would be to simply
post the code that saves the image, and the relevant enum and namespaces
that it uses. I'm positive that you can adapt this code as well, and let me
know if you have any trouble using it. It has performed well for me for the
past 2 years.

Note also that "_ImageObject" is a Bitmap, which I used in this class to
store and manipulate the Image. The "HandleError()" method is simply a
method that logs the exception, and you can replace it with anything you
like. _QualityPercentage is simply a quality factor that you will probably
use 100 for. Also, the additional "cloning" of the image is for the purpose
that the _ImageObject remains in the class after saving.

using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;

...

public enum ImageTypesEnum
{
BMP = 0,
JPG,
GIF,
PNG,
BMP_TRANSP
}
...

/// <summary>
/// Saves the Image to a file
/// </summary>
/// <param name="ImageType">Type of image file to save to</param>
/// <param name="strPath">Path to save image to</param>
public virtual void SaveToFile(string strPath, bool blnOverWrite)
{
EncoderParameters objEncoderParameters;
Bitmap b = null;
Graphics g = null;
long l = 24L;

ImageCodecInfo objImageCodecInfo;
try
{
if (File.Exists(strPath))
{
if (!blnOverWrite)
throw new Exception("File '" + strPath + "' exists, and blnOverWrite
is False. Unable to OverWrite.");
File.Delete(strPath);
}
switch (_ImageType)
{
default:
objImageCodecInfo = GetEncoderInfo("image/jpeg");
break;
case ImageTypesEnum.GIF:
objImageCodecInfo = GetEncoderInfo("image/gif");
break;
case ImageTypesEnum.BMP_TRANSP:
case ImageTypesEnum.BMP:
objImageCodecInfo = GetEncoderInfo("image/bmp");
_QualityPercentage = 100;
break;
case ImageTypesEnum.PNG:
objImageCodecInfo = GetEncoderInfo("image/png");
break;
}
if (_ImageType == ImageTypesEnum.BMP_TRANSP)
l = 32L;
objEncoderParameters = new EncoderParameters(3);
objEncoderParameters.Param[0] = new
EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionNone);
objEncoderParameters.Param[1] = new EncoderParameter(Encoder.Quality,
(long)_QualityPercentage);
objEncoderParameters.Param[2] = new EncoderParameter(Encoder.ColorDepth,
l);

if (_ImageType == ImageTypesEnum.BMP_TRANSP)
b = new Bitmap(_ImageObject.Width, _ImageObject.Height,
PixelFormat.Format32bppArgb);
else
b = new Bitmap(_ImageObject.Width, _ImageObject.Height,
PixelFormat.Format24bppRgb);
g = Graphics.FromImage(b);
g.DrawImage(_ImageObject, 0,0);
b.Save(strPath, objImageCodecInfo, objEncoderParameters);
}
catch (Exception E)
{
HandleError(E);
}
finally
{
if (g != null) g.Dispose();
if (b != null) b.Dispose();
}
}

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
What You Seek Is What You Get.

David said:
I gave a link to the original article because it supposedly works, but not
for me. Here is a link to my code which is simpler.

http://mysite.verizon.net/res8xvny/temp2/theCcode.htm

As I said, it works wonderfully for .jpg's but not for .gif's.

I don't disagree with your methodology as it works for me too, but after
too
many hours one seeks a fresh eyeball on the problem. Hence my post. If you
don't have the time to take a look, I understand, no problem. I'm just
asking here on the forum and if anyone has an idea.

David



there.
So is
a
 
D

David

Kevin,
I may be a bit confused, as there are 2 functions on the page, so maybe I'm
misunderstanding the code you provided. But it looks to me like the Page
code you posted performs one task and then Redirects to another page.

To clarify, Yes, there are 2 functions. The second function ( public void
GenerateThumbNail ) is the code that actually generates the thumbnails. The
first function ( public void GenerateThumbNailImagesForFolder ) creates the
folder necessary for the thumbs and as well provides the path to the
originals, as well as the type of image for the ( public void
GenerateThumbNail ). This seems a fairly logical approach to me.

My suspicion is that this other page somehow includes the second function in
your code page, since you say that your app "works" with GIFs but not with
JPGs.

Actually it works with all JPGs I have tried, but not GIFs. That is the
problem.

Anyway, I will work with the code you posted and see how I can make out with
that.

David
 
K

Kevin Spencer

Hi David,

GIFs also can differ in several ways. For example, a GIF can have a pallette
of anywhere from 1 to 255 colors.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 
K

Kevin Spencer

Correction: 256 colors!

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top