![]() 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. 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.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |