hello friends ...
I am Uploading single Image And That image i convert in different Size Because That Image I want in 5 different Size But The Problem Is Some Images Are Crop And Padding That I Want To Avoid .....So How To Avoid It ...This All I am Doing Using Asp.Net Mvc ..
Given Below Is My Code Of Image Resizer :-
public static string UploadFile(HttpPostedFileBase file, string file_Name, int height, int width)
{
var path = Path.Combine(HttpContext.Current.Request.MapPath(UploadPath), file_Name);
file.SaveAs(path);
ResizeSettings resizeSetting = new ResizeSettings
{
Width = width,
Height = height,
Mode = FitMode.Crop,
Quality = 90
};
ImageBuilder.Current.Build(file, path, resizeSetting);
return file_Name;
}
4 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.

Hussam BarouqaPosted Feb 23, 2017, 6:39 AM
{
using (Bitmap _MyBitmap = new Bitmap(@"C:\InputImage.jpg"))
{
ImageCodecInfo _MyJPGEncoder = GetEncoder(ImageFormat.Jpeg);
System.Drawing.Imaging.Encoder _MyEncoder = System.Drawing.Imaging.Encoder.Quality;
EncoderParameters _MyEncoderParameters = new EncoderParameters(1);
EncoderParameter _MyEncoderParameter = new EncoderParameter(_MyEncoder, 50L);
_MyEncoderParameters.Param[0] = _MyEncoderParameter;
_MyBitmap.Save(@"C:\OutputImage.jpg", _MyJPGEncoder, _MyEncoderParameters);
}
}
private ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] _MyCodecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in _MyCodecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
RasterCodecs _MyCodecs = new RasterCodecs();
RasterImage _SrcImage = _MyCodecs.Load(@"C:\InputImage.jpg", 200, 400, 0, RasterSizeFlags.Resample, CodecsLoadByteOrder.Bgr);
_MyCodecs.Options.Jpeg.Save.QualityFactor = 80;
_MyCodecs.Save(_SrcImage, @"C:\OutputImage.jpg", RasterImageFormat.Jpeg, 24);
Guest UserPosted Feb 20, 2017, 9:37 AM
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
///
/// Class contaning method to resize an image and save in JPEG format
///
public class ImageHandler
{
///
/// Method to resize, convert and save the image.
///
/// name="image">Bitmap image.
/// name="maxWidth">resize width.
/// name="maxHeight">resize height.
/// name="quality">quality setting value.
/// name="filePath">file path.
public void Save(Bitmap image, int maxWidth, int maxHeight, int quality, string filePath)
{
// Get the image's original width and height
int originalWidth = image.Width;
int originalHeight = image.Height;
// To preserve the aspect ratio
float ratioX = (float)maxWidth / (float)originalWidth;
float ratioY = (float)maxHeight / (float)originalHeight;
float ratio = Math.Min(ratioX, ratioY);
// New width and height based on aspect ratio
int newWidth = (int)(originalWidth * ratio);
int newHeight = (int)(originalHeight * ratio);
// Convert other formats (including CMYK) to RGB.
Bitmap newImage = new Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb);
// Draws the image in the specified size with quality mode set to HighQuality
using (Graphics graphics = Graphics.FromImage(newImage))
{
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.DrawImage(image, 0, 0, newWidth, newHeight);
}
// Get an ImageCodecInfo object that represents the JPEG codec.
ImageCodecInfo imageCodecInfo = this.GetEncoderInfo(ImageFormat.Jpeg);
// Create an Encoder object for the Quality parameter.
Encoder encoder = Encoder.Quality;
// Create an EncoderParameters object.
EncoderParameters encoderParameters = new EncoderParameters(1);
// Save the image as a JPEG file with quality level.
EncoderParameter encoderParameter = new EncoderParameter(encoder, quality);
encoderParameters.Param[0] = encoderParameter;
newImage.Save(filePath, imageCodecInfo, encoderParameters);
}
///
/// Method to get encoder infor for given image format.
///
/// name="format">Image format
/// image codec info.
private ImageCodecInfo GetEncoderInfo(ImageFormat format)
{
return ImageCodecInfo.GetImageDecoders().SingleOrDefault(c => c.FormatID == format.Guid);
}
}
Dr.Ajay KashyapPosted Feb 20, 2017, 6:54 AM
Guest UserPosted Feb 20, 2017, 4:53 AM
Resize Image In C#