c# .net Adsense ADO.NET Linq Viruses/security asp.net MVC JQuery Angular-js Node-js SEO Java C++ SQL API Networking vb.net .Net Css JavaScript Generics c#.Net entity framework HTML Website host Website Construction Guide HTTP tutorial W3C tutorial Web Services JSON Psychology Ionic framework Angular ReactJS Python Computer Android
asp.net MVC

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

| | ASP-NET , MVC

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 1Create 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 2Create 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>
   }