As Size
, Width
and Height
are Get()
properties of System.Drawing.Image
;
How can I resize an Image object at run-time in C#?
Right now, I am just creating a new Image
using:
// objImage is the original Image Bitmap objBitmap = new Bitmap(objImage, new Size(227, 171));
One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element. The values are set in px i.e. CSS pixels.
This will perform a high quality resize:
/// <summary> /// Resize the image to the specified width and height. /// </summary> /// <param name="image">The image to resize.</param> /// <param name="width">The width to resize to.</param> /// <param name="height">The height to resize to.</param> /// <returns>The resized image.</returns> public static Bitmap ResizeImage(Image image, int width, int height) { var destRect = new Rectangle(0, 0, width, height); var destImage = new Bitmap(width, height); destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); using (var graphics = Graphics.FromImage(destImage)) { graphics.CompositingMode = CompositingMode.SourceCopy; graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; using (var wrapMode = new ImageAttributes()) { wrapMode.SetWrapMode(WrapMode.TileFlipXY); graphics.DrawImage(image, destRect, 0, 0, image.Width,image.Height, GraphicsUnit.Pixel, wrapMode); } } return destImage; }
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)destImage.SetResolution
maintains DPI regardless of physical size -- may increase quality when reducing image dimensions or when printinggraphics.CompositingMode
determines whether pixels from a source image overwrite or are combined with background pixels. SourceCopy
specifies that when a color is rendered, it overwrites the background color.graphics.CompositingQuality
determines the rendering quality level of layered images.graphics.InterpolationMode
determines how intermediate values between two endpoints are calculatedgraphics.SmoothingMode
specifies whether lines, curves, and the edges of filled areas use smoothing (also called antialiasing) -- probably only works on vectorsgraphics.PixelOffsetMode
affects rendering quality when drawing the new imageMaintaining 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).
Also, this is a good article describing some of the pitfalls with image resizing. The above function will cover most of them, but you still have to worry about saving.
Not sure what is so difficult about this, do what you were doing, use the overloaded Bitmap constructor to create a re-sized image, the only thing you were missing was a cast back to the Image data type:
public static Image resizeImage(Image imgToResize, Size size) { return (Image)(new Bitmap(imgToResize, size)); } yourImage = resizeImage(yourImage, new Size(50,50));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With