Convert gif to jpg

Discussion in 'ASP .Net' started by Ignacio Marcos, Nov 24, 2003.

  1. 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.
    Ignacio Marcos, Nov 24, 2003
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:024201c3b2cb$dfcc76d0$...
    > 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, Nov 24, 2003
    #2
    1. Advertising

  3. 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 []

    >-----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.
    >
    >"Ignacio Marcos" <> wrote in

    message
    >news:024201c3b2cb$dfcc76d0$...
    >> 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.

    >
    >
    >.
    >
    Ignacio Marcos, Nov 24, 2003
    #3
  4. 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" <> wrote in message
    news:05db01c3b2d4$3de207a0$...
    > 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 []
    >
    > >-----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.
    > >
    > >"Ignacio Marcos" <> wrote in

    > message
    > >news:024201c3b2cb$dfcc76d0$...
    > >> 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, Nov 24, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ignacio Marcos

    need to convert from gif file to jpg file

    Ignacio Marcos, Nov 21, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    326
    Ignacio Marcos
    Nov 21, 2003
  2. David Hearn
    Replies:
    1
    Views:
    578
    Joel Leong
    Mar 5, 2005
  3. Replies:
    2
    Views:
    1,581
    Andrew Thompson
    May 19, 2007
  4. Beauregard T. Shagnasty

    Re: filename.gif or filename.gif.jpg?

    Beauregard T. Shagnasty, May 30, 2008, in forum: HTML
    Replies:
    1
    Views:
    730
    Jonathan N. Little
    May 30, 2008
  5. Bergamot
    Replies:
    0
    Views:
    432
    Bergamot
    May 30, 2008
Loading...

Share This Page