请选择 进入手机版 | 继续访问电脑版

无忧编程_ASP.NET  / C# / PHP 程序员的软件世界

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 323|回复: 0

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

[复制链接]

308

主题

337

帖子

1384

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1384
发表于 2018-3-20 14:47:56 | 显示全部楼层 |阅读模式
C#放缩、截取、合并图片并生成高质量新图的类整理
[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;
        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();
        }
}
}

因为tiff格式的图片,一张可以有多页,所以现在有需求如下:
1. 可以随便添加一张图片(tiff,jpeg,png等格式)的图片到指定的tiff图片中去
2. 可以从tiff图片中抽取出任意张图片出来,可以保存为(tiff,png,jpeg等格式)
[C#] 纯文本查看 复制代码
Image img = Image.FromFile("C:\\1.tif"); [/backcolor]
   Guid guid = (Guid)img.FrameDimensionsList.GetValue(0); 
   FrameDimension dimension = new FrameDimension(guid); 
   int totalPage = img.GetFrameCount(dimension); 

   this.statusBar1.Text = "共"+totalPage+"页"; 

   for(int i=0;i<totalPage;i++) 
   { 
    img.SelectActiveFrame(dimension,i); 
                img.Save("C:\\Gif"+i+".gif",System.Drawing.Imaging.ImageFormat.Gif); 
   } 
windows .net(C#+MSSQL) linux(php+mysql)  
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|软件编程 ( 鄂ICP备11006601号鄂公网安备 42011102001337号 |

GMT+8, 2018-4-27 03:20 , Processed in 0.140582 second(s), 24 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表