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> </td>
<td colspan="2" style="padding-top: 30px">
<input type="submit" class="btn" name="submit" />
</td>
</tr>
</table>
}
Post your comments / questions
Recent Article
- Your system's memory configuration has changed since it entered hibernation.
- Save image to client browser using jQuery?
- Get filename of image jQuery?
- How to get the image src using JavaScript?
- System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' which has a higher version than referenced assembly 'System.Net.Http.Formatting' with identity 'System.Net.Http.Formatting, Version=4.0.0.0
- Cordova Android build error "Cannot read property 'length' of undefined". An error occurred while running subprocess cordova.
- ERROR in The Angular Compiler requires TypeScript >=3.9.2 and <4.0.0 but 3.8.3 was found instead
- Code ENOLOCAL Could not install from "android" as it does not contain a package.json file.
Related Article