PROWAREtech

articles » archived » asp-net » jpeg-utility-library

ASP.NET: JPEG Utility Library

A tool to help Windows ASP.NET developers modify images (.NET Framework).

The JPEGLIB has been downgraded to just image sizing while EXIF needs have been delegated to EXIFTOOL by Phil Harvey as this is much more reliable.

NOTE: this library has been updated and improved in the ASP.NET Core section and it still applies to the ASP.NET Framework.

JPEGLIB now consists of this simple class and it actually supports more than JPEGs:

using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace prowaretech.com
{
	namespace JpegLib
	{
		public enum SaveImageAsType
		{
			JPEG,
			GIF,
			PNG,
			TIFF,
			WMF,
			ICON,
			BMP,
			EMF
		}
		public class ImageSizer
		{
			private int width;
			private int height;
			public ImageSizer(Stream fileStream)
			{
				Image iOriginal = new Bitmap(fileStream);
				width = iOriginal.Width;
				height = iOriginal.Height;
				iOriginal.Dispose();
			}
			public ImageSizer(string fileName)
			{
				Image iOriginal = new Bitmap(fileName);
				width = iOriginal.Width;
				height = iOriginal.Height;
				iOriginal.Dispose();
			}
			public int Width
			{
				get { return this.width; }
			}
			public int Height
			{
				get { return this.height; }
			}
		}
		public class ImageResizer
		{
			public void ResizeImage(string fileName, int newWidth, int newHeight, bool preserveImageRatio, string saveAsFileName, SaveImageAsType imageFormat)
			{
				Bitmap iOriginal = new Bitmap(fileName);

				if (preserveImageRatio)
				{
					float percentWidth = (float)newWidth / (float)iOriginal.Width;
					float percentHeight = (float)newHeight / (float)iOriginal.Height;
					float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
					newWidth = (int)Math.Round(iOriginal.Width * percent, 0);
					newHeight = (int)Math.Round(iOriginal.Height * percent, 0);
				}
				resize(iOriginal, newWidth, newHeight, saveAsFileName, imageFormat);

				iOriginal.Dispose();
			}
			public void ResizeImage(Stream fileStream, int newWidth, int newHeight, bool preserveImageRatio, string saveAsFileName, SaveImageAsType imageFormat)
			{
				Bitmap iOriginal = new Bitmap(fileStream);

				if (preserveImageRatio)
				{
					float percentWidth = (float)newWidth / (float)iOriginal.Width;
					float percentHeight = (float)newHeight / (float)iOriginal.Height;
					float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
					newWidth = (int)Math.Round(iOriginal.Width * percent, 0);
					newHeight = (int)Math.Round(iOriginal.Height * percent, 0);
				}
				resize(iOriginal, newWidth, newHeight, saveAsFileName, imageFormat);

				iOriginal.Dispose();
			}
			public void ResizeImage(string fileName, int newNumberOfPixels, string saveAsFileName, SaveImageAsType imageFormat)
			{
				Bitmap iOriginal = new Bitmap(fileName);

				double ratio = Math.Sqrt(newNumberOfPixels / (double)(iOriginal.Width * iOriginal.Height));
				resize(iOriginal, (int)Math.Round(iOriginal.Width * ratio, 0), (int)Math.Round(iOriginal.Height * ratio, 0), saveAsFileName, imageFormat);

				iOriginal.Dispose();
			}
			public void ResizeImage(Stream fileStream, int newNumberOfPixels, string saveAsFileName, SaveImageAsType imageFormat)
			{
				Bitmap iOriginal = new Bitmap(fileStream);

				double ratio = Math.Sqrt(newNumberOfPixels / (double)(iOriginal.Width * iOriginal.Height));
				resize(iOriginal, (int)Math.Round(iOriginal.Width * ratio, 0), (int)Math.Round(iOriginal.Height * ratio, 0), saveAsFileName, imageFormat);

				iOriginal.Dispose();
			}
			private void resize(Bitmap iOriginal, int newWidth, int newHeight, string saveAsFileName, SaveImageAsType imageType)
			{
				ImageFormat imageFormat;
				PixelFormat pixelFormat;
				switch (imageType)
				{
					case SaveImageAsType.BMP:
						imageFormat = ImageFormat.Bmp;
						pixelFormat = PixelFormat.Format24bppRgb;
						break;
					case SaveImageAsType.EMF:
						imageFormat = ImageFormat.Emf;
						pixelFormat = PixelFormat.Format24bppRgb;
						break;
					case SaveImageAsType.GIF:
						imageFormat = ImageFormat.Gif;
						pixelFormat = PixelFormat.Format8bppIndexed;
						break;
					case SaveImageAsType.ICON:
						imageFormat = ImageFormat.Icon;
						pixelFormat = PixelFormat.Format32bppArgb;
						break;
					case SaveImageAsType.JPEG:
						imageFormat = ImageFormat.Jpeg;
						pixelFormat = PixelFormat.Format24bppRgb;
						break;
					case SaveImageAsType.PNG:
						imageFormat = ImageFormat.Png;
						pixelFormat = PixelFormat.Format32bppArgb;
						break;
					case SaveImageAsType.TIFF:
						imageFormat = ImageFormat.Tiff;
						pixelFormat = PixelFormat.Format24bppRgb;
						break;
					case SaveImageAsType.WMF:
						imageFormat = ImageFormat.Wmf;
						pixelFormat = PixelFormat.Format24bppRgb;
						break;
					default:
						imageFormat = ImageFormat.Jpeg;
						pixelFormat = PixelFormat.Format24bppRgb;
						break;
				}
				Bitmap iSave = new Bitmap(newWidth, newHeight, pixelFormat);

				//Copy the original image over to the temp image
				Graphics gSave = Graphics.FromImage(iSave);
				gSave.DrawImage(iOriginal, 0, 0, newWidth, newHeight);
				gSave.Dispose();

				iSave.Save(saveAsFileName, imageFormat);
				iSave.Dispose();
			}
		}
	}
}

Using EXIFTOOL (See .NET processes for more information):

// this function returns all the exif data which is much faster than extracting the data one field at a time
string ExifExtractAllData(string imageFilePath)
{
	using (var proc = new System.Diagnostics.Process
	{
		StartInfo = new System.Diagnostics.ProcessStartInfo
		{
			FileName = "exiftool.exe", // make sure this path is found
			Arguments = '"' + imageFilePath + '"',
			UseShellExecute = false,
			RedirectStandardOutput = true,
			CreateNoWindow = true
		}
	})
	{
		proc.Start();
		if (!proc.StandardOutput.EndOfStream)
			return proc.StandardOutput.ReadToEnd();
	}
	return null;
}
// this parses the data returned by ExifExtractAllData()
// this returns an array of string values because "field" can be a wildcard,
// for example, every field beginning with Lens would be "Lens*"
public static string[] ExifExtractValue(string exifdata, string field)
{
	string ret = "\0";
	if (field.Length > 0 && field[0] == '-')
		field = field.Substring(1);
	bool multi = field.Contains('*');
	if (multi)
		field = field.Substring(0, field.IndexOf('*'));
	using (System.IO.StringReader reader = new System.IO.StringReader(exifdata))
	{
		while (true)
		{
			string line = reader.ReadLine();
			if (line == null)
				return ret.Split(new char[] { '\0' }, System.StringSplitOptions.RemoveEmptyEntries);
			int i = line.IndexOf(':');
			string f = line.Substring(0, i).Trim().Replace(" ", "");
			if (f == field || (multi && f.StartsWith(field)))
			{
				ret += (f + '|' + line.Substring(i + 1, line.Length - (i + 1)).Trim() + '\0');
				if (!multi)
					return ret.Split(new char[] { '\0' }, System.StringSplitOptions.RemoveEmptyEntries);
			}
		}
	}
}


This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
CLOSE