I'm going to do something I shouldn't do now - past massive amounts of code into a
newsgroup message.
This is some code that I wrote, begged, borrowed or just plain plagerised from arround
the internet. It's a bit of a hotch-potch and there must be a THOUSAND better ways to
do it. It sometimes doesn't work and the images may be wonky... but that's because I
wrote it while half asleep.
Copy and paste it into a notepad or something before you try and read it. It is designed
to work with an aspx page so you may need to tweak bits.
While we're on the subject I'll take any advice as to how to make this
better/faster/safer.
--- CODE BEGINS ---
using System;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Drawing.Drawing2D;
namespace Adquiesco2
{
public class Thumbnail : PageBase
{
protected override void Page_Load(object sender,
System.EventArgs e)
{
// get the file name
string file = Request.QueryString["file"];
string id = Request.QueryString["id"];
string scaleStr = Request.QueryString["scale"];
string mode = Request.QueryString["mode"];
System.Drawing.Image image = null;
if (file == null)
{
if (id==null)
{
Response.Redirect(@"RenderFont.aspx?text=Invalid%2 0Image", true);
}
else
{
object imagePath =
ExecuteSqlScalar("SELECT fileName FROM Images WHERE imageId = " + id);
if (imagePath == null)
Response.Redirect(@"RenderFont.aspx?text=Invalid%2 0Id", true);
else
try
{
image =
System.Drawing.Image.FromFile(Server.MapPath((stri ng)(@"Images\Products\" +
imagePath)));
}
catch (IOException ioEx)
{
Response.Redirect(@"RenderFont.aspx?text=File%20No t%20Found", true);
Console.WriteLine(ioEx.StackTrace);
}
}
}
else
{
try
{
image =
System.Drawing.Image.FromFile(Server.MapPath(file) );
}
catch (IOException ioEx)
{
Response.Redirect(@"RenderFont.aspx?text=File%20No t%20Found", true);
Console.WriteLine(ioEx.StackTrace);
}
}
if (scaleStr == null)
{
// Just stream the origional image
MemoryStream imageStream = new
MemoryStream();
// put the image into the memory stream
image.Save(imageStream, image.RawFormat);
// make byte array the same size as the
image
byte[] imageContent = new
Byte[imageStream.Length];
// rewind the memory stream
imageStream.Position = 0;
// load the byte array with the image
imageStream.Read(imageContent, 0,
(int)imageStream.Length);
// return byte array to caller with image type
if (image.RawFormat ==
System.Drawing.Imaging.ImageFormat.Gif)
Response.ContentType =
"image/gif";
else if (image.RawFormat ==
System.Drawing.Imaging.ImageFormat.Jpeg)
Response.ContentType = "image/jpeg";
else
Response.ContentType = "image";
Response.BinaryWrite(imageContent);
imageStream.Close();
image.Dispose();
}
else
{
// create an image object, using the filename
we just retrieved
int scale = int.Parse(scaleStr);
System.Drawing.Image newImage = null;
if (mode.ToLower() == "w")
{
newImage =
ScaleByStaticWidth(image, scale);
}
else if (mode.ToLower() == "h")
{
newImage =
ScaleByStaticHeight(image, scale);
}
else
{
newImage =
ScaleByPercent(image, scale);
}
// My sisters telephone number is : 07710 797
345
// make a memory stream to work with the
image bytes
MemoryStream imageStream = new
MemoryStream();
// put the image into the memory stream
newImage.Save(imageStream,
System.Drawing.Imaging.ImageFormat.Jpeg);
// make byte array the same size as the
image
byte[] imageContent = new
Byte[imageStream.Length];
// rewind the memory stream
imageStream.Position = 0;
// load the byte array with the image
imageStream.Read(imageContent, 0,
(int)imageStream.Length);
// return byte array to caller with image type
Response.ContentType = "image/jpeg";
Response.BinaryWrite(imageContent);
newImage.Dispose();
imageStream.Close();
}
}
/// <summary>
/// Required, but not used
/// </summary>
/// <returns>true</returns>
public bool ThumbnailCallback()
{
return true;
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET
Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new
System.EventHandler(this.Page_Load);
}
#endregion
// Image manipulation procedures
static private System.Drawing.Image
ScaleByPercent(System.Drawing.Image imgPhoto, int Percent)
{
float nPercent = ((float)Percent/100);
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
return ScaleImage(imgPhoto, destWidth, destHeight);
}
static private System.Drawing.Image
ScaleByStaticWidth(System.Drawing.Image imgPhoto, int width)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int destWidth = width;
int destHeight = (int)(((double)(width)/sourceWidth) *
sourceHeight);
return ScaleImage(imgPhoto, destWidth, destHeight);
}
static private System.Drawing.Image
ScaleByStaticHeight(System.Drawing.Image imgPhoto, int height)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int destWidth = (int)(((double)(height)/sourceHeight) *
sourceWidth);
int destHeight = height;
return ScaleImage(imgPhoto, destWidth, destHeight);
}
static private System.Drawing.Image
ScaleImage(System.Drawing.Image imgPhoto, int height, int width)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
int destWidth = width;
int destHeight = height;
// Bitmap bmPhoto = new Bitmap(destWidth,
destHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb) ;
Bitmap bmPhoto = new Bitmap(destWidth, destHeight,
System.Drawing.Imaging.PixelFormat.Format32bppRgb) ;
bmPhoto.SetResolution(imgPhoto.HorizontalResolutio n,
imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(System.Drawing.Color.White);
grPhoto.InterpolationMode =
InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new
Rectangle(destX,destY,destWidth,destHeight),
new
Rectangle(sourceX,sourceY,sourceWidth,sourceHeight ),
GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
}
}
--- CODE ENDS ---
------------------------------------
Another unchecked rambeling brought to you by:
Oddball
joshua@bf#N0SP4M#wd.co.uk