Torna al Thread

#region crop image enum Dimensions { Width, Height } enum AnchorPosition { Top, Center, Bottom, Left, Right } public static void CropImage(Bitmap imgStr, int Width, int Height, string filename) { //creo il bitmap dallo stream System.Drawing.Image bmpStream = imgStr; System.Drawing.Image imgPhoto = Crop(bmpStream, Width, Height, AnchorPosition.Center); imgPhoto.Save(filename, ImageFormat.Jpeg); imgPhoto.Dispose(); } public static void CropImage(Bitmap imgStr, int Width, int Height, int X, int Y, string filename) { //creo il bitmap dallo stream System.Drawing.Image bmpStream = imgStr; System.Drawing.Image imgPhoto = Crop(bmpStream, Width, Height, X, Y); imgPhoto.Save(filename, ImageFormat.Jpeg); imgPhoto.Dispose(); } static System.Drawing.Image Crop(System.Drawing.Image imgPhoto, int Width, int Height, AnchorPosition Anchor) { int sourceWidth = imgPhoto.Width; int sourceHeight = imgPhoto.Height; int sourceX = 0; int sourceY = 0; int destX = 0; int destY = 0; float nPercent = 0; float nPercentW = 0; float nPercentH = 0; nPercentW = ((float)Width / (float)sourceWidth); nPercentH = ((float)Height / (float)sourceHeight); if (nPercentH > nPercentW) { nPercent = nPercentW; switch (Anchor) { case AnchorPosition.Top: destY = 0; break; case AnchorPosition.Bottom: destY = (int) (Height - (sourceHeight * nPercent)); break; default: destY = (int) ((Height - (sourceHeight * nPercent)) / 2); break; } } else { nPercent = nPercentH; switch (Anchor) { case AnchorPosition.Left: destX = 0; break; case AnchorPosition.Right: destX = (int) (Width - (sourceWidth * nPercent)); break; default: destX = (int) ((Width - (sourceWidth * nPercent)) / 2); break; } } int destWidth = (int)(sourceWidth * nPercent); int destHeight = (int)(sourceHeight * nPercent); Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb); bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); Graphics grPhoto = Graphics.FromImage(bmPhoto); grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; grPhoto.Clear(Color.White); grPhoto.DrawImage(imgPhoto, new Rectangle(destX, destY, destWidth, destHeight), new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel); grPhoto.Dispose(); return bmPhoto; } static System.Drawing.Image Crop(System.Drawing.Image imgPhoto, int Width, int Height, int X, int Y) { if (Width > imgPhoto.Width) Width = imgPhoto.Width; if (Height > imgPhoto.Height) Height = imgPhoto.Height; if (X > (imgPhoto.Width - Width)) X = (imgPhoto.Width - Width); if (Y > (imgPhoto.Height - Height)) Y = (imgPhoto.Height - Height); Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);//creo img vuota bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); Graphics grPhoto = Graphics.FromImage(bmPhoto); grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, Width, Height), new Rectangle(X, Y, Width, Height), GraphicsUnit.Pixel); grPhoto.Dispose(); return bmPhoto; } #endregion
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5