In the application I made it was necessary to create a function with multiple options. Graph.DrawImage(source, (trimValue / 2), 0, scaleWidth, destinationBounds.Height) ScaleWidth = sourceBounds.Width * resizeRatio įloat trimValue = destinationBounds.Width - scaleWidth ResizeRatio = destinationBounds.Height/sourceBounds.Height Graph.DrawImage(source, 0, (trimValue / 2), destinationBounds.Width, scaleHeight) ScaleHeight = sourceBounds.Height * resizeRatio įloat trimValue = destinationBounds.Height - scaleHeight ResizeRatio = destinationBounds.Width / sourceBounds.Width SourceRatio = (float)source.Width / (float)source.Height Graph.FillRectangle(new SolidBrush(), destinationBounds) Graphics graph = Graphics.FromImage(destinationImage) Image destinationImage = new Bitmap((int)destinationBounds.Width, (int)destinationBounds.Height) RectangleF scaleBounds = new RectangleF() RectangleF sourceBounds = new RectangleF(0.0f,0.0f,(float)source.Width, (float)source.Height) public Image ResizeImage(Image source, RectangleF destinationBounds) This is the code that I worked out for a specific requirement ie: the destination is always in landscape ratio. Thanks:) public Image resizeImage(int newWidth, int newHeight, string stPhotoPath) but will convert transparent pixel to white instead of black. This code is same as posted from one of above answers. 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(newWidth, newHeight,īmPhoto.SetResolution(imgPhoto.HorizontalResolution, Int destWidth = (int)(sourceWidth * nPercent) NPercentH = ((float)newHeight / (float)sourceHeight) ĭestX = 16((newWidth -ĭestY = 16((newHeight. NPercentW = ((float)newWidth / (float)sourceWidth) Int sourceX = 0, sourceY = 0, destX = 0, destY = 0 įloat nPercent = 0, nPercentW = 0, nPercentH = 0 Image imgPhoto = Image.FromFile(stPhotoPath) In this question, you'll have some answers, including mine: public Image resizeImage(int newWidth, int newHeight, string stPhotoPath) The above function will cover most of them, but you still have to worry about saving. Maintaining aspect ratio is left as an exercise for the reader (actually, I just don't think it's this function's job to do that for you).Īlso, this is a good article describing some of the pitfalls with image resizing. graphics.PixelOffsetMode affects rendering quality when drawing the new image.graphics.SmoothingMode specifies whether lines, curves, and the edges of filled areas use smoothing (also called antialiasing) - probably only works on vectors.graphics.InterpolationMode determines how intermediate values between two endpoints are calculated.graphics.CompositingQuality determines the rendering quality level of layered images.SourceCopy specifies that when a color is rendered, it overwrites the background color. graphics.CompositingMode determines whether pixels from a source image overwrite or are combined with background pixels.Compositing controls how pixels are blended with the background - might not be needed since we're only drawing one thing.destImage.SetResolution maintains DPI regardless of physical size - may increase quality when reducing image dimensions or when printing.wrapMode.SetWrapMode(WrapMode.TileFlipXY) prevents ghosting around the image borders - naïve resizing will sample transparent pixels beyond the image boundaries, but by mirroring the image we can get a better sample (this setting is very noticeable).Graphics.DrawImage(image, destRect, 0, 0, image.Width,image.Height, GraphicsUnit.Pixel, wrapMode) WrapMode.SetWrapMode(WrapMode.TileFlipXY) Using (var wrapMode = new ImageAttributes()) Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality Graphics.SmoothingMode = SmoothingMode.HighQuality Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic Graphics.CompositingQuality = CompositingQuality.HighQuality Graphics.CompositingMode = CompositingMode.SourceCopy Using (var graphics = Graphics.FromImage(destImage)) Var destImage = new Bitmap(width, height) ĭestImage.SetResolution(image.HorizontalResolution, image.VerticalResolution) Var destRect = new Rectangle(0, 0, width, height) Public static Bitmap ResizeImage(Image image, int width, int height) / Resize the image to the specified width and height. This will perform a high quality resize: ///
0 Comments
Leave a Reply. |