Make Thumb
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
namespace Hdnug
{
/// <summary>
/// Summary description for ImageUtils
/// </summary>
public class ImageUtils
{
const int sizeThumb = 69;
public static byte[] MakeThumb(byte[] fullsize)
{
Image iOriginal, iThumb;
int targetH, targetW;
iOriginal = Image.FromStream(new MemoryStream(fullsize));
if (iOriginal.Height > iOriginal.Width)
{
targetH = sizeThumb;
targetW = Convert.ToInt32(iOriginal.Width * (sizeThumb / iOriginal.Height));
}
else
{
targetW = sizeThumb;
targetH = Convert.ToInt32(iOriginal.Height * (sizeThumb / iOriginal.Width));
}
iThumb = iOriginal.GetThumbnailImage(targetW, targetH, null, System.IntPtr.Zero);
MemoryStream m = new MemoryStream();
iThumb.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);
return m.GetBuffer();
}
public static byte[] MakeThumb(byte[] fullsize, int newwidth, int newheight)
{
Image iOriginal, iThumb;
double scaleH, scaleW;
Rectangle srcRect = new Rectangle();
iOriginal = Image.FromStream(new MemoryStream(fullsize));
scaleH = iOriginal.Height / newheight;
scaleW = iOriginal.Width / newwidth;
if (scaleH == scaleW)
{
srcRect.Width = iOriginal.Width;
srcRect.Height = iOriginal.Height;
srcRect.X = 0;
srcRect.Y = 0;
}
else if ((scaleH) > (scaleW))
{
srcRect.Width = iOriginal.Width;
srcRect.Height = Convert.ToInt32(newheight * scaleW);
srcRect.X = 0;
srcRect.Y = Convert.ToInt32((iOriginal.Height - srcRect.Height) / 2);
}
else
{
srcRect.Width = Convert.ToInt32(newwidth * scaleH);
srcRect.Height = iOriginal.Height;
srcRect.X = Convert.ToInt32((iOriginal.Width - srcRect.Width) / 2);
srcRect.Y = 0;
}
iThumb = new Bitmap(newwidth, newheight);
Graphics g = Graphics.FromImage(iThumb);
g.DrawImage(iOriginal, new Rectangle(0, 0, newwidth, newheight), srcRect, GraphicsUnit.Pixel);
MemoryStream m = new MemoryStream();
iThumb.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);
return m.GetBuffer();
}
public static byte[] MakeThumb(byte[] fullsize, int maxwidth)
{
Image iOriginal, iThumb;
double scale;
iOriginal = Image.FromStream(new MemoryStream(fullsize));
if (iOriginal.Width > maxwidth)
{
scale = iOriginal.Width / maxwidth;
int newheight = Convert.ToInt32(iOriginal.Height / scale);
iThumb = new Bitmap(iOriginal, maxwidth, newheight);
MemoryStream m = new MemoryStream();
iThumb.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);
return m.GetBuffer();
}
else
{
return fullsize;
}
}
}
}
Related examples in the same category