原文: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等格式)

1   Image img = Image.FromFile("C:\\1.tif");
2   Guid guid = (Guid)img.FrameDimensionsList.GetValue(0);
3   FrameDimension dimension = new FrameDimension(guid);
4   int totalPage = img.GetFrameCount(dimension);
5
6   this.statusBar1.Text = "共"+totalPage+"页";
7
8   for(int i=0;i<totalPage;i++)
9   {
10    img.SelectActiveFrame(dimension,i);
11                img.Save("C:\\Gif"+i+".gif",System.Drawing.Imaging.ImageFormat.Gif);
12   }
13

C#放缩、截取、合并图片并生成高质量新图的类的更多相关文章

  1. atitit.thumb生成高质量缩略图 php .net c++ java

    atitit.java thumb生成高质量缩略图 php .net c++ 1. 图像缩放(image scaling)---平滑度(smoothness)和清晰度(sharpness) 1 2.  ...

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

    /// <summary> /// 高质量缩放图片 /// </summary> /// <param name="OriginFilePath"&g ...

  3. Highcharts结合PhantomJS在服务端生成高质量的图表图片

    项目背景 最近忙着给部门开发一套交互式的报表系统,来替换原有的静态报表系统. 老系统是基于dotnetCHARTING开发的,dotnetCHARTING的优势是图表类型丰富,接口调用简单,使用时只需 ...

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

    http://pan.baidu.com/s/1eQm5a74 下载FFmpeg palettegen paletteuse documentation 需要下载 FFmpeg2.6 以上 并FFmp ...

  5. <1>Python生成高质量Html文件:Pyh模块+Bootstrap框架

    一,介绍 QQ交流群:585499566 本文的目的是怎么使用Pyh+Bootstrap快速生成简约,大方,清新的Html页面,涉及到的技能:Python小白技能,Pyh会阅读中文文档,Html基础, ...

  6. ASP.NET 画图与图像处理-生成高质量缩略图

    http://www.cftea.com/c/2007/08/SG9WFLZJD62Z2D0O.asp

  7. [golang]按图片中心旋转后的新图左顶点和原图左顶点的偏移量计算

    1 前言 略,作为记录使用 2 代码 /** * @Author: FB * @Description: * @File: RotateSample.go * @Version: 1.0.0 * @D ...

  8. PHPThumb处理图片,生成缩略图,图片尺寸调整,图片截取,图片加水印,图片旋转

    [强烈推荐]下载地址(github.com/masterexploder/PHPThumb). 注意这个类库有一个重名的叫phpThumb,只是大小写的差别,所以查找文档的时候千万注意. 在网站建设过 ...

  9. C#一些常用的图片操作方法:生成文字图片 合并图片等

    生成文字图片: /// <summary> /// 生成文字图片 /// </summary> /// <param name="text">& ...

随机推荐

  1. 一个人ACM(我们赶上了ACM)

    时间过得真快,不经意间我已经花了两年的大学生活,现在是时候写的东西.纪念馆两年左右的时间,最近一直在玩博客.我写了一个博客.纪念我们终将逝去的青春. 就从报考说起吧.高考成绩一般,自己选择了土建类的学 ...

  2. Codeforces Round #274 (Div. 2) B. Towers

    As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting o ...

  3. C++教程之lambda表达式一

    什么是Lambda? C++ 11增加了一个很重要的特性--Lambda表达式.营里(戴维营)的兄弟都对Objective-C很熟悉,很多人多block情有独钟,将各种回调函数.代理通通都用它来实现. ...

  4. Linux内核分析(四)----进程管理|网络子系统|虚拟文件系统|驱动简介

    原文:Linux内核分析(四)----进程管理|网络子系统|虚拟文件系统|驱动简介 Linux内核分析(四) 两天没有更新了,上次博文我们分析了linux的内存管理子系统,本来我不想对接下来的进程管理 ...

  5. (2)虚拟机下hadoop1.1.2集群环境搭建

    hadoop集群环境的搭建和单机版的搭建差点儿相同,就是多了一些文件的配置操作. 一.3台主机的hostname改动和IP地址绑定 注意:以下的操作我都是使用root权限进行! (1)3太主机的基本网 ...

  6. Angular绑定数据时转义html标签

    AngularJs在绑定数据时默认会以文本的形式出现在页面上,比如我现在有这样一段代码 <div ng-controller="testCtrl">{{data}}&l ...

  7. TortoiseGit push失败原因小结(转)

    花了我一个晚上,终于弄明白为什么总是 push 失败的原因了!竟然是因为我用的是注册的用户名而不是邮箱名……囧死. 另外搞清楚了一个问题,就是 Git 和远程仓库交互有两种方式,即 https 方式和 ...

  8. CSDN博文“待定”如何避免检测规则分析“待定”

    这些天一直很郁闷.鲍文本人一直"待定", 当然,这是非常不舒服的自己.那么今晚最终成为一个非成功出版"待定"文章,这CSDN于大家的反映而放弃了.没想到我后面又 ...

  9. FPGA 设计流程,延迟,时间

    FPGA 设计流程,延迟,时间 流程:每个时钟周期可以传输的数据比特. 延迟:从输入到时钟周期的输出数据需要经验. 时间:两个元件之间的最大延迟,最高时钟速度. 1 採用流水线能够提高 流量: 比如计 ...

  10. C#操作Xml:linq to xml操作XML

    LINQ to XML提供了更方便的读写xml方式.前几篇文章的评论中总有朋友提,你为啥不用linq to xml?现在到时候了,linq to xml出场了. .Net中的System.Xml.Li ...