Reputation: 303
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;
}
Upvotes: 0
Views: 1386
Reputation: 303
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.
Upvotes: 1
Reputation: 283634
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:
Like so:
graphics.DrawImage(image, 0, 0, width, height);
Upvotes: 1
Reputation: 7640
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);
Upvotes: 0