using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace SitePadrao.util
{
public partial class VerImagem : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false && Request.QueryString["caminho"] != null)
{
int Proporcao = Convert.ToInt32(Request.QueryString["Proporcao"]);
Response.Clear();
Response.ContentType = "image/jpeg";
string[] nomes = Request.QueryString["caminho"].ToString().Split('\\');
Response.AppendHeader("Content-Disposition", "attachment; filename=dove_sahara_" + nomes[nomes.Length - 1].Replace(".JPG", "") + ".jpg");
String caminho = Server.MapPath("~/" + Request.QueryString["caminho"]);
FileInfo f = new FileInfo(caminho);
if(f.Exists == false)
return;
System.Drawing.Image img = Bitmap.FromFile(caminho);
byte[] b = ConvertImageToByteArray(img, ImageFormat.Jpeg);
Response.BinaryWrite(ResizeImageFile(b, Proporcao));
Response.End();
}
}
//Converte a imagem para um byte[] para que possa ser desenhada na tela
private static byte[] ConvertImageToByteArray(System.Drawing.Image imageToConvert, ImageFormat formatOfImage)
{
byte[] Ret;
try
{
using (MemoryStream ms = new MemoryStream())
{
imageToConvert.Save(ms, formatOfImage);
Ret = ms.ToArray();
}
}
catch (Exception) { throw; }
return Ret;
}
//Faz o redimensionamento proporcional da imagem
public static byte[] ResizeImageFile(byte[] imageFile, int targetSize)
{
Image original = Image.FromStream(new MemoryStream(imageFile));
int targetH, targetW;
if (original.Height > original.Width)
{
targetH = targetSize;
targetW = (int)(original.Width * ((float)targetSize / (float)original.Height));
}
else
{
targetW = targetSize;
targetH = (int)(original.Height * ((float)targetSize / (float)original.Width));
}
Image imgPhoto = Image.FromStream(new MemoryStream(imageFile));
// Create a new blank canvas. The resized image will be drawn on this canvas.
Bitmap bmPhoto = new Bitmap(targetW, targetH, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(72, 72);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;
grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, targetW, targetH), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel);
// Save out to memory and then to a file. We dispose of all objects to make sure the files don't stay locked.
MemoryStream mm = new MemoryStream();
bmPhoto.Save(mm, System.Drawing.Imaging.ImageFormat.Jpeg);
original.Dispose();
imgPhoto.Dispose();
bmPhoto.Dispose();
grPhoto.Dispose();
return mm.GetBuffer();
}
}
}
Agora quando for chamar a imagem você poderá chamala assim:
<img src='VerImagem.aspx?caminho=/foto/minhaFoto.jpg&Proporcao=400'/>
ou
versão menor
<img src='VerImagem.aspx?caminho=/foto/minhaFoto.jpg&Proporcao=100'/>