private void CropFromFile(string file, string path, int width, int height, CropAnchorPosition anchor, int quality) { using(FileStream imageContents = new FileStream(file, FileMode.Open, FileAccess.Read)) { Image original = Image.FromStream(imageContents); imageContents.Close(); CropToDimensions(ref original, path, width, height, anchor, quality); } }
public void SaveImageCropToDimensions(string file, string path, int width, int height, CropAnchorPosition anchor, int quality) { CropFromFile(file, path, width, height, anchor, quality); }
private void CropToDimensions(ref Image original, string path, int width, int height, CropAnchorPosition anchor, int quality) { int sourceWidth = original.Width; int sourceHeight = original.Height; int sourceX = 0; int sourceY = 0; int destX = 0; int destY = 0; float nPercent = 0; float nPercentW = ((float)width / (float)sourceWidth); float nPercentH = ((float)height / (float)sourceHeight); if(nPercentH < nPercentW) { nPercent = nPercentW; switch(anchor) { case CropAnchorPosition.Top: destY = 0; break; case CropAnchorPosition.Bottom: destY = (int)(height - (sourceHeight * nPercent)); break; default: destY = (int)((height - (sourceHeight * nPercent)) / 2); break; } } else { nPercent = nPercentH; switch(anchor) { case CropAnchorPosition.Left: destX = 0; break; case CropAnchorPosition.Right: destX = (int)(width - (sourceWidth * nPercent)); break; default: destX = (int)((width - (sourceWidth * nPercent)) / 2); break; } } // Adding one should cure the black border bug int destWidth = (int)(sourceWidth * nPercent) + 1; int destHeight = (int)(sourceHeight * nPercent) + 1; Bitmap resized = new Bitmap(width, height, PixelFormat.Format24bppRgb); resized.SetResolution(original.HorizontalResolution, original.VerticalResolution); Graphics g = Graphics.FromImage(resized); g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.SmoothingMode = SmoothingMode.HighQuality; g.PixelOffsetMode = PixelOffsetMode.HighQuality; g.CompositingQuality = CompositingQuality.HighQuality; // Fixes the weird border bug ImageAttributes attr = new ImageAttributes(); attr.SetWrapMode(WrapMode.TileFlipXY); g.DrawImage(original, new Rectangle(destX, destY, destWidth, destHeight), sourceX, sourceY, sourceWidth, sourceHeight, GraphicsUnit.Pixel, attr); attr.Dispose(); g.Dispose(); ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders(); EncoderParameters encoderParameters = new EncoderParameters(1); encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)quality); resized.Save(path, info[1], encoderParameters); resized.Dispose(); return; }
private void CropFromHttpPostedFileBase(HttpPostedFileBase file, string path, int width, int height, CropAnchorPosition anchor, int quality) { if(file.ContentLength < (_uploadmaxsize * 1024)) { Image original = Image.FromStream(file.InputStream); CropToDimensions(ref original, path, width, height, anchor, quality); } else { throw new Exception("File size was too large (upload: " + (file.ContentLength / 1024) + "kb, maximum " + _uploadmaxsize + "kb)"); } }
public void SaveImageCropToDimensions(HttpPostedFileBase file, string path, int width, int height, CropAnchorPosition anchor) { CropFromHttpPostedFileBase(file, path, width, height, anchor, 100); }
public void SaveImageCropToDimensions(MemoryStream file, string path, int width, int height, CropAnchorPosition anchor) { CropFromMemoryStream(file, path, width, height, anchor, 100); }