Created
August 20, 2011 17:45
-
-
Save detroitpro/1159398 to your computer and use it in GitHub Desktop.
Image Resize Service
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.Drawing; | |
using System.IO; | |
using Uploaded.Core.Extensions; | |
using Uploaded.Core.Interfaces; | |
using Uploaded.Core.Model; | |
namespace Uploaded.Core.Services | |
{ | |
public class ImageResizerService : IImageResizerService | |
{ | |
private string _savePath; | |
private MemoryStream _stream; | |
public void Initialize(MemoryStream stream) | |
{ | |
//I'd normally use conditions, code contracts or AOP for these upfront assertions. | |
if (stream == null) | |
{ | |
throw new ArgumentException("stream can't be null. The most common use is Request.Files[0].InputStream"); | |
} | |
_stream = stream; | |
} | |
public void Initialize(ImageResizerRequest request) | |
{ | |
//I'd normally use conditions, code contracts or AOP for these upfront assertions. | |
if (request.InputMemoryStream == null) | |
{ | |
throw new ArgumentException("InputMemoryStream can't be null. The most common use is Request.Files[0].InputStream"); | |
} | |
_stream = request.InputMemoryStream; | |
if (!string.IsNullOrWhiteSpace(request.InputSaveFolder)) | |
{ | |
_savePath = request.InputSaveFolder; | |
} | |
else | |
{ | |
//using default path. (current directory/bin in this case) | |
//dead code. will be removed once working prototype. | |
} | |
} | |
public ImageResizerResponse Process() | |
{ | |
if (_stream == null) | |
{ | |
throw new ArgumentException("Use Initialize first to load the memory stream, save path and other parameters."); | |
} | |
//turn stream into image. | |
var image = _stream.ToImage(); | |
var newSize = CalulateDimensions(image.Width, image.Height); | |
var newImage = new Bitmap(image, newSize.Width, newSize.Height); | |
Graphics.FromImage(newImage).DrawImage(image, 0, 0, newSize.Width, newSize.Height); | |
var pathWithFilename = Path.Combine(@"E:\Projects\Uploaded\Uploaded.Tests\bin\Debug\", "test.png"); | |
newImage.Save(pathWithFilename); | |
var response = new ImageResizerResponse(){ }; | |
return response; | |
} | |
public CalulateDimensionsResponse CalulateDimensions(int sourceWidth, int sourceHeight) | |
{ | |
const int maxHeight = 700; | |
const int maxWidth = 500; | |
if (sourceHeight <= maxHeight && sourceWidth <= maxWidth) | |
{ | |
return new CalulateDimensionsResponse() { Height = sourceHeight, Width = sourceWidth }; | |
} | |
var ratioX = (double)maxWidth / sourceWidth; | |
var ratioY = (double)maxHeight / sourceHeight; | |
var ratio = Math.Min(ratioX, ratioY); | |
var newWidth = (int)(sourceWidth * ratio); | |
var newHeight = (int)(sourceHeight * ratio); | |
var calulatedWidth = newWidth; | |
var calulatedHeight = newHeight; | |
var result = new CalulateDimensionsResponse() {Height = calulatedHeight, Width = calulatedWidth}; | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
'newImage' doesn't get disposed here. The GC views Bitmap objects as tiny < 1KB objects - the 40-80MB of unmanaged memory is invisible to it. Those will stack up fast under load and crash the server.