public HandledImage Cut(HandledImageRequest request)
		{
			Image initImage = Image.FromStream(request.ImageStream, true);
			if (initImage.Width <= request.ImgSize.Width
				&& initImage.Height <= request.ImgSize.Height)
			{
				return new HandledImage()
				{
					Img = initImage,
					Name = request.SrcName
				};
			}

			double templateRate = (double)request.ImgSize.Width / (double)request.ImgSize.Height;
			double initRate = (double)initImage.Width / (double)initImage.Height;

			if(templateRate == initRate)
			{
				// 等比例缩放
				return ScaleCut(request, initImage);
			}
			else if(templateRate > initRate)
			{
				return HeightCut(request, initImage, templateRate);
			}
			else
			{
				return WidthCut(request, initImage, templateRate);
			}
		}
Beispiel #2
0
		public List<HandledImageResponse> CutImg(string vpath, string ppath)
		{
			// load file
			bool fileExists = File.Exists(ppath);
			if (!fileExists) return null;
			List<HandledImageResponse> imgPaths = new List<HandledImageResponse>();
			string pdirStr = GetDirectory(ppath);
			string vdirStr = GetVirtualDirectory(vpath);
			HandledImageRequest request = new HandledImageRequest();
			request.ImageStream = File.OpenRead(ppath);
			request.Quality = 100;
			request.SrcName = GetFileName(ppath);
			foreach (var imgsize in _imgsizes)
			{
				request.ImgSize = imgsize;
				var ret = _imgProvider.Cut(request);
				if(ret != null && ret.Img != null && !string.IsNullOrWhiteSpace(ret.Name))
				{
					// store file
					string scaleFilePath = string.Format("{0}\\{1}.jpg", pdirStr, ret.Name);
					ret.Img.Save(scaleFilePath, ImageFormat.Jpeg);
					ret.Img.Dispose();
					string scaleVisualFilePath = string.Format("{0}/{1}.jpg", vdirStr, ret.Name);
					imgPaths.Add(new HandledImageResponse() { ImgSize = imgsize, ImgUrl = scaleVisualFilePath });
				}
			}
			request.ImageStream.Close();
			return imgPaths;
		}
		private HandledImage ScaleCut(HandledImageRequest request, Image initImage)
		{
			Image templateImg = new Bitmap(request.ImgSize.Width, request.ImgSize.Height);
			Graphics templateGph = Graphics.FromImage(templateImg);
			templateGph.InterpolationMode = InterpolationMode.High;
			templateGph.SmoothingMode = SmoothingMode.HighQuality;
			templateGph.Clear(Color.White);
			templateGph.DrawImage(initImage, new Rectangle(0, 0, request.ImgSize.Width, request.ImgSize.Height),
				new Rectangle(0, 0, initImage.Width, initImage.Height), GraphicsUnit.Pixel);
			templateGph.Dispose();
			initImage.Dispose();
			return new HandledImage()
			{
				Img = templateImg,
				Name = string.Format(request.SrcName + "_{0}_{1}_{2}", request.ImgSize.Width, request.ImgSize.Height, request.Quality),
			};
		}
		private HandledImage HeightCut(HandledImageRequest request,Image initImage, double templateRate)
		{
			int newHeight = (int)Math.Floor(initImage.Width / templateRate);
			Image pickedImg = new Bitmap(initImage.Width, newHeight);
			Graphics pickedGph = Graphics.FromImage(pickedImg);
			Rectangle fromR = new Rectangle();
			fromR.X = 0; fromR.Y = (int)Math.Floor((double)(initImage.Height - newHeight) / 2);
			fromR.Width = initImage.Width; fromR.Height = newHeight;

			Rectangle toR = new Rectangle();
			toR.X = 0; toR.Y = 0;
			toR.Width = initImage.Width; toR.Height = newHeight;
			// 裁减
			pickedGph.DrawImage(initImage, toR, fromR, GraphicsUnit.Pixel);
			pickedGph.Dispose();
			initImage.Dispose();
			return ScaleCut(request, pickedImg);
		}