Convert gif to jpg

I

Ignacio Marcos

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.
 
K

Kevin Spencer

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.
 
I

Ignacio Marcos

Kevin:

Thanks for your answer! i read the code, but i need to
know how to save the file result (in jpg) to disk.

Eg: convert e:\wwwroot\pictures\1.gif to
e:\wwwroot\pictures\1.jpg

I dont wont to show the picture in the browser.
Thanks again, Ignacio Marcos [[email protected]]
-----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.


.
 
K

Kevin Spencer

The Bitmap.Save() Method is overloaded to allow you to save to a file path
or a stream, and allows you to set the parameters as well. For example, in
my method, it saves it to the Response.OutputStream, but that could also be
a file stream.

--
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

Ignacio Marcos said:
Kevin:

Thanks for your answer! i read the code, but i need to
know how to save the file result (in jpg) to disk.

Eg: convert e:\wwwroot\pictures\1.gif to
e:\wwwroot\pictures\1.jpg

I dont wont to show the picture in the browser.
Thanks again, Ignacio Marcos [[email protected]]
-----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.


.
 

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