scale image for fit size without stretch - c#

I want resize an Image for fit a size (width and height) without stretch the original image.
My code, resize the image to the specified size but stretch the original image.
this is the code:
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.Tile);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}

So you want to Scale with respect to Aspect Ratio.
What we should do is to get a factor and resize both width and height by that factor.
And we need not to go above desired width and height (that's why we use Math.Min on both width and height ratios).
double oldWidth = image.Width;
double oldHeight = image.Height;
var widthRatio = width / oldWidth;
var heightRatio = height / oldHeight;
var factor = Math.Min(widthRatio, heightRatio);
and here we use it like this:
var destRect = new Rectangle(0, 0, (int)(factor * oldWidth), (int)(factor * oldHeight));

Related

Losing image quality when resize in PictureBox Form C#

I have a form without border and need show only a image in the corner of window, the picture dimentions are 9567 x 18012 and size is 62 MB, i use a PictureBox in a form when resize the quality loss, why?, i a need a thirdparty library for show a image to big in dimensions and size?
The left image is a PictureBox with my image and the right is the original image with te information
Use the following code to reduce the image size.
public void SetPictureBoxImage(string path)
{
Image myImage = Image.FromFile(path, true);
int width = 500;
double ratio = (double)myImage.Width / (double)myImage.Height;
int height = Convert.ToInt32(width / ratio);
if (height > width)
{
ratio = (double)myImage.Height / (double)myImage.Width;
height = width;
width = Convert.ToInt32(height / ratio);
}
pictureBox1.Image = ResizeImage(myImage, width, height);
}
Resize Method
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);
}
}
return destImage;
}

How do I resize an image to fill the new dimensions?

I am trying to stretch an image to fit new dimensions, but I am not being able to figure out how to make the image to fill the new dimensions, it only creates a larger image size but it keeps the image untouched, I want the image to fill width and height specified.
private Bitmap resizeImage(Image image, int width, int height, float HorizontalResolution, float VerticalResolution)
{
Rectangle destRect = new Rectangle(0, 0, width, height);
Bitmap destImage = new Bitmap(width, height);
destImage.SetResolution(HorizontalResolution, 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;
}
Since you want to stretch the entire source image to a specified size, just use the simple five-argument overload that doesn't accept source coordinates:
DrawImage(Image, Int32, Int32, Int32, Int32)
Like so:
graphics.DrawImage(image, 0, 0, width, height);
Although none of the answers worked for me, I appreciate all the help and suggestions on which I could get to a solution pretty fast, here is the piece of code that worked for me:
private Bitmap resizeImage(Image image, int width, int height, float HorizontalResolution, float VerticalResolution)
{
Rectangle destRect = new Rectangle(0, 0, width, height);
Bitmap destImage = new Bitmap(width, height);
destImage.SetResolution(HorizontalResolution, VerticalResolution);
using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
//drawImage must be set this way to get the desired outcome
graphics.DrawImage(imageBox.Image, new Rectangle(0, 0, image.Width, image.Height), destRect, GraphicsUnit.Pixel);
}
return destImage;
}
Basically I removed the wrapMode code and changed my drawImage method.
You need to draw your image with the newer width and height. It will look like this:
graphics.DrawImage(image, destRect, 0, 0, destRect.Width, destRect.Height, GraphicsUnit.Pixel, wrapMode);

Resize image with percentage value in C#

I would like to know if its possible in C# to resize image by given percentage proportionally.
For example I have image from which I want to create thumbnail, and I want to resize to 25% of the size, is it possible to put the percentage and apply that within specific method?
I use this method to resize images in an application i made. It uses 'System.Drawing.2D'
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;
}

How to resize images with maintaining quality

I am trying to resize image to 500x500 px. Using below code:
public static Bitmap ResizeImage(System.Drawing.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;
}
But this just resizes horizontally and not doing cropping! Someone suggested me to crop the image to a centered 1:1 ratio and then downscale it.
I also wish to increase size of image to 500x500 if it is less than it.
How can I achieve it?
Instead of
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
Use
Rectangle cropRect = new Rectangle(0,0,500,500);
graphics.DrawImage(image, destRect, cropRect, GraphicsUnit.Pixel);
and you can also read more about DrawImageUnscaledAndClipped for cropping purpose.

Re size image not working C# [duplicate]

This question already has answers here:
Resize image proportionally with MaxHeight and MaxWidth constraints
(3 answers)
Closed 9 years ago.
Am re sizing an image with the following code
using (Image thumbnail = new Bitmap(100, 50))
{
using (Bitmap source = new Bitmap(imageFile))
{
using (Graphics g = Graphics.FromImage(thumbnail))
{
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(source, 0, 0, 100, 50);
}
}
using (MemoryStream ms = new MemoryStream())
{
thumbnail.Save(ms, ImageFormat.Png);
thumbnail.Save(dest, ImageFormat.Png);
}
}
but it is not giving an image of any quality. pixelation is making the image wired.
i have also tried the code
image re size in stack
but am getting a black screen as the result instead of jpg am using png is the only difference.
any suggestion for improving the image quality. i have to re size the transparent image to a size 100by50.
Thanks in advance.
Try this, Assuming you can use it
public static Image Resize(Image originalImage, int w, int h)
{
//Original Image attributes
int originalWidth = originalImage.Width;
int originalHeight = originalImage.Height;
// Figure out the ratio
double ratioX = (double)w / (double)originalWidth;
double ratioY = (double)h / (double)originalHeight;
// use whichever multiplier is smaller
double ratio = ratioX < ratioY ? ratioX : ratioY;
// now we can get the new height and width
int newHeight = Convert.ToInt32(originalHeight * ratio);
int newWidth = Convert.ToInt32(originalWidth * ratio);
Image thumbnail = new Bitmap(newWidth, newHeight);
Graphics graphic = Graphics.FromImage(thumbnail);
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphic.SmoothingMode = SmoothingMode.HighQuality;
graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphic.CompositingQuality = CompositingQuality.HighQuality;
graphic.Clear(Color.Transparent);
graphic.DrawImage(originalImage, 0, 0, newWidth, newHeight);
return thumbnail;
}

Categories

Resources