使用C#如何调整jpeg图像的大小?

使用C#如何调整jpeg图像的大小?,第1张

概述使用C#如何调整jpeg图像大小?代码示例会很棒. public static class ImageHelper{ /// <summary> /// Resize the image to the specified width and height. /// </summary> /// <param name="image">The image to r 使用C#如何调整jpeg图像的大小?代码示例会很棒.解决方法
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,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,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,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);            }        }    }}
总结

以上是内存溢出为你收集整理的使用C#如何调整jpeg图像的大小?全部内容,希望文章能够帮你解决使用C#如何调整jpeg图像的大小?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1251746.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存