![]() Private static void ResizeImage(string originalPath, string originalFileName, string newPath, string newFileName, int maximumWidth, int maximumHeight, bool enforceRatio, bool addPadding) This is used for maintaining a 1:1 ratio if the max width and height are the same. If set to true, the white padding fills the smaller dimension until it reach the specified max width or height. / Indicates whether fill the smaller dimension of the image with a white background. If set to true, the width/height ratio aspect is maintained and distortion does not occur. If set to false, images with an unequal width and height will be distorted and padding is disregarded. / Indicates whether to keep the width/height ratio aspect or not. / When resizing the image, this is the maximum height to resize the image to. / When resizing the image, this is the maximum width to resize the image to. / The folder which will hold the resized image. / The folder which holds the original image. ![]() / Resizes an image to a new width and height. ResizeImage(path, "large.jpg", path, "new.jpg", 100, 100, true, true) Var path = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName This is a modified version of egrunin's code. This code will allow you resize an image with or without the aspect ratio being enforced or to resize with padding. New Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), New Rectangle(destX, destY, destWidth, destHeight), Graphics grPhoto = Graphics.FromImage(bmPhoto) ![]() Int destHeight = (int)(sourceHeight * nPercent) īitmap bmPhoto = new Bitmap(Width, Height,īmPhoto.SetResolution(imgPhoto.HorizontalResolution, Int destWidth = (int)(sourceWidth * nPercent) NPercentH = ((float)Height / (float)sourceHeight) NPercentW = ((float)Width / (float)sourceWidth) static Image FixedSize(Image imgPhoto, int Width, int Height) I found out how to resize AND pad the image by learning from this this CodeProject Article. Those who want to improve this code should put it in the comments, or a new answer. Thumbnail.Save(path + newWidth + "." + originalFilename, info, Graphic.DrawImage(image, posX, posY, newWidth, newHeight) Graphic.Clear(Color.White) // white padding Int posY = Convert.ToInt32((canvasHeight - (originalHeight * ratio)) / 2) Int posX = Convert.ToInt32((canvasWidth - (originalWidth * ratio)) / 2) Now calculate the X,Y position of the upper-left corner ![]() Int newWidth = Convert.ToInt32(originalWidth * ratio) Int newHeight = Convert.ToInt32(originalHeight * ratio) now we can get the new height and width * - new code - */ĭouble ratioX = (double) canvasWidth / (double) originalWidth ĭouble ratioY = (double) canvasHeight / (double) originalHeight ĭouble ratio = ratioX < ratioY ? ratioX : ratioY New Bitmap(canvasWidth, canvasHeight) // changed parm names private void resizeImage(string path, string originalFilename, Thumbnail.Save(path + width + "." + originalFilename, info,ĮDIT: I'd like to have the image padded instead of cropped info =ĮncoderParameters = new EncoderParameters(1) ĮncoderParameters.Param = new EncoderParameter(Encoder.Quality, Graphic.DrawImage(image, 0, 0, width, height) Graphic.CompositingQuality = CompositingQuality.HighQuality Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality Graphic.SmoothingMode = SmoothingMode.HighQuality Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic Image image = Image.FromFile(path + originalFilename) It works well for resizing but changing the aspect ratio of the original image squashes the new image. The extra 50 pixels of the height need to be padded with a white background color. The result would guarantee that the whole image is shown.I'm trying to resize an image while preserving the aspect ratio from the original image so the new image doesn't look squashed.Ĭonvert a 150*100 image into a 150*150 image. Fitįit content mode attempts to show the whole image by scales the image to fit the view size along one axis, possibly leaving empty space along the other axis. The method accept one of two strategies of ContentMode, fit and fill. aspectRatio(_:contentMode:) modifier to constraints the view dimensions. Sponsor and reach thousands of iOS developers.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |