How to convert GIF format to static jpeg format in asp.net C#?

In this article, I will show you to convert animated moving pictures i.e GIF format to static image. It take the first frame from the GIF image and create a static image without loosing quality.

You should import the reference System.Drawing.

Step 1: Create an asp.net mvc project and create the class on the model folder and name it as pic. Copy and paste the following code.

public class pic
{
    public  IEnumerable<HttpPostedFileBase>files { get; set; }
}

 

Step 2: Create an asp.net mvc project and right click on the controller folder and create a new controller and name it as GifToJpgController. Inside the Controller, copy and paste the following code.

  public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(pic picture)
        {
            foreach(var file in picture.files) {

               if (file.ContentLength > 0)
                {
                    var filePath = Path.Combine(Server.MapPath("~/Images"), "test.jpg");
                    System.Drawing.Image img = System.Drawing.Image.FromStream(file.InputStream);
                    EncoderParameterscodecParams = new EncoderParameters(1);
                    codecParams.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
                    ImageCodecInfo[]encoders;
                    encoders = ImageCodecInfo.GetImageEncoders();
                    img.Save(filePath,encoders[1], codecParams);         
                }
            }

            TempData["Message"] = "files uploaded successfully";
            return RedirectToAction("Index");
        }

 

Step 3: Right click on the Share folder and create a razor view named as index. Copy and paste the following code.

@using (@Html.BeginForm(null, null, FormMethod.Post,

     new { enctype = "multipart/form-data" }))
    {
        if (TempData["Message"] != null)
        {
 
        <p style="font-family: Arial; font-size: 16px; font-weight: 200; color: red">@TempData["Message"]</p>
        }
        <table>
            <tr>
                <td style="padding-bottom: 35px" colspan="2">
                    <h2 style="color: #FF5722">Convert Gif image to Static JPG</h2>
                </td>
            </tr>
            <tr>
                <td style="width: 100px;">
                    <b style="color: #FF5722">File:</b>
                </td>
                <td>
                    <input type="file" name="files" id="files" multiple="multiple" />
                </td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td colspan="2" style="padding-top: 30px">
                    <input type="submit" class="btn" name="submit" />
                </td>
            </tr>
        </table>
   }

 

gif format to jpeg format