-----Original Message-----
The following is an enum and a static method I wrote to save images to the
browser. In .Net, images are worked with in memory as Bitmaps. The New
constructor for a Bitmap is overloaded to allow you to create a Bitmap from
a file easily. Once you've done that, you can use the WriteToBrowser()
method below to write it to the browser in any format you wish.
/// <summary>
/// Allowable Image File Types
/// </summary>
public enum ImageTypesEnum : int
{
BMP = 0,
JPG = 1,
GIF = 2
}
/// <summary>
/// Write Image to browser
/// </summary>
public static void WriteToBrowser(Bitmap Image, ImageTypesEnum ImageType)
{
HttpResponse Response;
MemoryStream objStream = new MemoryStream();
ImageCodecInfo objImageCodecInfo;
EncoderParameters objEncoderParameters;
try
{
if (Image == null)
throw new Exception("ImageObject is not initialized. Use CreateImage() to
initialize ImageObject");
if (HttpContext.Current == null)
throw new Exception("No HttpContext");
Response = HttpContext.Current.Response;
switch (ImageType)
{
default:
objImageCodecInfo = GetEncoderInfo("image/jpeg");
Response.ContentType = "image/jpeg";
break;
case ImageTypesEnum.GIF:
objImageCodecInfo = GetEncoderInfo("image/gif");
Response.ContentType = "image/gif";
break;
case ImageTypesEnum.BMP:
objImageCodecInfo = GetEncoderInfo("image/bmp");
Response.ContentType = "image/bmp";
break;
}
objEncoderParameters = new EncoderParameters(3);
objEncoderParameters.Param[0] = new EncoderParameter (Encoder.Compression,
(long)EncoderValue.CompressionLZW);
objEncoderParameters.Param[1] = new EncoderParameter (Encoder.Quality, 100L);
objEncoderParameters.Param[2] = new EncoderParameter (Encoder.ColorDepth,
24L);
Image.Save(Response.OutputStream, objImageCodecInfo, objEncoderParameters);
}
catch (Exception e)
{
throw e;
}
}
HTH,
--
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
Hi all. I need to convert files on my server from gif to
jpg format. I tried with the thumb function, but it does
not work (on the line thumb.Save(Response.OutputStream,
Imaging.ImageFormat.Jpeg)). Anybody can help me, tanks a
lot.
.