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