using System; using System.Collections.Generic; using System.IO; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; using System.Net; using System.Security.Permissions; using WebSupergoo.ImageGlue5; using WebSupergoo.ImageGlue5.GDIPlus; namespace DH.Drawing.Imaging { public enum ImageOptions { OptimizeMaxWidth = 1, OptimizeMaxHeight = 2, OptimizeMaxWidthAndMaxHeight = 3, OptimizeDefault = 4, OptimizeFillDefault = 5 } public class Images { public Images() { ClearParams(); } public Images(Image imageSource) { ClearParams(); _srcImage = (Image)imageSource.Clone(); } public Images(string fullImageSourcePath) { ClearParams(); _fullSourceImagePath = fullImageSourcePath; if (_srcImage != null) _srcImage.Dispose(); _srcImage = Image.FromFile(fullImageSourcePath); } #region Constants private const String MIME_JPEG = "image/jpeg"; private const String MIME_PJPEG = "image/pjpeg"; private const String MIME_GIF = "image/gif"; private const String MIME_BMP = "image/bmp"; private const String MIME_TIFF = "image/tiff"; private const String MIME_PNG = "image/png"; #endregion #region Members private ImageFormat _imageFormat = ImageFormat.Jpeg; private double _scaleFactor = 1; private ImageOptions _imageOption = ImageOptions.OptimizeDefault; private Color _backgroundColor = Color.White; private double _maxWidth = 100.0; private double _maxHeight = 70.0; private double _scaleX = 1; private double _scaleY = 1; private Image _srcImage = null; private string _fullSourceImagePath = string.Empty; private string _fullTargetImagePath = string.Empty; private string _sourceImagePath = string.Empty; private string _targetImagePath = string.Empty; private string _sourceImageName = string.Empty; private string _targetImageName = string.Empty; private Uri _fullSourceImageURI = null; private string _targetNamePrefix = string.Empty; private static String[] supportedMimeTypes = new String[] { MIME_GIF, MIME_JPEG, MIME_PJPEG, MIME_TIFF, MIME_PNG, MIME_BMP }; private int _quality = -1; private Stream _fileStream = null; #endregion #region Properties public Color BackgroundColor { get { return _backgroundColor; } set { _backgroundColor = value; } } public double MaxWidth { get { return _maxWidth; } set { _maxWidth = value; } } public double MaxHeight { get { return _maxHeight; } set { _maxHeight = value; } } public Image SrcImage { get { return _srcImage; } set { _srcImage = value; } } public ImageOptions ImageOption { get { return _imageOption; } set { _imageOption = value; } } public string FullSourceImagePath { get { return _fullSourceImagePath; } set { _fullSourceImagePath = value; } } public string FullTargetImagePath { get { return _fullTargetImagePath; } set { _fullTargetImagePath = value; } } public string SourceImagePath { get { return _sourceImagePath; } set { _sourceImagePath = value; } } public string TargetImagePath { get { return _targetImagePath; } set { _targetImagePath = value; } } public string SourceImageName { get { return _sourceImageName; } set { _sourceImageName = value; } } public string TargetImageName { get { return _targetImageName; } set { _targetImageName = value; } } public Uri FullSourceImageURL { get { return _fullSourceImageURI; } set { _fullSourceImageURI = value; } } public string ThumbnailNamePrefix { get { return _targetNamePrefix; } set { _targetNamePrefix = value; } } public int JPEGCompressionQuality { get { return _quality; } set { _quality = value; } } public Stream UploadFileStream { get { return _fileStream; } set { _fileStream = value; } } #endregion #region GetStream public Stream GetStream() { if (_srcImage.Equals(null) && _fullSourceImagePath.Equals(string.Empty) == false) _srcImage = Image.FromFile(_fullSourceImagePath); if (_srcImage != null && _srcImage.GetType().Equals(typeof(Bitmap)) == true) return GetStream(_srcImage, _maxWidth, _maxHeight, _imageOption, _backgroundColor); else throw new Exception("Non Image-Object defined!"); } public Stream GetStream(Image imageSource) { return GetStream((Image)imageSource.Clone(), _maxWidth, _maxHeight, _imageOption, _backgroundColor); } public Stream GetStream(Image imageSource, double maxWidth) { _maxWidth = maxWidth; _imageOption = ImageOptions.OptimizeMaxWidth; return GetStream((Image)imageSource.Clone(), _maxWidth, _maxHeight, _imageOption, _backgroundColor); } public Stream GetStream(Image imageSource, double maxWidth, double maxHeight) { _maxWidth = maxWidth; _maxHeight = maxHeight; _imageOption = ImageOptions.OptimizeMaxWidthAndMaxHeight; return GetStream((Image)imageSource.Clone(), _maxWidth, _maxHeight, _imageOption, _backgroundColor); } public Stream GetStream(Image imageSource, double maxWidth, double maxHeight, ImageOptions imageOption) { _maxWidth = maxWidth; _maxHeight = maxHeight; _imageOption = imageOption; return GetStream((Image)imageSource.Clone(), _maxWidth, _maxHeight, _imageOption, _backgroundColor); } public Stream GetStream(Image imageSource, double maxWidth, double maxHeight, ImageOptions imageOption, Color backgroundColor) { PixelFormat _pixelFormat = PixelFormat.Format32bppArgb; if (imageSource.RawFormat.Guid.Equals(ImageFormat.Jpeg.Guid)) { _imageFormat = ImageFormat.Jpeg; _pixelFormat = PixelFormat.Format24bppRgb; } else if (imageSource.RawFormat.Guid.Equals(ImageFormat.Gif.Guid)) { _imageFormat = ImageFormat.Gif; _pixelFormat = PixelFormat.Format8bppIndexed; } else if (imageSource.RawFormat.Guid.Equals(ImageFormat.Bmp.Guid)) { _imageFormat = ImageFormat.Bmp; } else if (imageSource.RawFormat.Guid.Equals(ImageFormat.Png.Guid)) { _imageFormat = ImageFormat.Png; _pixelFormat = PixelFormat.Format32bppArgb; } else if (imageSource.RawFormat.Guid.Equals(ImageFormat.Tiff.Guid)) { _imageFormat = ImageFormat.Tiff; _pixelFormat = PixelFormat.Format24bppRgb; } else if (imageSource.RawFormat.Guid.Equals(ImageFormat.Wmf.Guid)) { _imageFormat = ImageFormat.Wmf; _pixelFormat = PixelFormat.Undefined; } else { _imageFormat = ImageFormat.Jpeg; _pixelFormat = PixelFormat.Format24bppRgb; } _maxWidth = maxWidth; _maxHeight = maxHeight; _imageOption = imageOption; _backgroundColor = backgroundColor; SetScaleFactor(imageSource); Image thumbNail = imageSource.GetThumbnailImage((int)(imageSource.Size.Width * _scaleX), (int)(imageSource.Size.Height * _scaleY), null, IntPtr.Zero); Graphics g2 = Graphics.FromImage(thumbNail); g2.CompositingQuality = CompositingQuality.HighQuality; g2.SmoothingMode = SmoothingMode.HighQuality; g2.InterpolationMode = InterpolationMode.HighQualityBicubic; Rectangle destinationRect = new Rectangle(0, 0, thumbNail.Size.Width, thumbNail.Size.Height); g2.DrawImage(thumbNail, destinationRect, 0, 0, thumbNail.Size.Width, thumbNail.Size.Height, GraphicsUnit.Pixel); g2.Dispose(); Stream _stream = new MemoryStream(); if (_imageOption == ImageOptions.OptimizeFillDefault) { Bitmap _thumbNail = new Bitmap((int)_maxWidth, (int)_maxHeight, _pixelFormat); Graphics g1 = Graphics.FromImage(_thumbNail); Rectangle baseRect = new Rectangle(0, 0, _thumbNail.Size.Width, _thumbNail.Size.Height); Brush brush = new SolidBrush(_backgroundColor); g1.FillRectangle(brush, baseRect); g1.DrawImage(_thumbNail, baseRect, 0, 0, _thumbNail.Size.Width, _thumbNail.Size.Height, GraphicsUnit.Pixel); g1.CompositingQuality = CompositingQuality.HighQuality; g1.SmoothingMode = SmoothingMode.HighQuality; g1.InterpolationMode = InterpolationMode.HighQualityBicubic; Point point = new Point(_thumbNail.Size.Width / 2 - thumbNail.Size.Width / 2, _thumbNail.Size.Height / 2 - thumbNail.Size.Height / 2); Rectangle drawRect = new Rectangle(point.X, point.Y, thumbNail.Width, thumbNail.Height); g1.DrawImage(thumbNail, drawRect, destinationRect, GraphicsUnit.Pixel); g1.Dispose(); _thumbNail.Save(_stream, _imageFormat); _thumbNail.Dispose(); _thumbNail = null; } if (_imageOption != ImageOptions.OptimizeFillDefault) { thumbNail.Save(_stream, _imageFormat); } thumbNail.Dispose(); thumbNail = null; return _stream; } public Stream GetStream(string imageSourcePath) { return GetStream(Image.FromFile(imageSourcePath), _maxWidth, _maxHeight, _imageOption, _backgroundColor); } public Stream GetStream(string imageSourcePath, double maxWidth) { _maxWidth = maxWidth; _imageOption = ImageOptions.OptimizeMaxWidth; return GetStream(Image.FromFile(imageSourcePath), _maxWidth, _maxHeight, _imageOption, _backgroundColor); } public Stream GetStream(string imageSourcePath, double maxWidth, double maxHeight) { _maxWidth = maxWidth; _maxHeight = maxHeight; _imageOption = ImageOptions.OptimizeMaxWidthAndMaxHeight; return GetStream(Image.FromFile(imageSourcePath), _maxWidth, _maxHeight, _imageOption, _backgroundColor); } public Stream GetStream(string imageSourcePath, double maxWidth, double maxHeight, ImageOptions imageOption) { _maxWidth = maxWidth; _maxHeight = maxHeight; _imageOption = imageOption; return GetStream(Image.FromFile(imageSourcePath), _maxWidth, _maxHeight, _imageOption, _backgroundColor); } public Stream GetStream(string imageSourcePath, double maxWidth, double maxHeight, ImageOptions imageOption, Color backgroundColor) { _maxWidth = maxWidth; _maxHeight = maxHeight; _imageOption = imageOption; _backgroundColor = backgroundColor; return GetStream(Image.FromFile(imageSourcePath), _maxWidth, _maxHeight, _imageOption, _backgroundColor); } #endregion #region GetImage public Image GetImage() { return Image.FromStream(GetStream()); } public Image GetImage(Image imageSource, double maxWidth) { return Image.FromStream(GetStream(imageSource, maxWidth)); } public Image GetImage(Image imageSource, double maxWidth, double maxHeight) { return Image.FromStream(GetStream(imageSource, maxWidth, maxHeight)); } public Image GetImage(Image imageSource, double maxWidth, double maxHeight, ImageOptions imageOption) { return Image.FromStream(GetStream(imageSource, maxWidth, maxHeight, imageOption)); } public Image GetImage(Image imageSource, double maxWidth, double maxHeight, ImageOptions imageOption, Color backgroundColor) { return Image.FromStream(GetStream(imageSource, maxWidth, maxHeight, imageOption, backgroundColor)); } public Image GetImage(string imageSourcePath) { return Image.FromStream(GetStream(imageSourcePath)); } public Image GetImage(string imageSourcePath, double maxWidth) { return Image.FromStream(GetStream(imageSourcePath, maxWidth)); } public Image GetImage(string imageSourcePath, double maxWidth, double maxHeight) { return Image.FromStream(GetStream(imageSourcePath, maxWidth, maxHeight)); } public Image GetImage(string imageSourcePath, double maxWidth, double maxHeight, ImageOptions imageOption) { return Image.FromStream(GetStream(imageSourcePath, maxWidth, maxHeight, imageOption)); } public Image GetImage(string imageSourcePath, double maxWidth, double maxHeight, ImageOptions imageOption, Color backgroundColor) { return Image.FromStream(GetStream(imageSourcePath, maxWidth, maxHeight, imageOption, backgroundColor)); } public Image GetImage(Stream imageSourceStream) { return Image.FromStream(imageSourceStream); } public Image GetImage(Stream imageSourceStream, double maxWidth) { return Image.FromStream(GetStream(GetImage(imageSourceStream), maxWidth)); } public Image GetImage(Stream imageSourceStream, double maxWidth, double maxHeight) { return Image.FromStream(GetStream(GetImage(imageSourceStream), maxWidth, maxHeight)); } public Image GetImage(Stream imageSourceStream, double maxWidth, double maxHeight, ImageOptions imageOption) { return Image.FromStream(GetStream(GetImage(imageSourceStream), maxWidth, maxHeight, imageOption)); } public Image GetImage(Stream imageSourceStream, double maxWidth, double maxHeight, ImageOptions imageOption, Color backgroundColor) { return Image.FromStream(GetStream(GetImage(imageSourceStream), maxWidth, maxHeight, imageOption, backgroundColor)); } #endregion #region GetBitmap public Bitmap GetBitmap() { return new Bitmap(GetStream()); } public Bitmap GetBitmap(Image imageSource) { return new Bitmap(GetStream(imageSource)); } public Bitmap GetBitmap(Image imageSource, double maxWidth) { return new Bitmap(GetStream(imageSource, maxWidth)); } public Bitmap GetBitmap(Image imageSource, double maxWidth, double maxHeight) { return new Bitmap(GetStream(imageSource, maxWidth, maxHeight)); } public Bitmap GetBitmap(Image imageSource, double maxWidth, double maxHeight, ImageOptions imageOption) { return new Bitmap(GetStream(imageSource, maxWidth, maxHeight, imageOption)); } public Bitmap GetBitmap(Image imageSource, double maxWidth, double maxHeight, ImageOptions imageOption, Color backgroundColor) { return new Bitmap(GetStream(imageSource, maxWidth, maxHeight, imageOption, backgroundColor)); } public Bitmap GetBitmap(string imageSourcePath) { return new Bitmap(GetStream(imageSourcePath)); } public Bitmap GetBitmap(string imageSourcePath, double maxWidth) { return new Bitmap(GetStream(imageSourcePath, maxWidth)); } public Bitmap GetBitmap(string imageSourcePath, double maxWidth, double maxHeight) { return new Bitmap(GetStream(imageSourcePath, maxWidth, maxHeight)); } public Bitmap GetBitmap(string imageSourcePath, double maxWidth, double maxHeight, ImageOptions imageOption) { return new Bitmap(GetStream(imageSourcePath, maxWidth, maxHeight, imageOption)); } public Bitmap GetBitmap(string imageSourcePath, double maxWidth, double maxHeight, ImageOptions imageOption, Color backgroundColor) { return new Bitmap(GetStream(imageSourcePath, maxWidth, maxHeight, imageOption, backgroundColor)); } #endregion #region CreateThumbnail public bool CreateThumbnail() { if (_targetNamePrefix == string.Empty) _targetNamePrefix = "100_"; if (CheckPath()) { if (_fullSourceImageURI == null) return CreateThumbnail(_fullSourceImagePath, _fullTargetImagePath, _imageOption, _maxWidth, _maxHeight, _backgroundColor); else return CreateThumbnail(_fullSourceImageURI, _fullTargetImagePath, _imageOption, _maxWidth, _maxHeight, _backgroundColor); } else return false; } public bool CreateThumbnail(string fullSourceImagePath, string fullThumbnailPath) { _fullSourceImagePath = fullSourceImagePath; _fullTargetImagePath = fullThumbnailPath; return CreateThumbnail(); } public bool CreateThumbnail(string fullSourceImagePath, string fullThumbnailPath, ImageOptions imageOption) { _fullSourceImagePath = fullSourceImagePath; _fullTargetImagePath = fullThumbnailPath; _imageOption = imageOption; return CreateThumbnail(); } public bool CreateThumbnail(string fullSourceImagePath, string fullThumbnailPath, ImageOptions imageOption, double maxWidth) { _fullSourceImagePath = fullSourceImagePath; _fullTargetImagePath = fullThumbnailPath; _imageOption = imageOption; _maxWidth = maxWidth; return CreateThumbnail(); } public bool CreateThumbnail(string fullSourceImagePath, string fullThumbnailPath, ImageOptions imageOption, double maxWidth, Color backColor) { _fullSourceImagePath = fullSourceImagePath; _fullTargetImagePath = fullThumbnailPath; _imageOption = imageOption; _maxWidth = maxWidth; _backgroundColor = backColor; return CreateThumbnail(); } public bool CreateThumbnail(string fullSourceImagePath, string fullThumbnailPath, ImageOptions imageOption, double maxWidth, double maxHeight) { _fullSourceImagePath = fullSourceImagePath; _fullTargetImagePath = fullThumbnailPath; _imageOption = imageOption; _maxWidth = maxWidth; _maxHeight = maxHeight; return CreateThumbnail(); } public bool CreateThumbnail(string fullSourceImagePath, string fullThumbnailPath, ImageOptions imageOption, double maxWidth, double maxHeight, Color backColor) { bool returnValue = true; _fullSourceImagePath = fullSourceImagePath; _fullTargetImagePath = fullThumbnailPath; _imageOption = imageOption; _maxWidth = maxWidth; _maxHeight = maxHeight; _backgroundColor = backColor; try { Bitmap.FromStream(GetStream(_fullSourceImagePath)).Save(_fullTargetImagePath); } catch { returnValue = false; } return returnValue; } public bool CreateThumbnail(Uri fullSourceImageURI, string fullThumbnailPath) { _fullSourceImageURI = fullSourceImageURI; _fullTargetImagePath = fullThumbnailPath; return CreateThumbnail(); } public bool CreateThumbnail(Uri fullSourceImageURI, string fullThumbnailPath, ImageOptions imageOption) { _fullSourceImageURI = fullSourceImageURI; _fullTargetImagePath = fullThumbnailPath; _imageOption = imageOption; return CreateThumbnail(); } public bool CreateThumbnail(Uri fullSourceImageURI, string fullThumbnailPath, ImageOptions imageOption, double maxWidth) { _fullSourceImageURI = fullSourceImageURI; _fullTargetImagePath = fullThumbnailPath; _imageOption = imageOption; _maxWidth = maxWidth; return CreateThumbnail(); } public bool CreateThumbnail(Uri fullSourceImageURI, string fullThumbnailPath, ImageOptions imageOption, double maxWidth, Color backColor) { _fullSourceImageURI = fullSourceImageURI; _fullTargetImagePath = fullThumbnailPath; _imageOption = imageOption; _maxWidth = maxWidth; _backgroundColor = backColor; return CreateThumbnail(); } public bool CreateThumbnail(Uri fullSourceImageURI, string fullThumbnailPath, ImageOptions imageOption, double maxWidth, double maxHeight) { _fullSourceImageURI = fullSourceImageURI; _fullTargetImagePath = fullThumbnailPath; _imageOption = imageOption; _maxWidth = maxWidth; _maxHeight = maxHeight; return CreateThumbnail(); } public bool CreateThumbnail(Uri fullSourceImageURI, string fullThumbnailPath, ImageOptions imageOption, double maxWidth, double maxHeight, Color backColor) { bool returnValue = true; _fullSourceImageURI = fullSourceImageURI; _fullTargetImagePath = fullThumbnailPath; _imageOption = imageOption; _maxWidth = maxWidth; _maxHeight = maxHeight; _backgroundColor = backColor; try { Bitmap.FromStream(GetStream()).Save(_fullTargetImagePath); } catch { returnValue = false; } return returnValue; } #endregion #region CreateImage public bool CreateImage() { if (CheckPath()) { if (_fullSourceImageURI == null) return CreateThumbnail(_fullSourceImagePath, _fullTargetImagePath, _imageOption, _maxWidth, _maxHeight, _backgroundColor); else return CreateThumbnail(_fullSourceImageURI, _fullTargetImagePath, _imageOption, _maxWidth, _maxHeight, _backgroundColor); } else return false; } public bool CreateImage(string fullSourceImagePath, string fullTargetImagePath) { _fullSourceImagePath = fullSourceImagePath; _fullTargetImagePath = fullTargetImagePath; return CreateImage(); } public bool CreateImage(string fullSourceImagePath, string fullTargetImagePath, ImageOptions imageOption) { _fullSourceImagePath = fullSourceImagePath; _fullTargetImagePath = fullTargetImagePath; _imageOption = imageOption; return CreateImage(); } public bool CreateImage(string fullSourceImagePath, string fullTargetImagePath, ImageOptions imageOption, double maxWidth) { _fullSourceImagePath = fullSourceImagePath; _fullTargetImagePath = fullTargetImagePath; _imageOption = imageOption; _maxWidth = maxWidth; return CreateImage(); } public bool CreateImage(string fullSourceImagePath, string fullTargetImagePath, ImageOptions imageOption, double maxWidth, Color backColor) { _fullSourceImagePath = fullSourceImagePath; _fullTargetImagePath = fullTargetImagePath; _imageOption = imageOption; _maxWidth = maxWidth; _backgroundColor = backColor; return CreateImage(); } public bool CreateImage(string fullSourceImagePath, string fullTargetImagePath, ImageOptions imageOption, double maxWidth, double maxHeight) { _fullSourceImagePath = fullSourceImagePath; _fullTargetImagePath = fullTargetImagePath; _imageOption = imageOption; _maxWidth = maxWidth; _maxHeight = maxHeight; return CreateImage(); } public bool CreateImage(string fullSourceImagePath, string fullTargetImagePath, ImageOptions imageOption, double maxWidth, double maxHeight, Color backColor) { bool returnValue = true; _fullSourceImagePath = fullSourceImagePath; _fullTargetImagePath = fullTargetImagePath; _imageOption = imageOption; _maxWidth = maxWidth; _maxHeight = maxHeight; _backgroundColor = backColor; try { System.Threading.Thread.Sleep(5000); Bitmap.FromStream(GetStream(_fullSourceImagePath)).Save(_fullTargetImagePath); } catch { returnValue = false; } return returnValue; } public bool CreateImage(Uri fullSourceImageURI, string fullTargetImagePath) { _fullSourceImageURI = fullSourceImageURI; _fullTargetImagePath = fullTargetImagePath; return CreateImage(); } public bool CreateImage(Uri fullSourceImageURI, string fullTargetImagePath, ImageOptions imageOption) { _fullSourceImageURI = fullSourceImageURI; _fullTargetImagePath = fullTargetImagePath; _imageOption = imageOption; return CreateImage(); } public bool CreateImage(Uri fullSourceImageURI, string fullTargetImagePath, ImageOptions imageOption, double maxWidth) { _fullSourceImageURI = fullSourceImageURI; _fullTargetImagePath = fullTargetImagePath; _imageOption = imageOption; _maxWidth = maxWidth; return CreateImage(); } public bool CreateImage(Uri fullSourceImageURI, string fullTargetImagePath, ImageOptions imageOption, double maxWidth, Color backColor) { _fullSourceImageURI = fullSourceImageURI; _fullTargetImagePath = fullTargetImagePath; _imageOption = imageOption; _maxWidth = maxWidth; _backgroundColor = backColor; return CreateImage(); } public bool CreateImage(Uri fullSourceImageURI, string fullTargetImagePath, ImageOptions imageOption, double maxWidth, double maxHeight) { _fullSourceImageURI = fullSourceImageURI; _fullTargetImagePath = fullTargetImagePath; _imageOption = imageOption; _maxWidth = maxWidth; _maxHeight = maxHeight; return CreateImage(); } public bool CreateImage(Uri fullSourceImageURI, string fullTargetImagePath, ImageOptions imageOption, double maxWidth, double maxHeight, Color backColor) { bool returnValue = true; _fullSourceImageURI = fullSourceImageURI; _fullTargetImagePath = fullTargetImagePath; _imageOption = imageOption; _maxWidth = maxWidth; _maxHeight = maxHeight; _backgroundColor = backColor; try { Bitmap.FromStream(GetStream()).Save(_fullTargetImagePath); } catch { returnValue = false; } return returnValue; } #endregion #region GetJPEGImage public Image GetJPEGImage(Image sourceImage, ImageFormat targetImageFormat) { return ConvertImage(sourceImage, targetImageFormat); } public Image GetJPEGImage(Image sourceImage, ImageFormat targetImageFormat, int quality) { return ConvertImage(sourceImage, targetImageFormat, quality); } #endregion #region GetConvertedImage public Image GetConvertedImage(Image sourceImage, ImageFormat targetImageFormat) { return ConvertImage(sourceImage, targetImageFormat); } #endregion #region Upload public bool Upload() { bool returnValue = false; if (_fileStream != null && _fullTargetImagePath.Equals(string.Empty) == false) { returnValue = Upload(_fileStream, _fullTargetImagePath); } else if (_fullSourceImagePath.Equals(string.Empty) == false && _fullTargetImagePath.Equals(string.Empty) == false) { returnValue = Upload(_fullSourceImagePath, _fullTargetImagePath); } return returnValue; } public bool Upload(Stream fileStream, string fullTargetImagePath) { bool returnValue = true; try { returnValue = Upload(Image.FromStream(fileStream), fullTargetImagePath); } catch { returnValue = false; } return returnValue; } public bool Upload(Stream fileStream, string fullTargetImagePath, ImageFormat targetImageFormat) { bool returnValue = true; try { returnValue = Upload(ConvertImage(Image.FromStream(fileStream), targetImageFormat), fullTargetImagePath); } catch { returnValue = false; } return returnValue; } public bool Upload(Image imageObject, string fullTargetImagePath) { bool returnValue = true; try { fullTargetImagePath = ReplaceFileExtension(imageObject, fullTargetImagePath); imageObject.Save(fullTargetImagePath); imageObject.Dispose(); } catch { returnValue = false; } return returnValue; } public bool Upload(Image imageObject, string fullTargetImagePath, ImageFormat targetImageFormat) { bool returnValue = true; try { Image img = ConvertImage(imageObject, targetImageFormat); fullTargetImagePath = ReplaceFileExtension(img, fullTargetImagePath); img.Save(fullTargetImagePath); img.Dispose(); imageObject.Dispose(); } catch { returnValue = false; } return returnValue; } public bool Upload(string fullSourceImagePath, string fullTargetImagePath) { bool returnValue = false; using (Stream fileStream = GetStream(fullSourceImagePath)) { returnValue = Upload(fileStream, fullTargetImagePath); fileStream.Close(); } return returnValue; } public bool Upload(string fullSourceImagePath, string fullTargetImagePath, ImageFormat targetImageFormat) { bool returnValue = false; using (Stream fileStream = GetStream(fullSourceImagePath)) { returnValue = Upload(fileStream, fullTargetImagePath, targetImageFormat); fileStream.Close(); } return returnValue; } #endregion #region Helpers private void SetScaleFactor(Image _srcImage) { double _w = _maxWidth / (double)_srcImage.Size.Width; double _h = _maxHeight / (double)_srcImage.Size.Height; double _f = 0; ScaleParams(ref _w, ref _h, ref _f, _srcImage); switch (_imageOption) { case ImageOptions.OptimizeMaxWidthAndMaxHeight: _f = 1; _scaleX = _w; _scaleY = _h; break; case ImageOptions.OptimizeMaxHeight: _scaleX = _h; _scaleY = _h; break; case ImageOptions.OptimizeMaxWidth: _scaleX = _w; _scaleY = _w; break; default: _scaleX = _f; _scaleY = _f; break; } _scaleFactor = _f; } private void ScaleParams(ref double _w, ref double _h, ref double _f, Image _srcImage) { if (_w < _h) { if ((double)_srcImage.Size.Height * _w > _maxHeight) { _h = (double)(_maxHeight * _w) / (double)_srcImage.Size.Height; _f = _w * _h; } else { _f = _w; } } else { if ((double)_srcImage.Size.Width * _h > _maxWidth) { _w = (double)(_maxWidth * _h) / (double)_srcImage.Size.Width; _f = _w * _h; } else { _f = _h; } } } private void ClearParams() { GC.GetTotalMemory(true); GC.Collect(); _scaleFactor = 1; _imageOption = ImageOptions.OptimizeDefault; _backgroundColor = Color.Transparent; _maxWidth = 100.0; _maxHeight = 70.0; _scaleX = 1; _scaleY = 1; if (_srcImage != null) _srcImage.Dispose(); _srcImage = null; _fullSourceImagePath = string.Empty; _fullTargetImagePath = string.Empty; _sourceImagePath = string.Empty; _targetImagePath = string.Empty; _sourceImageName = string.Empty; _targetImageName = string.Empty; _fullSourceImageURI = null; } private bool CheckPath() { bool returnValue = true; if (_fullSourceImagePath == string.Empty) { if (_sourceImagePath != string.Empty) { // TODO: Leserechte prüfen if (_sourceImageName == string.Empty) returnValue = false; else _fullSourceImagePath = string.Format("{0}\\{1}", _sourceImagePath, _sourceImageName); } else if (_fullSourceImageURI != null) { WebRequest req = WebRequest.Create(_fullSourceImageURI); req.Timeout = 10000; WebResponse resp = req.GetResponse(); if (Array.IndexOf(supportedMimeTypes, resp.ContentType) == -1) { String contentType = resp.ContentType; resp.Close(); returnValue = false; } else { _srcImage = Image.FromStream(resp.GetResponseStream()); resp.Close(); } } else returnValue = false; } else { _sourceImagePath = _fullSourceImagePath.Substring(0, _fullSourceImagePath.LastIndexOf("\\")); _sourceImageName = _fullSourceImagePath.Substring(_fullSourceImagePath.LastIndexOf("\\") + 1); } if (_fullTargetImagePath == string.Empty) { if (_targetImagePath != string.Empty) { if (_targetImageName == string.Empty) { string _ext = string.Empty; if (_fullSourceImagePath != string.Empty) { _ext = _fullSourceImagePath.Split('.')[1]; } else if (_fullSourceImageURI != null) { _ext = _fullSourceImageURI.OriginalString.Substring(_fullSourceImageURI.OriginalString.LastIndexOf("/") + 1).Split('.')[1]; _sourceImageName = _fullSourceImageURI.OriginalString.Substring(_fullSourceImageURI.OriginalString.LastIndexOf("/") + 1); } else _ext = "jpeg"; if (_sourceImageName == string.Empty) _targetImageName = string.Format("{0}{1}.{2}", _targetNamePrefix, System.Guid.NewGuid(), _ext); else _targetImageName = string.Format("{0}{1}", _targetNamePrefix, _sourceImageName); } _fullTargetImagePath = string.Format("{0}\\{1}", _targetImagePath, _targetImageName); } else { if (_sourceImagePath != string.Empty) { _targetImagePath = _sourceImagePath; if (_sourceImageName == string.Empty) _targetImageName = string.Format("{0}{1}.{2}", _targetNamePrefix, System.Guid.NewGuid(), _fullSourceImagePath.Split('.')[1]); else _targetImageName = string.Format("{0}{1}", _targetNamePrefix, _sourceImageName); _fullTargetImagePath = string.Format("{0}\\{1}", _targetImagePath, _targetImageName); } else returnValue = false; } } if (_srcImage == null && File.Exists(_fullSourceImagePath) == false) { returnValue = false; } else if (Directory.Exists(_targetImagePath) == false) { try { Directory.CreateDirectory(_targetImagePath); CheckPath(); } catch { returnValue = false; } } else { // TODO: Schreibrechte prüfen } return returnValue; } private Image ConvertImage(Image sourceImage, ImageFormat targetImageFormat) { if (sourceImage.RawFormat.Equals(targetImageFormat)) { return sourceImage; } PixelFormat _pixelFormat = PixelFormat.Format32bppArgb; if (targetImageFormat.Equals(ImageFormat.Jpeg)) { _pixelFormat = PixelFormat.Format24bppRgb; } else if (targetImageFormat.Equals(ImageFormat.Gif)) { _pixelFormat = PixelFormat.Format32bppArgb; } else if (targetImageFormat.Equals(ImageFormat.Bmp)) { _pixelFormat = PixelFormat.Format32bppArgb; } else if (targetImageFormat.Equals(ImageFormat.Png)) { _pixelFormat = PixelFormat.Format32bppArgb; } else if (targetImageFormat.Equals(ImageFormat.Tiff)) { _pixelFormat = PixelFormat.Format32bppArgb; } else if (targetImageFormat.Equals(ImageFormat.Wmf)) { _pixelFormat = PixelFormat.Undefined; } else { _pixelFormat = PixelFormat.Format24bppRgb; } Bitmap bmp = new Bitmap(sourceImage.Size.Width, sourceImage.Size.Height, _pixelFormat); Graphics g1 = Graphics.FromImage(bmp); g1.DrawImage(bmp, 0, 0, bmp.Size.Width, bmp.Size.Height); g1.CompositingQuality = CompositingQuality.HighQuality; g1.SmoothingMode = SmoothingMode.HighQuality; g1.InterpolationMode = InterpolationMode.HighQualityBicubic; g1.Dispose(); using (Stream imgStream = new MemoryStream()) { bmp.Save(imgStream, targetImageFormat); bmp.Dispose(); imgStream.Seek(0, SeekOrigin.Begin); return GetImage(imgStream); } return null; } private Image ConvertImage(Image sourceImage, ImageFormat targetImageFormat, int quality) { if (targetImageFormat.Equals(ImageFormat.Jpeg) == false) { return ConvertImage(sourceImage, targetImageFormat); } else if (sourceImage.RawFormat.Equals(targetImageFormat) && quality == -1) { return (Image)sourceImage.Clone(); } _quality = quality; using (Stream imgStream = new MemoryStream()) { ImageCodecInfo destCodec = GetCodecForType(MimeTypeFromImageFormat(ImageFormat.Jpeg)); if (destCodec == null) { return (Image)sourceImage.Clone(); } EncoderParameters destEncParams = new EncoderParameters(1); EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, quality); destEncParams.Param[0] = qualityParam; sourceImage.Save(imgStream, destCodec, destEncParams); return Image.FromStream(imgStream); } return null; } private Image ConvertImage(Image sourceImage, string targetMimeType) { return ConvertImage(sourceImage, ImageFormatFromMimeType(targetMimeType)); } private String MimeTypeFromImageFormat(ImageFormat format) { if (format.Equals(ImageFormat.Jpeg)) { return MIME_JPEG; } else if (format.Equals(ImageFormat.Gif)) { return MIME_GIF; } else if (format.Equals(ImageFormat.Bmp)) { return MIME_BMP; } else if (format.Equals(ImageFormat.Tiff)) { return MIME_TIFF; } else if (format.Equals(ImageFormat.Png)) { return MIME_PNG; } else { return string.Empty; } } private ImageFormat ImageFormatFromMimeType(String mimeType) { switch (mimeType) { case MIME_JPEG: case MIME_PJPEG: return ImageFormat.Jpeg; case MIME_GIF: return ImageFormat.Gif; case MIME_BMP: return ImageFormat.Bmp; case MIME_TIFF: return ImageFormat.Tiff; case MIME_PNG: return ImageFormat.Png; default: return ImageFormat.Bmp; } } private ImageCodecInfo GetCodecForType(String mimeType) { ImageCodecInfo[] imgEncoders = ImageCodecInfo.GetImageEncoders(); for (int i = 0; i < imgEncoders.GetLength(0); i++) { if (imgEncoders[i].MimeType.Equals(mimeType)) { return imgEncoders[i]; } } return null; } public string GetFileExtensionByContentType(string contentType) { switch (contentType) { case MIME_JPEG: case MIME_PJPEG: return ".jpg"; case MIME_GIF: return ".gif"; case MIME_PNG: return ".png"; case MIME_TIFF: return ".tif"; default: return ".bmp"; } } public string GetFileExtensionByContentType(ImageFormat imageFormat) { return GetFileExtensionByContentType(MimeTypeFromImageFormat(imageFormat)); } private string ReplaceFileExtension(Image imageObject, string fullTargetImagePath) { string ex = Path.GetExtension(fullTargetImagePath); string _ex = GetFileExtensionByContentType(imageObject.RawFormat); if (ex.Equals(_ex) == false) fullTargetImagePath = fullTargetImagePath.Replace(ex, _ex); return fullTargetImagePath; } #endregion } }