C#剪切生成高质量缩放图片

/// <summary>
        /// 高质量缩放图片
        /// </summary>
        /// <param name="OriginFilePath">源图的路径</param>
        /// <param name="TargetFilePath">存储缩略图的路径</param>
        /// <param name="DestWidth">缩放后图片宽度</param>
        /// <param name="DestHeight">缩放后图片高度</param>
        /// <returns>表明此次操作是否成功</returns>
        public static bool ToSuolue(System.IO.Stream oStream, string TargetFilePath, int towidth, int toheight)
        {
            //System.Drawing.Image OriginImage = System.Drawing.Image.FromFile(OriginFilePath);
            System.Drawing.Image OriginImage = System.Drawing.Image.FromStream(oStream);
            System.Drawing.Imaging.ImageFormat thisFormat = OriginImage.RawFormat;
            //按比例缩放
            int x = 0, y = 0;
            int ow = OriginImage.Width;
            int oh = OriginImage.Height;

            if ((double)ow / (double)oh > (double)towidth / (double)toheight)
            {
                oh = OriginImage.Height;
                ow = OriginImage.Height * towidth / toheight;
                y = 0;
                x = (OriginImage.Width - ow) / 2;
            }
            else
            {
                ow = OriginImage.Width;
                oh = OriginImage.Width * toheight / towidth;
                x = 0;
                y = (OriginImage.Height - oh) / 2;
            }

            Bitmap bt = new Bitmap(towidth, toheight); //根据指定大小创建Bitmap实例
            using (Graphics g = Graphics.FromImage(bt))
            {
                g.Clear(Color.White);
                //设置画布的描绘质量
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.DrawImage(OriginImage, new Rectangle(0, 0, towidth, toheight),
                    new System.Drawing.Rectangle(x, y, ow, oh),
                System.Drawing.GraphicsUnit.Pixel);
                g.Dispose();
            }

            System.Drawing.Imaging.EncoderParameters EncoderParams = new System.Drawing.Imaging.EncoderParameters(); //取得内置的编码器
            long[] Quality = new long[1];
            Quality[0] = 100;
            System.Drawing.Imaging.EncoderParameter EncoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, Quality);
            EncoderParams.Param[0] = EncoderParam;

            //try
            //{
                //获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象
                System.Drawing.Imaging.ImageCodecInfo[] arrayICI = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
                System.Drawing.Imaging.ImageCodecInfo jpegICI = null;
                for (int i = 0; i < arrayICI.Length; i++)
                {
                    if (arrayICI[i].FormatDescription.Equals("JPEG"))
                    {
                        jpegICI = arrayICI[i]; //设置为JPEG编码方式
                        break;
                    }
                }

                if (jpegICI != null) //保存缩略图
                {
                    bt.Save(TargetFilePath, jpegICI, EncoderParams);
                }
                else
                {
                    bt.Save(TargetFilePath, thisFormat);
                }
                OriginImage.Dispose();
                return true;
            //}
            //catch (System.Runtime.InteropServices.ExternalException e1)  //GDI+发生一般错误
            //{
            //    return false;
            //}
            //catch (Exception e2)
            //{
            //    return false;
            //}
        }
时间: 2024-08-04 17:33:31

C#剪切生成高质量缩放图片的相关文章

移动Web—CSS为Retina屏幕替换更高质量的图片

来源:互联网 作者:佚名 时间:12-24 10:37:45 [大 中 小] 点评:Retian似乎是屏幕显示的一种趋势,这也是Web设计师面对的一个新挑战;移动应用程序的设计师们已经学会了如何为Retina屏幕设备显示更好质量的图片,用来提高用户的体验;在本文中,你将看到如何使用CSS3技巧在Web应用中为Retina屏幕替换更高质量的图片 特别声明:此篇文章根据Stéphanie Walter 的英文文章<The Mobile Web: CSS Image Replacement for R

推荐几款高质量的图片网站

推荐几款高质量的图片网站 能你还不知道在用的哪些高质量的图片网站,是不是有点out了哈 ~ 今天良心推荐,下面推荐几个热门.国内主题摄影.小众图库.COO图片搜索引擎.PPT大背景图片.矢量图标类.建筑绘图类及其他相关的网站应有尽有!欢迎转发~ 目录一.热门网站二.国内主题摄影三.小众图库四.CCO图片搜索引擎五.PPT大背景图片六.矢量图标类七.建筑绘图类八.其他 一.热门网站1)Pixabayhttps://pixabay.com/ 2)unsplashhttps://unsplash.co

Python高质量缩放切图

最近刚接触Python,以迅雷不及掩耳盗铃之势(只是迫不及待)应用到工作中去了之前用 cmd+photoshop做批量图像处理(缩放切片),在执行效率(速度)上和灵活度上有很大限制,遂转战Python(我对于大语种庞大的类库感到力不从心,喜欢用脚本语言来快速编辑.执行)真正开始学习Python第一天,我奋战到深夜三点,勉强完成了可以替代先前用“cmd+photoshop快捷批处理”实现的切图工具,执行效率大概是先前的10倍.为什么说是“勉强”,因为输出图像质量没Photoshop输出的好.Pyt

C#放缩、截取、合并图片并生成高质量新图的类

using System;using System.Drawing;using System.Drawing.Imaging;using System.Drawing.Drawing2D;using System.IO; namespace Framework{public class ImageClass{        public Image ResourceImage;        public int Width=0;        public int Height=0;     

MPlayer-ww 增加边看边剪切功能+高质量GIF生成功能

http://pan.baidu.com/s/1eQm5a74 下载FFmpeg palettegen paletteuse documentation 需要下载 FFmpeg2.6 以上 并FFmpeg.exe所在的目录名添加到系统环境变量PATH中 解压到 D:\MPlayer-ww 运行 copy_font.bat 安装字体 LED_font.ttf 双击 MPlayer_ww_openWith.reg 添加右键播放功能 outformat.ini 配置视频分割命令参数 ; 往前0.05秒

c# 无损高质量压缩图片代码

最近,项目上涉及到了图像压缩,发现原有的图像压缩功能,虽然保证了图像的大小300K以内,但是压缩后的图像看的不在清晰,并且,限定了图片的Height或者是Width. 在CSDN上看到了一个压缩算法:http://blog.csdn.net/qq_16542775/article/details/51792149 进过测试这个算法,发现,将原始图像的大小进行对半处理,然后迭代跳转压缩质量参数,可以得到不错的效果. 修改后的算法如下: /// <summary> /// 无损压缩图片 /// &

c#无损高质量压缩图片

这几天在做同城交友网www.niyuewo.com时遇到的一个问题,如何将会员的头像压缩,在网上搜索整理如下:在此也感谢医药精(www.yiyaojing.com)站长的帮忙 /// <summary> /// 无损压缩图片 /// </summary> /// <param name="sFile">原图片</param> /// <param name="dFile">压缩后保存位置</param

高质量的图片压缩

/// <summary> /// 无损压缩图片 /// </summary> /// <param name="sFile">原图片</param> /// <param name="dFile">压缩后保存位置</param> /// <param name="dHeight">高度</param> /// <param name="

全球依靠PointWise.18.0.R1.build.20160823网格计算分析高质量

全球依靠PointWise.18.0.R1.build.20160823网格计算分析高质量 自1984年以来被全球的工程师和科学家能够可靠地生成高质量的网格CFD.它可以生成高精度的网格以使 得分析结果更加准确.同时它还可以分析并不完美的CAD模型,同时不需要人工清理模型.PointWise.18.0.R1.build.20160823Carlson.SurvPC.v5.03ZWCAD.ZW3D.2016.SP.v20.00.CHINESE中望CAD软件BUW.SmartAssembly.6.0