Convert gif to jpg

  • Thread starter Thread starter Ignacio Marcos
  • Start date Start date
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.
 
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.
 
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.


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


.
 
Back
Top