Created
November 11, 2011 13:07
-
-
Save juanplopes/1357961 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.Text; | |
using System.Drawing.Imaging; | |
using System.Drawing; | |
using System.IO; | |
using System.Drawing.Drawing2D; | |
namespace UmNamespaceMuitoLouco | |
{ | |
public static class ImageHelpers | |
{ | |
public static byte[] Redimensionar(this byte[] byteArrayImage, TamanhoImagem tamanho, ImageFormat formato) | |
{ | |
using (byteArrayImage.ComoImagem()) | |
{ | |
if (imagem == null) return byteArrayImage; | |
float proporcao = imagem.CalcularProporcao(tamanho); | |
if (NecessitaCorrecao(formato, proporcao)) | |
{ | |
float newH = imagem.Height.CalcularNovoTamanho(proporcao); | |
float newW = imagem.Width.CalcularNovoTamanho(proporcao); | |
using (var thumb = imagem.CriaThumbnail(newH, newW)) | |
using (var graphic = thumb.PortarParaGrafico()) | |
{ | |
graphic.DrawImage(imagem, 0, 0, newW, newH); | |
byteArrayImage = thumb.ComoByteArray(imagem, formato); | |
} | |
} | |
} | |
return byteArrayImage; | |
} | |
private static Image ComoImagem(this byte[] imagem) | |
{ | |
Image img = null; | |
try | |
{ | |
img = Image.FromStream(new MemoryStream(imagem)); | |
} | |
catch (ArgumentException) { } | |
return img; | |
} | |
private static bool NecessitaCorrecao(ImageFormat format, float prop) | |
{ | |
return !prop.FloatEq(1, 1e-6f) || format != null; | |
} | |
private static float CalcularNovoTamanho(this int tamanho, float proporcao) | |
{ | |
return (float)Math.Round(tamanho / proporcao); | |
} | |
private static float CalcularProporcao(this Image imagem, TamanhoImagem tamanho) | |
{ | |
if (tamanho.Altura == null && tamanho.Largura == null) return 1f; | |
float proporcao = Math.Max( | |
imagem.Height / (tamanho.Altura ?? float.MaxValue), | |
imagem.Width / (tamanho.Largura ?? float.MaxValue)); | |
return proporcao; | |
} | |
private static Image CriaThumbnail(this Image imagem, float novaAltura, float novaLargura) | |
{ | |
var pixelFormat = imagem.PixelFormat; | |
if ((pixelFormat & PixelFormat.Indexed) != 0) pixelFormat = PixelFormat.Format64bppPArgb; | |
Image thumb = new Bitmap((int)novaLargura, (int)novaAltura, pixelFormat); | |
return thumb; | |
} | |
private static byte[] ComoByteArray(this Image thumb, Image imagem, ImageFormat formato) | |
{ | |
MemoryStream resultado = new MemoryStream(); | |
thumb.Save(resultado, formato ?? imagem.RawFormat); | |
byte[] array = resultado.GetBuffer(); | |
Array.Resize(ref array, (int)resultado.Length); | |
return array; | |
} | |
private static Graphics PortarParaGrafico(this Image thumb) | |
{ | |
Graphics graphic = Graphics.FromImage(thumb); | |
graphic.CompositingQuality = CompositingQuality.HighQuality; | |
graphic.SmoothingMode = SmoothingMode.HighQuality; | |
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic; | |
return graphic; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment