- UploadImage.zip
- UploadImage
- packages
- Antlr.3.4.1.9004
- Antlr.3.4.1.9004.nupkg
- lib
- Antlr3.Runtime.dll
- Antlr3.Runtime.pdb
- bootstrap.3.0.0
- bootstrap.3.0.0.nupkg
- content
- jQuery.1.10.2
- Content
- Scripts
- jQuery.1.10.2.nupkg
- Tools
- jQuery.Validation.1.11.1
- Content
- Scripts
- jQuery.Validation.1.11.1.nupkg
- Microsoft.AspNet.Mvc.5.2.0
- Content
- Web.config.install.xdt
- Web.config.uninstall.xdt
- lib
- net45
- Microsoft.AspNet.Mvc.5.2.0.nupkg
- Microsoft.AspNet.Razor.3.2.0
- lib
- net45
- Microsoft.AspNet.Razor.3.2.0.nupkg
- Microsoft.AspNet.Web.Optimization.1.1.3
- lib
- net40
- Microsoft.AspNet.Web.Optimization.1.1.3.nupkg
- Microsoft.AspNet.WebPages.3.2.0
- Content
- Web.config.install.xdt
- Web.config.uninstall.xdt
- lib
- net45
- Microsoft.AspNet.WebPages.3.2.0.nupkg
- Microsoft.jQuery.Unobtrusive.Validation.3.2.0
- Content
- Scripts
- Microsoft.jQuery.Unobtrusive.Validation.3.2.0.nupkg
- Microsoft.Web.Infrastructure.1.0.0.0
- lib
- net40
- Microsoft.Web.Infrastructure.dll
- Microsoft.Web.Infrastructure.1.0.0.0.nupkg
- Newtonsoft.Json.6.0.3
- lib
- net20
- net35
- net40
- net45
- netcore45
- portable-net40%2Bsl4%2Bwp7%2Bwin8
- portable-net40+sl4+wp7+win8
- portable-net45%2Bwp80%2Bwin8%2Bwpa81
- portable-net45+wp80+win8+wpa81
- Newtonsoft.Json.6.0.3.nupkg
- tools
- repositories.config
- Respond.1.2.0
- content
- Scripts
- Respond.1.2.0.nupkg
- WebGrease.1.5.2
- lib
- WebGrease.dll
- tools
- WG.exe
- WebGrease.1.5.2.nupkg
- UploadImage.sln
- UploadImage.v11.suo
- UploadImage.v12.suo
- UploadImage
- UploadImage-noexe.zip
|
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using UploadImage.POCO;
namespace UploadImage.Lib
{
public class ImageUpload
{
// set default size here
public int Width { get; set; }
public int Height { get; set; }
// folder for the upload, you can put this in the web.config
private readonly string UploadPath = "~/Images/Items/";
public ImageResult RenameUploadFile(HttpPostedFileBase file, Int32 counter = 0)
{
var fileName = Path.GetFileName(file.FileName);
string prepend = "item_";
string finalFileName = prepend + ((counter).ToString()) + "_" + fileName;
if (System.IO.File.Exists
(HttpContext.Current.Request.MapPath(UploadPath + finalFileName)))
{
//file exists => add country try again
return RenameUploadFile(file, ++counter);
}
//file doesn't exist, upload item but validate first
return UploadFile(file, finalFileName);
}
private ImageResult UploadFile(HttpPostedFileBase file, string fileName)
{
ImageResult imageResult = new ImageResult { Success = true, ErrorMessage = null };
var path =
Path.Combine(HttpContext.Current.Request.MapPath(UploadPath), fileName);
string extension = Path.GetExtension(file.FileName);
//make sure the file is valid
if (!ValidateExtension(extension))
{
imageResult.Success = false;
imageResult.ErrorMessage = "Invalid Extension";
return imageResult;
}
try
{
file.SaveAs(path);
Image imgOriginal = Image.FromFile(path);
//pass in whatever value you want
Image imgActual = Scale(imgOriginal);
imgOriginal.Dispose();
imgActual.Save(path);
imgActual.Dispose();
imageResult.ImageName = fileName;
return imageResult;
}
catch (Exception ex)
{
// you might NOT want to show the exception error for the user
// this is generaly logging or testing
imageResult.Success = false;
imageResult.ErrorMessage = ex.Message;
return imageResult;
}
}
private bool ValidateExtension(string extension)
{
extension = extension.ToLower();
switch (extension)
{
case ".jpg":
return true;
case ".png":
return true;
case ".gif":
return true;
case ".jpeg":
return true;
default:
return false;
}
}
private Image Scale(Image imgPhoto)
{
float sourceWidth = imgPhoto.Width;
float sourceHeight = imgPhoto.Height;
float destHeight = 0;
float destWidth = 0;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
// force resize, might distort image
if (Width != 0 && Height != 0)
{
destWidth = Width;
destHeight = Height;
}
// change size proportially depending on width or height
else if (Height != 0)
{
destWidth = (float)(Height * sourceWidth) / sourceHeight;
destHeight = Height;
}
else
{
destWidth = Width;
destHeight = (float)(sourceHeight * Width / sourceWidth);
}
Bitmap bmPhoto = new Bitmap((int)destWidth, (int)destHeight,
PixelFormat.Format32bppPArgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, (int)destWidth, (int)destHeight),
new Rectangle(sourceX, sourceY, (int)sourceWidth, (int)sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Passionate Software Developer with over 3 years of experience in the field. In love with the girl next door and the MVC architecture.