小智 9
public static class ImageHelper
{
/// <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;
}
public static Bitmap ResizeImage(Image image, decimal percentage)
{
int width = (int)Math.Round(image.Width * percentage, MidpointRounding.AwayFromZero);
int height = (int)Math.Round(image.Height * percentage, MidpointRounding.AwayFromZero);
return ResizeImage(image, width, height);
}
}
class Program
{
static void Main(string[] args)
{
string fileName = @"C:\Images\MyImage.jpg";
FileInfo info = new FileInfo(fileName);
using (Image image = Image.FromFile(fileName))
{
using(Bitmap resizedImage = ImageHelper.ResizeImage(image, 0.25m))
{
resizedImage.Save(
info.DirectoryName + "\\"
+ info.Name.Substring(0, info.Name.LastIndexOf(info.Extension))
+ "_" + resizedImage.Width + "_" + resizedImage.Height
+ info.Extension,
ImageFormat.Jpeg);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用这个:
public static void ResizeJpg(string path, int nWidth, int nHeight)
{
using (var result = new Bitmap(nWidth, nHeight))
{
using (var input = new Bitmap(path))
{
using (Graphics g = Graphics.FromImage((System.Drawing.Image)result))
{
g.DrawImage(input, 0, 0, nWidth, nHeight);
}
}
var ici = ImageCodecInfo.GetImageEncoders().FirstOrDefault(ie => ie.MimeType == "image/jpeg");
var eps = new EncoderParameters(1);
eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
result.Save(path, ici, eps);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 2
C#(或者更确切地说:.NET 框架)本身不提供此类功能,但它确实为您提供了 System.Drawing 中的位图,以便轻松访问各种图片格式的原始像素数据。其余部分,请参阅http://en.wikipedia.org/wiki/Image_scaling
| 归档时间: |
|
| 查看次数: |
23583 次 |
| 最近记录: |