public static SizingOptions Parse(string filename)
        {
            SizingOptions o = new SizingOptions();

            string[] arr = filename.Split('.');
            string[] dim = arr[0].Split('_');
            if (arr.Length != 2 || dim.Length != 11)
            {
                return(null);
            }
            int[] values = new int[dim.Length];
            for (int i = 0; i < dim.Length; i++)
            {
                if (!int.TryParse(dim[i], out values[i]))
                {
                    return(null);
                }
            }
            o.sprocketFileID     = values[0];
            o.width              = values[1];
            o.height             = values[2];
            o.padding            = values[3];
            o.backgroundColor    = Color.FromArgb(values[4]);
            o.borderColor        = Color.FromArgb(values[5]);
            o.borderSize         = values[6];
            o.tileSpacing        = values[7];
            o.displayType        = (Display)values[8];
            o.anchor             = (CropAnchor)values[9];
            o.preventEnlargement = values[10] == 1;
            o.SetFilename();
            return(o);
        }
Exemple #2
0
        public void TransmitImage(SizingOptions options)
        {
            MemoryStream stream = new MemoryStream();

            ResizeImage(options, stream);
            HttpContext.Current.Response.ContentType = "image/jpeg";
            HttpContext.Current.Response.BinaryWrite(stream.ToArray());
            HttpContext.Current.Response.End();
        }
Exemple #3
0
        public void TransmitImage(string filename)
        {
            SizingOptions options = SizingOptions.Parse(filename);

            HttpContext.Current.Response.ContentType = "image/jpeg";
            if (WebUtility.RawQueryString == "nocache")
            {
                ResizeImage(options, HttpContext.Current.Response.OutputStream);
                HttpContext.Current.Response.End();
            }
            else
            {
                if (!File.Exists(options.PhysicalPath))
                {
                    ResizeImage(options);
                }
                HttpContext.Current.Response.TransmitFile(options.PhysicalPath);
            }
        }
Exemple #4
0
        public void TransmitRequestedImage()
        {
            // note: if processing ever gets this far, we definitely don't have a cached version
            // of this file, or the ContentCache would have served it up by now.

            FileInfo      info    = new FileInfo(SprocketPath.Physical);
            SizingOptions options = SizingOptions.Parse(info.Name);

            HttpContext.Current.Response.ContentType = "image/jpeg";
            if (WebUtility.RawQueryString == "nocache")
            {
                ResizeImage(options, HttpContext.Current.Response.OutputStream);
                HttpContext.Current.Response.End();
            }
            else
            {
                MemoryStream stream = new MemoryStream();
                ResizeImage(options, stream);
                string path = ContentCache.Store("Sprocket.Web.FileManager.CachedImage." + options.SprocketFileID + "." + options.Filename, null, false, SprocketPath.Value, "image/jpeg", stream);
                HttpContext.Current.Response.TransmitFile(path);
            }
        }
        void OnLoadRequestedPath(HandleFlag handled)
        {
            switch (SprocketPath.Value)
            {
                case "test":
                    Response.Write("<form method=\"post\" action=\""
                        + WebUtility.BasePath + "test/upload/\" enctype=\"multipart/form-data\">"
                        + "<input type=\"file\" size=\"40\" name=\"thefile\" /> <input type=\"submit\" value=\"upload\" />"
                        + "</form>"
                        );
                    break;

                case "test/upload":
                    HttpPostedFile posted = HttpContext.Current.Request.Files[0];
                    SprocketFile file = new SprocketFile(Security.SecurityProvider.ClientSpaceID, posted, "Test Image", "A test image.");
                    FileManager.DataLayer.Store(file);
                    WebUtility.Redirect("test/show/?" + file.SprocketFileID);
                    break;

                case "test/show":
                    long id = long.Parse(WebUtility.RawQueryString);
                    SizingOptions options = new SizingOptions(320, 180, 10, Color.Black, Color.CadetBlue, 2, SizingOptions.Display.Letterbox, id);
                    Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                    options = new SizingOptions(200, 200, 0, Color.Black, Color.CadetBlue, 0, SizingOptions.Display.Letterbox, id);
                    Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                    options = new SizingOptions(200, 200, 0, Color.Black, Color.CadetBlue, 0, SizingOptions.Display.Stretch, id);
                    Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                    options = new SizingOptions(100, 200, 0, Color.Black, Color.CadetBlue, 0, SizingOptions.Display.Letterbox, id);
                    Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                    options = new SizingOptions(100, 100, 10, Color.White, Color.FromArgb(240, 240, 240), 1, SizingOptions.Display.Letterbox, id);
                    Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                    options = new SizingOptions(100, 100, 0, Color.Black, Color.CadetBlue, 0, SizingOptions.CropAnchor.Top, id);
                    Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                    options = new SizingOptions(100, 100, 15, Color.Black, Color.Red, 5, SizingOptions.CropAnchor.Top, id);
                    Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                    options = new SizingOptions(100, 100, 0, Color.Black, Color.CadetBlue, 0, SizingOptions.CropAnchor.Bottom, id);
                    Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                    options = new SizingOptions(100, 100, 15, Color.Black, Color.Red, 5, SizingOptions.CropAnchor.Bottom, id);
                    Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                    options = new SizingOptions(100, 100, 0, Color.Black, Color.CadetBlue, 0, SizingOptions.CropAnchor.Center, id);
                    Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                    options = new SizingOptions(100, 100, 15, Color.Black, Color.Red, 5, SizingOptions.CropAnchor.Center, id);
                    Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                    options = new SizingOptions(100, 100, 0, Color.Black, Color.CadetBlue, 0, SizingOptions.CropAnchor.Left, id);
                    Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                    options = new SizingOptions(100, 100, 15, Color.Black, Color.Red, 5, SizingOptions.CropAnchor.Left, id);
                    Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                    options = new SizingOptions(100, 100, 0, Color.Black, Color.CadetBlue, 0, SizingOptions.CropAnchor.Right, id);
                    Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                    options = new SizingOptions(100, 100, 15, Color.Black, Color.Red, 5, SizingOptions.CropAnchor.Right, id);
                    Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                    options = new SizingOptions(100, 100, 0, Color.Black, Color.CadetBlue, 0, SizingOptions.Display.Center, id);
                    Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                    options = new SizingOptions(100, 100, 15, Color.Black, Color.Red, 5, SizingOptions.Display.Center, id);
                    Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                    options = new SizingOptions(400, 300, 10, Color.Black, Color.CadetBlue, 0, 10, id);
                    Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" align=\"top\" /> ");
                    break;

                default:
                    if (SprocketPath.Value.EndsWith(".jpg") && SprocketPath.Value.StartsWith("test/image/"))
                    {
                        FileManager.Instance.TransmitRequestedImage();
                        break;
                    }
                    return;
            }
            handled.Set();
        }
		public void ResizeImage(SizingOptions options)
		{
			ResizeImage(options, null);
		}
		public void ResizeImage(SizingOptions options, Stream outStream)
		{
			SprocketFile file = dataLayer.SelectSprocketFile(options.SprocketFileID, true);
			Image sourceImage = options.Image == null ? Image.FromStream(new MemoryStream(file.FileData)) : options.Image;
			Size imageSize;
			if (options.DisplayType == SizingOptions.Display.Constrain)
			{
				int maxWidth = 0, maxHeight = 0;
				if (options.Width > 0)
					maxWidth = options.Width - options.Padding * 2 - options.BorderSize * 2;
				if (options.Height > 0)
					maxHeight = options.Height - options.Padding * 2 - options.BorderSize * 2;
				if (maxWidth == 0 && maxHeight == 0)
					throw new Exception("Can't resize image. No dimensions were specified.");
				float srcRatio = (float)sourceImage.Width / (float)sourceImage.Height;
				if (maxWidth == 0)
					imageSize = new Size((int)((float)maxHeight * srcRatio) + options.BorderSize * 2 + options.Padding * 2,
						maxHeight + options.BorderSize * 2 + options.Padding * 2);
				else if (maxHeight == 0)
					imageSize = new Size(maxWidth + options.BorderSize * 2 + options.Padding * 2,
						(int)((float)maxWidth / srcRatio) + options.BorderSize * 2 + options.Padding * 2);
				else
				{
					float destRatio = (float)maxWidth / (float)maxHeight;
					if (destRatio < srcRatio) // landscape; keep width, set height
						imageSize = new Size(maxWidth + options.BorderSize * 2 + options.Padding * 2,
							(int)((float)maxWidth / srcRatio) + options.BorderSize * 2 + options.Padding * 2);
					else // portrait; keep height, set width
						imageSize = new Size((int)((float)maxHeight * srcRatio) + options.BorderSize * 2 + options.Padding * 2,
							maxHeight + options.BorderSize * 2 + options.Padding * 2);
				}
			}
			else
			{
				imageSize = new Size(options.Width, options.Height);
			}
			if (options.PreventEnlargement)
			{
				if (imageSize.Width - options.Padding * 2 - options.BorderSize * 2 > sourceImage.Width)
					imageSize.Width = sourceImage.Width + options.Padding * 2 + options.BorderSize * 2;
				if (imageSize.Height - options.Padding * 2 - options.BorderSize * 2 > sourceImage.Height)
					imageSize.Height = sourceImage.Height + options.Padding * 2 + options.BorderSize * 2;
			}
			using (Image finalImage = new Bitmap(imageSize.Width, imageSize.Height))
			{
				using (Graphics gfx = Graphics.FromImage(finalImage))
				{
					// set the rendering defaults
					gfx.CompositingQuality = CompositingQuality.HighQuality;
					gfx.SmoothingMode = SmoothingMode.HighQuality;
					gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;

					// set colours and pens
					Brush fill = new SolidBrush(options.BackgroundColor);
					Pen pen = new Pen(options.BorderColor, options.BorderSize);
					pen.Alignment = PenAlignment.Inset;

					// fill the background
					gfx.FillRectangle(fill, 0, 0, finalImage.Width, finalImage.Height);

					// calculate the area in which we can paint the image
					Rectangle clientRect = new Rectangle(
						options.BorderSize + options.Padding, options.BorderSize + options.Padding,
						finalImage.Width - options.BorderSize * 2 - options.Padding * 2,
						finalImage.Height - options.BorderSize * 2 - options.Padding * 2);

					// work out the letterboxing ratios (if the inner ratio is greater, the photo is
					// proportionally wider than the container, i.e. standard widescreen letterbox)
					float containerRatio = (float)clientRect.Width / (float)clientRect.Height;
					float imgRatio = (float)sourceImage.Width / (float)sourceImage.Height;
					bool landscape = imgRatio > containerRatio;

					Rectangle destRect = new Rectangle(clientRect.X, clientRect.Y, clientRect.Width, clientRect.Height);
					switch (options.DisplayType)
					{
						case SizingOptions.Display.Letterbox:
							if (landscape)
							{
								int h = (int)((float)destRect.Width / imgRatio);
								destRect.Y += (destRect.Height - h) / 2;
								destRect.Height = h;
							}
							else
							{
								int w = (int)((float)destRect.Height * imgRatio);
								destRect.X += (destRect.Width - w) / 2;
								destRect.Width = w;
							}
							gfx.DrawImage(sourceImage, destRect);
							break;

						case SizingOptions.Display.Constrain:
						case SizingOptions.Display.Stretch:
							gfx.DrawImage(sourceImage, destRect);
							break;

						case SizingOptions.Display.Center:
							gfx.DrawImage(sourceImage, destRect, new Rectangle(
								(sourceImage.Width - destRect.Width) / 2, (sourceImage.Height - destRect.Height) / 2,
								destRect.Width, destRect.Height), GraphicsUnit.Pixel);
							break;

						case SizingOptions.Display.Tile:
							int tileX, tileY = clientRect.Y;
							do
							{
								tileX = clientRect.X;
								do
								{
									destRect = new Rectangle(tileX, tileY,
										tileX + sourceImage.Width > clientRect.Right ? clientRect.Right - tileX : sourceImage.Width,
										tileY + sourceImage.Height > clientRect.Bottom ? clientRect.Bottom - tileY : sourceImage.Height);
									gfx.DrawImageUnscaledAndClipped(sourceImage, destRect);
									tileX += sourceImage.Width + options.TileSpacing;
								} while (tileX < clientRect.Right);
								tileY += sourceImage.Height + options.TileSpacing;
							} while (tileY < clientRect.Bottom);
							break;

						case SizingOptions.Display.Crop:
							switch (options.Anchor)
							{
								case SizingOptions.CropAnchor.Top:
									using (Bitmap bmp = new Bitmap(destRect.Width, (int)((float)destRect.Width / imgRatio)))
									{
										using (Graphics gfx2 = Graphics.FromImage(bmp))
										{
											gfx2.InterpolationMode = InterpolationMode.HighQualityBicubic;
											gfx2.DrawImage(sourceImage, 0, 0, bmp.Width, bmp.Height);
										}
										gfx.DrawImage(bmp, destRect, new Rectangle(0, 0, destRect.Width, destRect.Height), GraphicsUnit.Pixel);
									}
									break;

								case SizingOptions.CropAnchor.Bottom:
									using (Bitmap bmp = new Bitmap(destRect.Width, (int)((float)destRect.Width / imgRatio)))
									{
										using (Graphics gfx2 = Graphics.FromImage(bmp))
										{
											gfx2.InterpolationMode = InterpolationMode.HighQualityBicubic;
											gfx2.DrawImage(sourceImage, 0, 0, bmp.Width, bmp.Height);
										}
										gfx.DrawImage(bmp, destRect, new Rectangle(0, bmp.Height - destRect.Height, destRect.Width, destRect.Height), GraphicsUnit.Pixel);
									}
									break;

								case SizingOptions.CropAnchor.Left:
									using (Bitmap bmp = new Bitmap((int)((float)destRect.Height * imgRatio), destRect.Height))
									{
										using (Graphics gfx2 = Graphics.FromImage(bmp))
										{
											gfx2.InterpolationMode = InterpolationMode.HighQualityBicubic;
											gfx2.DrawImage(sourceImage, 0, 0, bmp.Width, bmp.Height);
										}
										gfx.DrawImage(bmp, destRect, new Rectangle(0, 0, destRect.Width, destRect.Height), GraphicsUnit.Pixel);
									}
									break;

								case SizingOptions.CropAnchor.Right:
									using (Bitmap bmp = new Bitmap((int)((float)destRect.Height * imgRatio), destRect.Height))
									{
										using (Graphics gfx2 = Graphics.FromImage(bmp))
										{
											gfx2.InterpolationMode = InterpolationMode.HighQualityBicubic;
											gfx2.DrawImage(sourceImage, 0, 0, bmp.Width, bmp.Height);
										}
										gfx.DrawImage(bmp, destRect, new Rectangle(bmp.Width - destRect.Width, 0, destRect.Width, destRect.Height), GraphicsUnit.Pixel);
									}
									break;

								case SizingOptions.CropAnchor.Center:
									int midW, midH;
									if (landscape)
									{
										midH = destRect.Height;
										midW = (int)((float)destRect.Height * imgRatio);
									}
									else
									{
										midW = destRect.Width;
										midH = (int)((float)destRect.Width / imgRatio);
									}
									using (Bitmap bmp = new Bitmap(midW, midH))
									{
										using (Graphics gfx2 = Graphics.FromImage(bmp))
										{
											gfx2.InterpolationMode = InterpolationMode.HighQualityBicubic;
											gfx2.DrawImage(sourceImage, 0, 0, bmp.Width, bmp.Height);
										}
										if (landscape)
											gfx.DrawImage(bmp, destRect, new Rectangle((bmp.Width - destRect.Width) / 2, 0, destRect.Width, destRect.Height), GraphicsUnit.Pixel);
										else
											gfx.DrawImage(bmp, destRect, new Rectangle(0, (bmp.Height - destRect.Height) / 2, destRect.Width, destRect.Height), GraphicsUnit.Pixel);
									}
									break;

								default:
									break;
							}
							break;

						default:
							break;
					}

					// draw the border last to ensure it doesn't get obscured;
					if (options.BorderSize > 0)
						gfx.DrawRectangle(pen, 0, 0, finalImage.Width - 1, finalImage.Height - 1);

					// find the jpeg encoder
					ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
					ImageCodecInfo encoder = null;
					for (int i = 0; i < encoders.Length; i++)
						if (encoders[i].MimeType == "image/jpeg")
						{
							encoder = encoders[i];
							break;
						}
					if (encoder == null)
						throw new SprocketException("Can't create a thumbnail because no JPEG encoder exists.");
					EncoderParameters prms = new EncoderParameters(1);

					// set the jpeg quality
					prms.Param[0] = new EncoderParameter(Encoder.Quality, options.JpegQuality);

					if (outStream != null)
					{
						finalImage.Save(outStream, encoder, prms);
						if(outStream is MemoryStream)
							outStream.Seek(0, SeekOrigin.Begin);
					}
					else
					{
						// make sure the cache directory exists
						DirectoryInfo info = new FileInfo(options.PhysicalPath).Directory;
						if (!info.Exists)
							info.Create();

						// save the image
						finalImage.Save(options.PhysicalPath, encoder, prms);
					}
				}

			}
			if (options.Image == null)
				sourceImage.Dispose();
		}
		public static SizingOptions Parse(string filename)
		{
			SizingOptions o = new SizingOptions();
			string[] arr = filename.Split('.');
			string[] dim = arr[0].Split('_');
			if (arr.Length != 2 || dim.Length != 11)
				return null;
			int[] values = new int[dim.Length];
			for (int i = 0; i < dim.Length; i++)
				if (!int.TryParse(dim[i], out values[i]))
					return null;
			o.sprocketFileID = values[0];
			o.width = values[1];
			o.height = values[2];
			o.padding = values[3];
			o.backgroundColor = Color.FromArgb(values[4]);
			o.borderColor = Color.FromArgb(values[5]);
			o.borderSize = values[6];
			o.tileSpacing = values[7];
			o.displayType = (Display)values[8];
			o.anchor = (CropAnchor)values[9];
			o.preventEnlargement = values[10] == 1;
			o.SetFilename();
			return o;
		}
        void OnLoadRequestedPath(HandleFlag handled)
        {
            switch (SprocketPath.Value)
            {
            case "test":
                Response.Write("<form method=\"post\" action=\""
                               + WebUtility.BasePath + "test/upload/\" enctype=\"multipart/form-data\">"
                               + "<input type=\"file\" size=\"40\" name=\"thefile\" /> <input type=\"submit\" value=\"upload\" />"
                               + "</form>"
                               );
                break;

            case "test/upload":
                HttpPostedFile posted = HttpContext.Current.Request.Files[0];
                SprocketFile   file   = new SprocketFile(Security.SecurityProvider.ClientSpaceID, posted, "Test Image", "A test image.");
                FileManager.DataLayer.Store(file);
                WebUtility.Redirect("test/show/?" + file.SprocketFileID);
                break;

            case "test/show":
                long          id      = long.Parse(WebUtility.RawQueryString);
                SizingOptions options = new SizingOptions(320, 180, 10, Color.Black, Color.CadetBlue, 2, SizingOptions.Display.Letterbox, id);
                Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                options = new SizingOptions(200, 200, 0, Color.Black, Color.CadetBlue, 0, SizingOptions.Display.Letterbox, id);
                Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                options = new SizingOptions(200, 200, 0, Color.Black, Color.CadetBlue, 0, SizingOptions.Display.Stretch, id);
                Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                options = new SizingOptions(100, 200, 0, Color.Black, Color.CadetBlue, 0, SizingOptions.Display.Letterbox, id);
                Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                options = new SizingOptions(100, 100, 10, Color.White, Color.FromArgb(240, 240, 240), 1, SizingOptions.Display.Letterbox, id);
                Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                options = new SizingOptions(100, 100, 0, Color.Black, Color.CadetBlue, 0, SizingOptions.CropAnchor.Top, id);
                Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                options = new SizingOptions(100, 100, 15, Color.Black, Color.Red, 5, SizingOptions.CropAnchor.Top, id);
                Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                options = new SizingOptions(100, 100, 0, Color.Black, Color.CadetBlue, 0, SizingOptions.CropAnchor.Bottom, id);
                Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                options = new SizingOptions(100, 100, 15, Color.Black, Color.Red, 5, SizingOptions.CropAnchor.Bottom, id);
                Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                options = new SizingOptions(100, 100, 0, Color.Black, Color.CadetBlue, 0, SizingOptions.CropAnchor.Center, id);
                Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                options = new SizingOptions(100, 100, 15, Color.Black, Color.Red, 5, SizingOptions.CropAnchor.Center, id);
                Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                options = new SizingOptions(100, 100, 0, Color.Black, Color.CadetBlue, 0, SizingOptions.CropAnchor.Left, id);
                Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                options = new SizingOptions(100, 100, 15, Color.Black, Color.Red, 5, SizingOptions.CropAnchor.Left, id);
                Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                options = new SizingOptions(100, 100, 0, Color.Black, Color.CadetBlue, 0, SizingOptions.CropAnchor.Right, id);
                Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                options = new SizingOptions(100, 100, 15, Color.Black, Color.Red, 5, SizingOptions.CropAnchor.Right, id);
                Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                options = new SizingOptions(100, 100, 0, Color.Black, Color.CadetBlue, 0, SizingOptions.Display.Center, id);
                Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                options = new SizingOptions(100, 100, 15, Color.Black, Color.Red, 5, SizingOptions.Display.Center, id);
                Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" /> ");

                options = new SizingOptions(400, 300, 10, Color.Black, Color.CadetBlue, 0, 10, id);
                Response.Write("<img src=\"" + WebUtility.BasePath + "test/image/" + options.Filename + "?nocache\" hspace=\"5\" vspace=\"5\" align=\"top\" /> ");
                break;

            default:
                if (SprocketPath.Value.EndsWith(".jpg") && SprocketPath.Value.StartsWith("test/image/"))
                {
                    FileManager.Instance.TransmitRequestedImage();
                    break;
                }
                return;
            }
            handled.Set();
        }
Exemple #10
0
        public void ResizeImage(SizingOptions options, Stream outStream)
        {
            SprocketFile file        = dataLayer.SelectSprocketFile(options.SprocketFileID, true);
            Image        sourceImage = options.Image == null?Image.FromStream(new MemoryStream(file.FileData)) : options.Image;

            Size imageSize;

            if (options.DisplayType == SizingOptions.Display.Constrain)
            {
                int maxWidth = 0, maxHeight = 0;
                if (options.Width > 0)
                {
                    maxWidth = options.Width - options.Padding * 2 - options.BorderSize * 2;
                }
                if (options.Height > 0)
                {
                    maxHeight = options.Height - options.Padding * 2 - options.BorderSize * 2;
                }
                if (maxWidth == 0 && maxHeight == 0)
                {
                    throw new Exception("Can't resize image. No dimensions were specified.");
                }
                float srcRatio = (float)sourceImage.Width / (float)sourceImage.Height;
                if (maxWidth == 0)
                {
                    imageSize = new Size((int)((float)maxHeight * srcRatio) + options.BorderSize * 2 + options.Padding * 2,
                                         maxHeight + options.BorderSize * 2 + options.Padding * 2);
                }
                else if (maxHeight == 0)
                {
                    imageSize = new Size(maxWidth + options.BorderSize * 2 + options.Padding * 2,
                                         (int)((float)maxWidth / srcRatio) + options.BorderSize * 2 + options.Padding * 2);
                }
                else
                {
                    float destRatio = (float)maxWidth / (float)maxHeight;
                    if (destRatio < srcRatio)                     // landscape; keep width, set height
                    {
                        imageSize = new Size(maxWidth + options.BorderSize * 2 + options.Padding * 2,
                                             (int)((float)maxWidth / srcRatio) + options.BorderSize * 2 + options.Padding * 2);
                    }
                    else                     // portrait; keep height, set width
                    {
                        imageSize = new Size((int)((float)maxHeight * srcRatio) + options.BorderSize * 2 + options.Padding * 2,
                                             maxHeight + options.BorderSize * 2 + options.Padding * 2);
                    }
                }
            }
            else
            {
                imageSize = new Size(options.Width, options.Height);
            }
            if (options.PreventEnlargement)
            {
                if (imageSize.Width - options.Padding * 2 - options.BorderSize * 2 > sourceImage.Width)
                {
                    imageSize.Width = sourceImage.Width + options.Padding * 2 + options.BorderSize * 2;
                }
                if (imageSize.Height - options.Padding * 2 - options.BorderSize * 2 > sourceImage.Height)
                {
                    imageSize.Height = sourceImage.Height + options.Padding * 2 + options.BorderSize * 2;
                }
            }
            using (Image finalImage = new Bitmap(imageSize.Width, imageSize.Height))
            {
                using (Graphics gfx = Graphics.FromImage(finalImage))
                {
                    // set the rendering defaults
                    gfx.CompositingQuality = CompositingQuality.HighQuality;
                    gfx.SmoothingMode      = SmoothingMode.HighQuality;
                    gfx.InterpolationMode  = InterpolationMode.HighQualityBicubic;

                    // set colours and pens
                    Brush fill = new SolidBrush(options.BackgroundColor);
                    Pen   pen  = new Pen(options.BorderColor, options.BorderSize);
                    pen.Alignment = PenAlignment.Inset;

                    // fill the background
                    gfx.FillRectangle(fill, 0, 0, finalImage.Width, finalImage.Height);

                    // calculate the area in which we can paint the image
                    Rectangle clientRect = new Rectangle(
                        options.BorderSize + options.Padding, options.BorderSize + options.Padding,
                        finalImage.Width - options.BorderSize * 2 - options.Padding * 2,
                        finalImage.Height - options.BorderSize * 2 - options.Padding * 2);

                    // work out the letterboxing ratios (if the inner ratio is greater, the photo is
                    // proportionally wider than the container, i.e. standard widescreen letterbox)
                    float containerRatio = (float)clientRect.Width / (float)clientRect.Height;
                    float imgRatio       = (float)sourceImage.Width / (float)sourceImage.Height;
                    bool  landscape      = imgRatio > containerRatio;

                    Rectangle destRect = new Rectangle(clientRect.X, clientRect.Y, clientRect.Width, clientRect.Height);
                    switch (options.DisplayType)
                    {
                    case SizingOptions.Display.Letterbox:
                        if (landscape)
                        {
                            int h = (int)((float)destRect.Width / imgRatio);
                            destRect.Y     += (destRect.Height - h) / 2;
                            destRect.Height = h;
                        }
                        else
                        {
                            int w = (int)((float)destRect.Height * imgRatio);
                            destRect.X    += (destRect.Width - w) / 2;
                            destRect.Width = w;
                        }
                        gfx.DrawImage(sourceImage, destRect);
                        break;

                    case SizingOptions.Display.Constrain:
                    case SizingOptions.Display.Stretch:
                        gfx.DrawImage(sourceImage, destRect);
                        break;

                    case SizingOptions.Display.Center:
                        gfx.DrawImage(sourceImage, destRect, new Rectangle(
                                          (sourceImage.Width - destRect.Width) / 2, (sourceImage.Height - destRect.Height) / 2,
                                          destRect.Width, destRect.Height), GraphicsUnit.Pixel);
                        break;

                    case SizingOptions.Display.Tile:
                        int tileX, tileY = clientRect.Y;
                        do
                        {
                            tileX = clientRect.X;
                            do
                            {
                                destRect = new Rectangle(tileX, tileY,
                                                         tileX + sourceImage.Width > clientRect.Right ? clientRect.Right - tileX : sourceImage.Width,
                                                         tileY + sourceImage.Height > clientRect.Bottom ? clientRect.Bottom - tileY : sourceImage.Height);
                                gfx.DrawImageUnscaledAndClipped(sourceImage, destRect);
                                tileX += sourceImage.Width + options.TileSpacing;
                            } while (tileX < clientRect.Right);
                            tileY += sourceImage.Height + options.TileSpacing;
                        } while (tileY < clientRect.Bottom);
                        break;

                    case SizingOptions.Display.Crop:
                        switch (options.Anchor)
                        {
                        case SizingOptions.CropAnchor.Top:
                            using (Bitmap bmp = new Bitmap(destRect.Width, (int)((float)destRect.Width / imgRatio)))
                            {
                                using (Graphics gfx2 = Graphics.FromImage(bmp))
                                {
                                    gfx2.InterpolationMode = InterpolationMode.HighQualityBicubic;
                                    gfx2.DrawImage(sourceImage, 0, 0, bmp.Width, bmp.Height);
                                }
                                gfx.DrawImage(bmp, destRect, new Rectangle(0, 0, destRect.Width, destRect.Height), GraphicsUnit.Pixel);
                            }
                            break;

                        case SizingOptions.CropAnchor.Bottom:
                            using (Bitmap bmp = new Bitmap(destRect.Width, (int)((float)destRect.Width / imgRatio)))
                            {
                                using (Graphics gfx2 = Graphics.FromImage(bmp))
                                {
                                    gfx2.InterpolationMode = InterpolationMode.HighQualityBicubic;
                                    gfx2.DrawImage(sourceImage, 0, 0, bmp.Width, bmp.Height);
                                }
                                gfx.DrawImage(bmp, destRect, new Rectangle(0, bmp.Height - destRect.Height, destRect.Width, destRect.Height), GraphicsUnit.Pixel);
                            }
                            break;

                        case SizingOptions.CropAnchor.Left:
                            using (Bitmap bmp = new Bitmap((int)((float)destRect.Height * imgRatio), destRect.Height))
                            {
                                using (Graphics gfx2 = Graphics.FromImage(bmp))
                                {
                                    gfx2.InterpolationMode = InterpolationMode.HighQualityBicubic;
                                    gfx2.DrawImage(sourceImage, 0, 0, bmp.Width, bmp.Height);
                                }
                                gfx.DrawImage(bmp, destRect, new Rectangle(0, 0, destRect.Width, destRect.Height), GraphicsUnit.Pixel);
                            }
                            break;

                        case SizingOptions.CropAnchor.Right:
                            using (Bitmap bmp = new Bitmap((int)((float)destRect.Height * imgRatio), destRect.Height))
                            {
                                using (Graphics gfx2 = Graphics.FromImage(bmp))
                                {
                                    gfx2.InterpolationMode = InterpolationMode.HighQualityBicubic;
                                    gfx2.DrawImage(sourceImage, 0, 0, bmp.Width, bmp.Height);
                                }
                                gfx.DrawImage(bmp, destRect, new Rectangle(bmp.Width - destRect.Width, 0, destRect.Width, destRect.Height), GraphicsUnit.Pixel);
                            }
                            break;

                        case SizingOptions.CropAnchor.Center:
                            int midW, midH;
                            if (landscape)
                            {
                                midH = destRect.Height;
                                midW = (int)((float)destRect.Height * imgRatio);
                            }
                            else
                            {
                                midW = destRect.Width;
                                midH = (int)((float)destRect.Width / imgRatio);
                            }
                            using (Bitmap bmp = new Bitmap(midW, midH))
                            {
                                using (Graphics gfx2 = Graphics.FromImage(bmp))
                                {
                                    gfx2.InterpolationMode = InterpolationMode.HighQualityBicubic;
                                    gfx2.DrawImage(sourceImage, 0, 0, bmp.Width, bmp.Height);
                                }
                                if (landscape)
                                {
                                    gfx.DrawImage(bmp, destRect, new Rectangle((bmp.Width - destRect.Width) / 2, 0, destRect.Width, destRect.Height), GraphicsUnit.Pixel);
                                }
                                else
                                {
                                    gfx.DrawImage(bmp, destRect, new Rectangle(0, (bmp.Height - destRect.Height) / 2, destRect.Width, destRect.Height), GraphicsUnit.Pixel);
                                }
                            }
                            break;

                        default:
                            break;
                        }
                        break;

                    default:
                        break;
                    }

                    // draw the border last to ensure it doesn't get obscured;
                    if (options.BorderSize > 0)
                    {
                        gfx.DrawRectangle(pen, 0, 0, finalImage.Width - 1, finalImage.Height - 1);
                    }

                    // find the jpeg encoder
                    ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
                    ImageCodecInfo   encoder  = null;
                    for (int i = 0; i < encoders.Length; i++)
                    {
                        if (encoders[i].MimeType == "image/jpeg")
                        {
                            encoder = encoders[i];
                            break;
                        }
                    }
                    if (encoder == null)
                    {
                        throw new SprocketException("Can't create a thumbnail because no JPEG encoder exists.");
                    }
                    EncoderParameters prms = new EncoderParameters(1);

                    // set the jpeg quality
                    prms.Param[0] = new EncoderParameter(Encoder.Quality, options.JpegQuality);

                    if (outStream != null)
                    {
                        finalImage.Save(outStream, encoder, prms);
                        if (outStream is MemoryStream)
                        {
                            outStream.Seek(0, SeekOrigin.Begin);
                        }
                    }
                    else
                    {
                        // make sure the cache directory exists
                        DirectoryInfo info = new FileInfo(options.PhysicalPath).Directory;
                        if (!info.Exists)
                        {
                            info.Create();
                        }

                        // save the image
                        finalImage.Save(options.PhysicalPath, encoder, prms);
                    }
                }
            }
            if (options.Image == null)
            {
                sourceImage.Dispose();
            }
        }
Exemple #11
0
 public void ResizeImage(SizingOptions options)
 {
     ResizeImage(options, null);
 }
 public void TransmitImage(SizingOptions options)
 {
     MemoryStream stream = new MemoryStream();
     ResizeImage(options, stream);
     HttpContext.Current.Response.ContentType = "image/jpeg";
     HttpContext.Current.Response.BinaryWrite(stream.ToArray());
     HttpContext.Current.Response.End();
 }
        public Result ReadAdminField(out IEditFieldData efdata)
        {
            ImageEditFieldData data = new ImageEditFieldData();

            SprocketFile file = null;
            if (HttpContext.Current.Request.Files[fieldName] != null)
                if (HttpContext.Current.Request.Files[fieldName].ContentLength > 0)
                {
                    file = new SprocketFile(SecurityProvider.ClientSpaceID, HttpContext.Current.Request.Files[fieldName], "", "");
                    Image img;
                    Result r = file.ValidateImageData(out img);
                    if (!r.Succeeded)
                    {
                        efdata = new ImageEditFieldData();
                        return new Result("The image selected could not be loaded. The system reported back the following error: " + r.Message);
                    }
                    if (maxwidth > 0 || maxheight > 0)
                    {
                        SizingOptions options = new SizingOptions(maxwidth, maxheight, SizingOptions.Display.Constrain, 0);
                        options.Image = img;
                        MemoryStream stream = new MemoryStream();
                        FileManager.FileManager.Instance.ResizeImage(options, stream);
                        file.FileData = stream.ToArray();
                    }
                }

            bool deleted = HttpContext.Current.Request.Form["deleteimage_" + fieldName] != null;
            long existing;
            if (!long.TryParse(HttpContext.Current.Request.Form["existingimage_" + fieldName], out existing))
                existing = 0;

            if (deleted)
                data.SprocketFileID = 0;
            else if (file != null)
                data.SprocketFile = file;
            else
                data.SprocketFileID = existing;
            efdata = data;

            return new Result();
        }