Connecting Tech Pros Worldwide Help | Site Map

Convert gif to jpg

Ignacio Marcos
Guest
 
Posts: n/a
#1: Nov 18 '05
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.
Kevin Spencer
Guest
 
Posts: n/a
#2: Nov 18 '05

re: Convert gif to jpg


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.

"Ignacio Marcos" <marcosignacio@ciudad.com.ar> wrote in message
news:024201c3b2cb$dfcc76d0$a501280a@phx.gbl...[color=blue]
> 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.[/color]


Ignacio Marcos
Guest
 
Posts: n/a
#3: Nov 18 '05

re: Convert gif to jpg


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 [marcosignacio@ciudad.com.ar]
[color=blue]
>-----Original Message-----
>The following is an enum and a static method I wrote to[/color]
save images to the[color=blue]
>browser. In .Net, images are worked with in memory as[/color]
Bitmaps. The New[color=blue]
>constructor for a Bitmap is overloaded to allow you to[/color]
create a Bitmap from[color=blue]
>a file easily. Once you've done that, you can use the[/color]
WriteToBrowser()[color=blue]
>method below to write it to the browser in any format you[/color]
wish.[color=blue]
>
>/// <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,[/color]
ImageTypesEnum ImageType)[color=blue]
>{
>HttpResponse Response;
>MemoryStream objStream = new MemoryStream();
>ImageCodecInfo objImageCodecInfo;
>EncoderParameters objEncoderParameters;
>try
>{
>if (Image == null)
>throw new Exception("ImageObject is not initialized. Use[/color]
CreateImage() to[color=blue]
>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[/color]
(Encoder.Compression,[color=blue]
>(long)EncoderValue.CompressionLZW);
>objEncoderParameters.Param[1] = new EncoderParameter[/color]
(Encoder.Quality, 100L);[color=blue]
>objEncoderParameters.Param[2] = new EncoderParameter[/color]
(Encoder.ColorDepth,[color=blue]
>24L);
>Image.Save(Response.OutputStream, objImageCodecInfo,[/color]
objEncoderParameters);[color=blue]
>}
>catch (Exception e)
>{
>throw e;
>}
>}
>
>HTH,
>--
>Kevin Spencer
>..Net Developer
>Microsoft MVP
>Big things are made up
>of lots of little things.
>
>"Ignacio Marcos" <marcosignacio@ciudad.com.ar> wrote in[/color]
message[color=blue]
>news:024201c3b2cb$dfcc76d0$a501280a@phx.gbl...[color=green]
>> 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.[/color]
>
>
>.
>[/color]
Kevin Spencer
Guest
 
Posts: n/a
#4: Nov 18 '05

re: Convert gif to jpg


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" <anonymous@discussions.microsoft.com> wrote in message
news:05db01c3b2d4$3de207a0$a301280a@phx.gbl...[color=blue]
> 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 [marcosignacio@ciudad.com.ar]
>[color=green]
> >-----Original Message-----
> >The following is an enum and a static method I wrote to[/color]
> save images to the[color=green]
> >browser. In .Net, images are worked with in memory as[/color]
> Bitmaps. The New[color=green]
> >constructor for a Bitmap is overloaded to allow you to[/color]
> create a Bitmap from[color=green]
> >a file easily. Once you've done that, you can use the[/color]
> WriteToBrowser()[color=green]
> >method below to write it to the browser in any format you[/color]
> wish.[color=green]
> >
> >/// <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,[/color]
> ImageTypesEnum ImageType)[color=green]
> >{
> >HttpResponse Response;
> >MemoryStream objStream = new MemoryStream();
> >ImageCodecInfo objImageCodecInfo;
> >EncoderParameters objEncoderParameters;
> >try
> >{
> >if (Image == null)
> >throw new Exception("ImageObject is not initialized. Use[/color]
> CreateImage() to[color=green]
> >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[/color]
> (Encoder.Compression,[color=green]
> >(long)EncoderValue.CompressionLZW);
> >objEncoderParameters.Param[1] = new EncoderParameter[/color]
> (Encoder.Quality, 100L);[color=green]
> >objEncoderParameters.Param[2] = new EncoderParameter[/color]
> (Encoder.ColorDepth,[color=green]
> >24L);
> >Image.Save(Response.OutputStream, objImageCodecInfo,[/color]
> objEncoderParameters);[color=green]
> >}
> >catch (Exception e)
> >{
> >throw e;
> >}
> >}
> >
> >HTH,
> >--
> >Kevin Spencer
> >..Net Developer
> >Microsoft MVP
> >Big things are made up
> >of lots of little things.
> >
> >"Ignacio Marcos" <marcosignacio@ciudad.com.ar> wrote in[/color]
> message[color=green]
> >news:024201c3b2cb$dfcc76d0$a501280a@phx.gbl...[color=darkred]
> >> 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.[/color]
> >
> >
> >.
> >[/color][/color]


Closed Thread