C# .net 对图片操作

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
public class ImageHelper
{
/// <summary>
    /// 获取图片中的各帧
    /// </summary>
    /// <param name="pPath">图片路径</param>
    /// <param name="pSavePath">保存路径</param>
    public void GetFrames(string pPath, string pSavedPath)
{
Image gif = Image.FromFile(pPath);
FrameDimension fd = new FrameDimension(gif.FrameDimensionsList[0]);
//获取帧数(gif图片可能包含多帧,其它格式图片一般仅一帧)
        int count = gif.GetFrameCount(fd);
//以Jpeg格式保存各帧
        for (int i = 0; i < count; i++)
{
gif.SelectActiveFrame(fd, i);
gif.Save(pSavedPath + "//frame_" + i + ".jpg", ImageFormat.Jpeg);
}
}
/// <summary>
    /// 获取图片缩略图
    /// </summary>
    /// <param name="pPath">图片路径</param>
    /// <param name="pSavePath">保存路径</param>
    /// <param name="pWidth">缩略图宽度</param>
    /// <param name="pHeight">缩略图高度</param>
    /// <param name="pFormat">保存格式,通常可以是jpeg</param>
    public void GetSmaller(string pPath, string pSavedPath, int pWidth, int pHeight)
{
try
{
Image smallerImg;
Image originalImg = Image.FromFile(pPath);
Image.GetThumbnailImageAbort callback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
smallerImg = originalImg.GetThumbnailImage(pWidth, pHeight, callback, IntPtr.Zero);
smallerImg.Save(pSavedPath + "//smaller.jpg", ImageFormat.Jpeg);
}
catch (Exception x)
{
//
        }
}
/// <summary>
    /// 获取图片指定部分
    /// </summary>
    /// <param name="pPath">图片路径</param>
    /// <param name="pSavePath">保存路径</param>
    /// <param name="pPartStartPointX">目标图片开始绘制处的坐标X值(通常为)</param>
    /// <param name="pPartStartPointY">目标图片开始绘制处的坐标Y值(通常为)</param>
    /// <param name="pPartWidth">目标图片的宽度</param>
    /// <param name="pPartHeight">目标图片的高度</param>
    /// <param name="pOrigStartPointX">原始图片开始截取处的坐标X值</param>
    /// <param name="pOrigStartPointY">原始图片开始截取处的坐标Y值</param>
    /// <param name="pFormat">保存格式,通常可以是jpeg</param>
    public void GetPart(string pPath, string pSavedPath, int pPartStartPointX, int pPartStartPointY, int pPartWidth, int pPartHeight, int pOrigStartPointX, int pOrigStartPointY)
{
Image originalImg = Image.FromFile(pPath);
Bitmap partImg = new Bitmap(pPartWidth, pPartHeight);
Graphics graphics = Graphics.FromImage(partImg);
Rectangle destRect = new Rectangle(new Point(pPartStartPointX, pPartStartPointY), new Size(pPartWidth, pPartHeight));//目标位置
        Rectangle origRect = new Rectangle(new Point(pOrigStartPointX, pOrigStartPointY), new Size(pPartWidth, pPartHeight));//原图位置(默认从原图中截取的图片大小等于目标图片的大小)

graphics.DrawImage(originalImg, destRect, origRect, GraphicsUnit.Pixel);
partImg.Save(pSavedPath + "//part.jpg", ImageFormat.Jpeg);
}
public bool ThumbnailCallback()
{
return false;
}
}

 //********************************************************************************************C#对图片的几种简单处理

#region 生成缩略图
        public void MakeSmallImg(string filePath, string saveImg)
        {
            //从文件取得图片对象
            System.Drawing.Image image = System.Drawing.Image.FromFile(filePath, true);

           

            //取得图片大小
            System.Drawing.Size size = new System.Drawing.Size((int)image.Width, (int)image.Height);
            //新建一个bmp图片
            System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width, size.Height);
            //新建一个画板
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
            //设置高质量插值法
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default;
            //设置高质量,低速度呈现平滑程度
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
            //清空一下画布
            g.Clear(System.Drawing.Color.White);
            //在指定位置画图
            g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
                new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
                System.Drawing.GraphicsUnit.Pixel);


            ///文字水印
            //System.Drawing.Graphics G=System.Drawing.Graphics.FromImage(bitmap);
            //System.Drawing.Font f=new Font("宋体",10);
            //System.Drawing.Brush b=new SolidBrush(Color.Black);
            //G.DrawString("myohmine",f,b,10,10);
            //G.Dispose();

 

            ///图片水印
            //System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("pic/1.gif"));
            //Graphics a = Graphics.FromImage(bitmap);
            //a.DrawImage(copyImage, new Rectangle(bitmap.Width-copyImage.Width,bitmap.Height-copyImage.Height,copyImage.Width, copyImage.Height),0,0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);

            //copyImage.Dispose();
            //a.Dispose();
            //copyImage.Dispose();


            //保存高清晰度的缩略图
            //   bitmap.Save(strGoodFile, System.Drawing.Imaging.ImageFormat.Jpeg);
            //   加个a表示是缩略图
            bitmap.Save(saveImg, System.Drawing.Imaging.ImageFormat.Jpeg);
            g.Dispose();
            image.Dispose();
            bitmap.Dispose();
        }
        #endregion

//****************************************************************************************关于图片处理方法(C#)

处理图片时常用的过程是:读入图片文件并转化为Bitmap -> 处理此Bitmap的每个点以得到需要的效果 -> 保存新的Bitmap到文件
使用C#很方便的就可以把多种格式的图片文件读到Bitmap对象中。一句话就够了,常见的格式都支持,诸如JPEG,BMP,PNG等等。

Bitmap bmp = new Bitmap("文件名");

然后就是怎么处理这个图片的问题了,与本案无关,pass。

最后就是保存。JPEG虽然是有损压缩方案,但是它在缩减文件体积和尽可能好的保留原有信息的矛盾上很好的找到了平衡点,所以在很多情况下成为首选的保存方案。

C#当然不会无视这一点,Bitmap类提供了默认的另存为JPEG的方法:

bmp.Save("输出文件", System.Drawing.Imaging.ImageFormat.Jpeg);

这样当然很方便,但有时候更在乎文件体积而有时候更在乎图像质量,是不是有什么办法可以让自己来控制压缩质量呢?

答案是肯定的,bmp.Save方法中有个重载用到了EncoderParameters参数。我们可以在这个参数中加入自己的控制质量。


        
/// <summary>
        
/// 保存JPG时用
        
/// </summary>
        
/// <param name="mimeType"></param>
        
/// <returns>得到指定mimeType的ImageCodecInfo</returns>

        private static ImageCodecInfo GetCodecInfo(string mimeType)
        
{
            ImageCodecInfo[] CodecInfo 
= ImageCodecInfo.GetImageEncoders();
            
foreach (ImageCodecInfo ici in CodecInfo)
            
{
                
if (ici.MimeType == mimeType) return ici;
            }

            
return null;
        }



        
/// <summary>
        
/// 保存为JPEG格式,支持压缩质量选项
        
/// </summary>
        
/// <param name="bmp"></param>
        
/// <param name="FileName"></param>
        
/// <param name="Qty"></param>
        
/// <returns></returns>

        public static bool KiSaveAsJPEG(Bitmap bmp, string FileName, int Qty)
        
{
            
try
            
{
                EncoderParameter p;
                EncoderParameters ps;

                ps 
= new EncoderParameters(1);

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

                bmp.Save(FileName, GetCodecInfo(
"image/jpeg"), ps);
                
                
return true;
            }

            
catch
            
{
                
return false;
            }


        }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/389308.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

数据类型之Integer与int

数据类型之Integer与int Java入门 基本数据类型 众所周知&#xff0c;Java是面向对象的语言&#xff0c;一切皆对象。但是为了兼容人类根深蒂固的数据处理习惯&#xff0c;加快常规数据的处理速度&#xff0c;提供了9种基本数据类型&#xff0c;他们都不具备对象的特性&#xf…

PCA(主成分分析)思想及实现

PCA的概念&#xff1a; PCA是用来实现特征提取的。 特征提取的主要目的是为了排除信息量小的特征&#xff0c;减少计算量等。 简单来说&#xff1a; 当数据含有多个特征的时候&#xff0c;选取主要的特征&#xff0c;排除次要特征或者不重要的特征。 比如说&#xff1a;我们要…

【安富莱二代示波器教程】第8章 示波器设计—测量功能

第8章 示波器设计—测量功能 二代示波器测量功能实现比较简单&#xff0c;使用2D函数绘制即可。不过也专门开辟一个章节&#xff0c;为大家做一个简单的说明&#xff0c;方便理解。 8.1 水平测量功能 8.2 垂直测量功能 8.3 总结 8.1 水平测量功能 水平测量方…

深度学习数据更换背景_开始学习数据科学的最佳方法是了解其背景

深度学习数据更换背景数据科学教育 (DATA SCIENCE EDUCATION) 目录 (Table of Contents) The Importance of Context Knowledge 情境知识的重要性 (Optional) Research Supporting Context-Based Learning (可选)研究支持基于上下文的学习 The Context of Data Science 数据科学…

熊猫数据集_用熊猫掌握数据聚合

熊猫数据集Data aggregation is the process of gathering data and expressing it in a summary form. This typically corresponds to summary statistics for numerical and categorical variables in a data set. In this post we will discuss how to aggregate data usin…

IOS CALayer的属性和使用

一、CALayer的常用属性 1、propertyCGPoint position; 图层中心点的位置&#xff0c;类似与UIView的center&#xff1b;用来设置CALayer在父层中的位置&#xff1b;以父层的左上角为原点&#xff08;0&#xff0c;0&#xff09;&#xff1b; 2、 property CGPoint anchorPoint…

GridView详解

快速预览&#xff1a;GridView无代码分页排序GridView选中&#xff0c;编辑&#xff0c;取消&#xff0c;删除GridView正反双向排序GridView和下拉菜单DropDownList结合GridView和CheckBox结合鼠标移到GridView某一行时改变该行的背景色方法一鼠标移到GridView某一行时改变该行…

访问模型参数,初始化模型参数,共享模型参数方法

一. 访问模型参数 对于使用Sequential类构造的神经网络&#xff0c;我们可以通过方括号[]来访问网络的任一层。回忆一下上一节中提到的Sequential类与Block类的继承关系。 对于Sequential实例中含模型参数的层&#xff0c;我们可以通过Block类的params属性来访问该层包含的所有…

QZEZ第一届“饭吉圆”杯程序设计竞赛

终于到了饭吉圆杯的开赛&#xff0c;这是EZ我参与的历史上第一场ACM赛制的题目然而没有罚时 不过题目很好&#xff0c;举办地也很成功&#xff0c;为法老点赞&#xff01;&#xff01;&#xff01; 这次和翰爷&#xff0c;吴骏达 dalao&#xff0c;陈乐扬dalao组的队&#xff0…

谈谈数据分析 caoz_让我们谈谈开放数据…

谈谈数据分析 caozAccording to the International Open Data Charter(1), it defines open data as those digital data that are made available with the technical and legal characteristics necessary so that they can be freely used, reused and redistributed by any…

数据创造价值_展示数据并创造价值

数据创造价值To create the maximum value, urgency, and leverage in a data partnership, you must present the data available for sale or partnership in a clear and comprehensive way. Partnerships are based upon the concept that you are offering value for valu…

Java入门系列-22-IO流

File类的使用 Java程序如何访问文件&#xff1f;通过 java.io.File 类 使用File类需要先创建文件对象 File filenew File(String pathname);&#xff0c;创建时在构造函数中指定物理文件或目录&#xff0c;然后通过文件对象的方法操作文件或目录的属性。 \ 是特殊字符&#xff…

缺了一部分

学Java好多年&#xff0c;也参与一次完整项目&#xff0c;觉得让自己写项目写不出来&#xff0c;总觉得缺了一部分。 在这方面愚笨&#xff0c;不知道缺在哪里。以前觉得是知识不够牢固&#xff0c;于是重复去学&#xff0c;发现就那些东西。如果没有业务来熟悉的话&#xff0c…

卷积神经网络——各种网络的简洁介绍和实现

各种网络模型&#xff1a;来源《动手学深度学习》 一&#xff0c;卷积神经网络&#xff08;LeNet&#xff09; LeNet分为卷积层块和全连接层块两个部分。下面我们分别介绍这两个模块。 卷积层块里的基本单位是卷积层后接最大池化层&#xff1a;卷积层用来识别图像里的空间模…

数据中台是下一代大数据_全栈数据科学:下一代数据科学家群体

数据中台是下一代大数据重点 (Top highlight)Data science has been an eye-catching field for many years now to young individuals having formal education with a bachelors, masters or Ph.D. in computer science, statistics, business analytics, engineering manage…

net如何判断浏览器的类别

回复&#xff1a;.net如何判断浏览器的类别?浏览器型号&#xff1a;Request.Browser.Type 浏览器名称&#xff1a;Request.Browser.browser 浏览器版本&#xff1a;Request.Browser.Version 浏览器Cookie&#xff1a;Request.Browser.Cookies 你的操作系统&#xff1a;Request…

AVS 端能力模块

Mark 转载于:https://www.cnblogs.com/clxye/p/9939333.html

pwn学习之四

本来以为应该能出一两道ctf的pwn了&#xff0c;结果又被sctf打击了一波。 bufoverflow_a 做这题时libc和堆地址都泄露完成了&#xff0c;卡在了unsorted bin attack上&#xff0c;由于delete会清0变量导致无法写&#xff0c;一直没构造出unsorted bin attack&#xff0c;后面根…

优化算法的简洁实现

动量法 思想&#xff1a; 动量法使用了指数加权移动平均的思想。它将过去时间步的梯度做了加权平均&#xff0c;且权重按时间步指数衰减。 代码&#xff1a; 在Gluon中&#xff0c;只需要在Trainer实例中通过momentum来指定动量超参数即可使用动量法。 d2l.train_gluon_ch7…

北方工业大学gpa计算_北方大学联盟仓库的探索性分析

北方工业大学gpa计算This is my firts publication here and i will start simple.这是我的第一篇出版物&#xff0c;这里我将简单介绍 。 I want to make an exploratory data analysis of UFRN’s warehouse and answer some questions about the data using Python and Pow…