惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threat Research - Cisco Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
V
Vulnerabilities – Threatpost
GbyAI
GbyAI
P
Proofpoint News Feed
L
LINUX DO - 热门话题
P
Palo Alto Networks Blog
A
About on SuperTechFans
T
Tenable Blog
M
MIT News - Artificial intelligence
IT之家
IT之家
I
Intezer
D
DataBreaches.Net
爱范儿
爱范儿
T
Threatpost
C
CERT Recently Published Vulnerability Notes
云风的 BLOG
云风的 BLOG
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
K
Kaspersky official blog
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
Y
Y Combinator Blog
Cyberwarzone
Cyberwarzone
酷 壳 – CoolShell
酷 壳 – CoolShell
D
Darknet – Hacking Tools, Hacker News & Cyber Security
H
Help Net Security
Microsoft Security Blog
Microsoft Security Blog
Spread Privacy
Spread Privacy
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
AWS News Blog
AWS News Blog
博客园 - 聂微东
C
Check Point Blog
S
Securelist
有赞技术团队
有赞技术团队
雷峰网
雷峰网
aimingoo的专栏
aimingoo的专栏
Last Week in AI
Last Week in AI
Stack Overflow Blog
Stack Overflow Blog
MongoDB | Blog
MongoDB | Blog
D
Docker
G
GRAHAM CLULEY
T
The Exploit Database - CXSecurity.com
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tailwind CSS Blog
L
Lohrmann on Cybersecurity
G
Google Developers Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LangChain Blog

博客园 - 飞舞的蒲公英

开发式新手引导设计思想 flash系统奔溃的主要原因 WinRAR(WinZip)压缩与解压实现(C#版Window平台) c#真正判断文件类型 winform文件拖入 c#winform选择文件,文件夹,打开指定目录方法 C#图片无损压缩 as3.0 动态文本属性大全 卡​马​克​卷​轴​算​法​研​究​_​地​图​双​缓​冲 春卷活动心得 As3 常用日期工具 As3 计算两个日期之间的天数差 解决Asp.net Mvc返回JsonResult中DateTime类型数据格式的问题 C#图片压缩算法 C#图片处理之: 另存为压缩质量可自己控制的JPEG C# :实现水印与图片合成,并利用Graphics 压缩图像质量 , (委托实现listBox的动态添加提示) 手机游戏模拟器汇总 用于开发 SQL SERVER 2008 无法启动T-SQL调试的解决方法 WinAPI 操作串口
C#放缩、截取、合并图片并生成高质量新图的类
飞舞的蒲公英 · 2013-01-10 · via 博客园 - 飞舞的蒲公英
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;
        private int ImageWidth;
        private int ImageHeight;
        private ImageFormat imgFormat;
        public string ErrMessage;

        public ImageClass(string ImageFileName)
        {
            ResourceImage = Image.FromFile(ImageFileName);
            Width = ResourceImage.Width;
            Height = ResourceImage.Height;
            imgFormat = ResourceImage.RawFormat;
            ErrMessage = "";
        }
        public ImageClass(byte[] Img)
        {
            MemoryStream imgStream = new MemoryStream(Img);
            try
            {
                ResourceImage = System.Drawing.Image.FromStream(imgStream);
                Width = ResourceImage.Width;
                Height = ResourceImage.Height;
                imgFormat = ResourceImage.RawFormat;
            }
            catch (Exception ex)
            {
                ErrMessage = ex.ToString();
            }
        }

        public bool ThumbnailCallback()
        {
            return false;
        }

        public Image GetReducedImage(int Width, int Height)
        {
            try
            {
                Image ReducedImage;

                Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);

                ReducedImage = ResourceImage.GetThumbnailImage(Width, Height, callb, IntPtr.Zero);

                return ReducedImage;
            }
            catch (Exception e)
            {
                ErrMessage = e.Message;
                return null;
            }
        }
        public bool GetReducedImage(int Width, int Height, string targetFilePath)
        {
            try
            {
                EncoderParameter p;
                EncoderParameters ps;

                ps = new EncoderParameters(1);

                p = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 85L);
                ps.Param[0] = p;

                Image ReducedImage;

                Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);

                ReducedImage = ResourceImage.GetThumbnailImage(Width, Height, callb, IntPtr.Zero);


                Graphics g = System.Drawing.Graphics.FromImage(ReducedImage);
                // 设置画布的描绘质量
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;

                //清空画布并以透明背景色填充
                g.Clear(Color.Transparent);

                g.DrawImage(ResourceImage, new Rectangle(0, 0, Width, Height), new Rectangle(0, 0, ResourceImage.Width, ResourceImage.Height), GraphicsUnit.Pixel);

                ImageCodecInfo imgCodecInfo = GetCodecInfo(imgFormat.ToString());

                if (imgCodecInfo == null)
                {
                    ReducedImage.Save(targetFilePath, imgFormat);
                }
                else
                {
                    ReducedImage.Save(targetFilePath, GetCodecInfo(targetFilePath), ps);
                }

                ReducedImage.Dispose();
                g.Dispose();

                return true;
            }
            catch (Exception e)
            {
                ErrMessage = e.Message;
                return false;
            }
        }

        public Image GetReducedImage(double Percent)
        {
            try
            {
                Image ReducedImage;

                Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);

                ImageWidth = Convert.ToInt32(ResourceImage.Width * Percent);
                ImageHeight = Convert.ToInt32(ResourceImage.Height * Percent);

                ReducedImage = ResourceImage.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero);

                return ReducedImage;
            }
            catch (Exception e)
            {
                ErrMessage = e.Message;
                return null;
            }
        }

        public bool GetReducedImage(double Percent, string targetFilePath)
        {
            try
            {
                EncoderParameter p;
                EncoderParameters ps;

                ps = new EncoderParameters(1);

                p = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 85L);
                ps.Param[0] = p;
                Image ReducedImage;

                Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);

                ImageWidth = Convert.ToInt32(ResourceImage.Width * Percent);
                ImageHeight = Convert.ToInt32(ResourceImage.Height * Percent);

                ReducedImage = ResourceImage.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero);

                Graphics g = System.Drawing.Graphics.FromImage(ReducedImage);

                // 设置画布的描绘质量
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;

                //清空画布并以透明背景色填充
                g.Clear(Color.Transparent);

                g.DrawImage(ResourceImage, new Rectangle(0, 0, ImageWidth, ImageHeight), new Rectangle(0, 0, ResourceImage.Width, ResourceImage.Height), GraphicsUnit.Pixel);

                ImageCodecInfo imgCodecInfo = GetCodecInfo(imgFormat.ToString());

                if (imgCodecInfo == null)
                {
                    ReducedImage.Save(targetFilePath, imgFormat);
                }
                else
                {
                    ReducedImage.Save(targetFilePath, GetCodecInfo(targetFilePath), ps);
                }

                ReducedImage.Dispose();
                g.Dispose();

                return true;
            }
            catch (Exception e)
            {
                ErrMessage = e.Message;
                return false;
            }
        }

        public bool GetReducedImage(string targetFilePath)
        {
            try
            {
                ResourceImage.Save(targetFilePath, ImageHelper.GetFormat(targetFilePath));

                return true;
            }
            catch (Exception e)
            {
                ErrMessage = e.Message;
                return false;
            }
        }

        public bool CaptureImg(string targetFilePath, int width, int height, int spaceX, int spaceY)
        {
            try
            {
                //载入底图   
                int x = 0;   //截取X坐标   
                int y = 0;   //截取Y坐标   
                //原图宽与生成图片宽   之差       
                //当小于0(即原图宽小于要生成的图)时,新图宽度为较小者   即原图宽度   X坐标则为0     
                //当大于0(即原图宽大于要生成的图)时,新图宽度为设置值   即width         X坐标则为   sX与spaceX之间较小者   
                //Y方向同理   
                int sX = ResourceImage.Width - width;
                int sY = ResourceImage.Height - height;
                if (sX > 0)
                {
                    x = sX > spaceX ? spaceX : sX;
                }
                else
                {
                    width = ResourceImage.Width;
                }
                if (sY > 0)
                {
                    y = sY > spaceY ? spaceY : sY;
                }
                else
                {
                    height = ResourceImage.Height;
                }

                EncoderParameter p;
                EncoderParameters ps;

                ps = new EncoderParameters(1);
                p = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 85L);
                ps.Param[0] = p;

                Bitmap ReducedImage = new Bitmap(width, height);
                Graphics g = System.Drawing.Graphics.FromImage(ReducedImage);

                // 设置画布的描绘质量
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;

                //清空画布并以透明背景色填充
                g.Clear(Color.Transparent);

                g.DrawImage(ResourceImage, 0, 0, new Rectangle(x, y, width, height), GraphicsUnit.Pixel);

                ImageCodecInfo imgCodecInfo = GetCodecInfo(imgFormat.ToString());

                if (imgCodecInfo == null)
                {
                    ReducedImage.Save(targetFilePath, imgFormat);
                }
                else
                {
                    ReducedImage.Save(targetFilePath, GetCodecInfo(targetFilePath), ps);
                }

                //释放资源   
                //saveImage.Dispose();
                ReducedImage.Dispose();
                g.Dispose();
            }
            catch (Exception ex)
            {
                ErrMessage = ex.Message;
                return false;
            }
            return true;
        }

        public bool MergerImg(string targetFilePath, int ImgWidth, int ImgHeight)
        {
            try
            {
                EncoderParameter p;
                EncoderParameters ps;

                ps = new EncoderParameters(1);
                p = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 85L);
                ps.Param[0] = p;

                //创建要显示的图片对象,根据参数的个数设置宽度
                Bitmap ReducedImage = new Bitmap(ImgWidth, ImgHeight);
                Graphics g = Graphics.FromImage(ReducedImage);

                // 设置画布的描绘质量
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;

                //清空画布并以透明背景色填充
                g.Clear(Color.Transparent);

                int StartX = (ImgWidth - this.Width) / 2;
                int StartY = (ImgHeight - this.Height) / 2;

                g.DrawImage(ResourceImage, StartX, StartY, this.Width, this.Height);

                //保存图象   
                ImageCodecInfo imgCodecInfo = GetCodecInfo(imgFormat.ToString());
                if (imgCodecInfo == null)
                {
                    ReducedImage.Save(targetFilePath, imgFormat);
                }
                else
                {
                    ReducedImage.Save(targetFilePath, GetCodecInfo(targetFilePath), ps);
                }

                //释放资源   
                ReducedImage.Dispose();
                g.Dispose();
            }
            catch (Exception ex)
            {
                ErrMessage = ex.Message;
                return false;
            }
            return true;
        }

        public bool MergerImg(string targetFilePath, System.Drawing.Image image, int ImgWidth, int ImgHeight)
        {
            try
            {
                EncoderParameter p;
                EncoderParameters ps;

                ps = new EncoderParameters(1);
                p = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 85L);
                ps.Param[0] = p;

                //创建要显示的图片对象,根据参数的个数设置宽度
                Bitmap ReducedImage = new Bitmap(ImgWidth, ImgHeight);
                Graphics g = Graphics.FromImage(ReducedImage);

                // 设置画布的描绘质量
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;

                //清空画布并以透明背景色填充
                g.Clear(Color.Transparent);

                int StartX = (ImgWidth - image.Width) / 2;
                int StartY = (ImgHeight - image.Height) / 2;

                g.DrawImage(image, StartX, StartY, image.Width, image.Height);
                //保存图象   
                ImageCodecInfo imgCodecInfo = GetCodecInfo(imgFormat.ToString());

                if (imgCodecInfo == null)
                {
                    ReducedImage.Save(targetFilePath, imgFormat);
                }
                else
                {
                    ReducedImage.Save(targetFilePath, GetCodecInfo(targetFilePath), ps);
                }

                //释放资源   
                ReducedImage.Dispose();
                g.Dispose();
            }
            catch (Exception ex)
            {
                ErrMessage = ex.Message;
                return false;
            }
            return true;
        }

        //返回图片解码器信息用于jpg图片
        private ImageCodecInfo GetCodecInfo(string str)
        {
            string ext = str.Substring(str.LastIndexOf(".") + 1);
            string mimeType = "";
            switch (ext.ToLower())
            {
                case "jpe":
                case "jpg":
                case "jpeg":
                    mimeType = "image/jpeg";
                    break;
                case "bmp":
                    mimeType = "image/bmp";
                    break;
                case "png":
                    mimeType = "image/png";
                    break;
                case "tif":
                case "tiff":
                    mimeType = "image/tiff";
                    break;
                default:
                    mimeType = "image/jpeg";
                    break;
            }
            ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders();
            foreach (ImageCodecInfo ici in CodecInfo)
            {
                if (ici.MimeType == mimeType) return ici;
            }
            return null;
        }

        public void Dispose()
        {
            ResourceImage.Dispose();
        }
}
}