Created
October 29, 2015 21:03
-
-
Save AFelipeTrujillo/6cad2a478635c9f46bac to your computer and use it in GitHub Desktop.
How to create a thumbnail image with C-Sharp (C#)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@model Project.Models.PictureModel | |
@{ | |
ViewBag.Title = "PictureUpload"; | |
} | |
<h2>PictureUpload</h2> | |
@using (Html.BeginForm("PictureUpload", "Controller", FormMethod.Post, new { enctype = "multipart/form-data" })) | |
{ | |
@Html.AntiForgeryToken() | |
@Html.ValidationSummary(true) | |
<div> | |
@Html.LabelFor(model => model.PictureFile) | |
</div> | |
<div> | |
@Html.TextBoxFor(model => model.PictureFile, new { type = "file" }) | |
@Html.ValidationMessageFor(model => model.PictureFile) | |
</div> | |
<input type="submit" /> | |
} | |
@section Scripts{ | |
@Scripts.Render("~/bundles/jqueryval") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.ComponentModel.DataAnnotations; | |
namespace Project.Models | |
{ | |
public class PictureModel | |
{ | |
[Required] | |
public HttpPostedFileBase PictureFile { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[HttpPost] | |
[ValidateAntiForgeryToken] | |
public ActionResult PictureUpload(PictureModel model) | |
{ | |
if (model.PictureFile.ContentLength > 0) | |
{ | |
var fileName = Path.GetFileName(model.PictureFile.FileName); | |
var filePath = Server.MapPath("/Content/Uploads"); | |
string savedFileName = Path.Combine(filePath,fileName); | |
model.PictureFile.SaveAs(savedFileName); | |
Thumbnail.Create(fileName, filePath, 100, 100, true); | |
} | |
return View(model); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Drawing; | |
using System.Drawing.Drawing2D; | |
using System.IO; | |
namespace Porject.Models.Bussiness | |
{ | |
public class Thumbnail | |
{ | |
public static void Create(string fileName, string filePath, int thumbWi, int thumbHi, bool maintainAspect) | |
{ | |
var originalFile = Path.Combine(filePath, fileName); | |
var source = Image.FromFile(originalFile); | |
if (source.Width <= thumbWi && source.Height <= thumbHi) return; | |
Bitmap thumbnail; | |
try | |
{ | |
int wi = thumbWi; | |
int hi = thumbHi; | |
if (maintainAspect) | |
{ | |
if (source.Width > source.Height) | |
{ | |
wi = thumbWi; | |
hi = (int)(source.Height * ((decimal)thumbWi / source.Width)); | |
} | |
else | |
{ | |
hi = thumbHi; | |
wi = (int)(source.Width * ((decimal)thumbHi / source.Height)); | |
} | |
} | |
thumbnail = new Bitmap(wi, hi); | |
using (Graphics g = Graphics.FromImage(thumbnail)) | |
{ | |
g.InterpolationMode = InterpolationMode.HighQualityBicubic; | |
g.FillRectangle(Brushes.Transparent, 0, 0, wi, hi); | |
g.DrawImage(source, 0, 0, wi, hi); | |
} | |
var thumbnailName = Path.Combine(filePath, "thmbnail_" + fileName); | |
thumbnail.Save(thumbnailName); | |
} | |
catch | |
{ | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment