zoukankan      html  css  js  c++  java
  • C#图片处理---基础

    简单的图片剪裁

      1 using System;
      2 using System.Drawing;
      3 using System.Drawing.Drawing2D;
      4 using System.IO;
      5 
      6 namespace ClassLibrary
      7 {
      8     /// <summary>
      9     /// 图片处理
     10     /// </summary>
     11     public static class Img
     12     {
     13         #region 图片裁剪
     14         #region 通过文件路径图片 public static Image ImgCutFromFile
     15         /// <summary>
     16         /// 通过文件路径裁剪图片
     17         /// </summary>
     18         /// <param name="imgFile">文件路径</param>
     19         /// <param name="startX">图片左上角X坐标</param>
     20         /// <param name="startY">图片左上角Y坐标</param>
     21         /// <param name="width">截取宽度</param>
     22         /// <param name="height">截取高度</param>
     23         /// <param name="widthS">图片显示宽度</param>
     24         /// <param name="heightS">图片显示高度</param>
     25         /// <returns></returns>
     26         public static Image ImgCutFromFile(string imgFile, int startX, int startY, int width, int height, int widthS = 0, int heightS=0)
     27         {
     28             
     29             Image img = Image.FromFile(imgFile);
     30             if (widthS > 0 && heightS > 0)
     31             {
     32                 GetXYWH(ref startX, ref startY, ref width, ref height, widthS, heightS, img);
     33             }
     34             return ImgCut(img, startX, startY, width, height);
     35         }
     36         #endregion
     37 
     38         #region 通过流图片 public static Image ImgCutFromStream
     39         /// <summary>
     40         /// 通过流图片
     41         /// </summary>
     42         /// <param name="stream"></param>
     43         /// <param name="startX">图片左上角X坐标</param>
     44         /// <param name="startY">图片左上角Y坐标</param>
     45         /// <param name="width">截取宽度</param>
     46         /// <param name="height">截取高度</param>
     47         /// <param name="widthS">图片显示宽度</param>
     48         /// <param name="heightS">图片显示高度</param>
     49         /// <returns></returns>
     50         public static Image ImgCutFromStream(Stream stream, int startX, int startY, int width, int height, int widthS = 0, int heightS = 0)
     51         {
     52             Image img = Image.FromStream(stream);
     53             if (widthS > 0 && heightS > 0)
     54             {
     55                 GetXYWH(ref startX, ref startY, ref width, ref height, widthS, heightS, img);
     56             }
     57             return ImgCut(img, startX, startY, width, height);
     58         }
     59         #endregion
     60 
     61         #region 获取实际XYWH private static void GetXYWH
     62         /// <summary>
     63         /// 获取实际XYWH
     64         /// </summary>
     65         /// <param name="startX">图片左上角X坐标</param>
     66         /// <param name="startY">图片左上角Y坐标</param>
     67         /// <param name="width">截取宽度</param>
     68         /// <param name="height">截取高度</param>
     69         /// <param name="widthS">图片显示宽度</param>
     70         /// <param name="heightS">图片显示高度</param>
     71         /// <param name="img"></param>
     72         private static void GetXYWH(ref int startX, ref int startY, ref int width, ref int height, int widthS, int heightS, Image img)
     73         {
     74             /*img.Width / widthS 是实际图片大小与页面显示图片大小比值*/
     75             startX = Convert.ToInt32(startX * img.Width / widthS);
     76             startY = Convert.ToInt32(startY * img.Height / heightS);
     77             width = Convert.ToInt32(width * img.Width / widthS);
     78             height = Convert.ToInt32(height * img.Height / heightS);
     79         }
     80         #endregion
     81 
     82         #region 执行剪裁 public static Image ImgCut
     83         /// <summary>
     84         /// 图片裁剪
     85         /// </summary>
     86         /// <param name="image">图片</param>
     87         /// <param name="startX">图片左上角X坐标</param>
     88         /// <param name="startY">图片左上角Y坐标</param>
     89         /// <param name="width">截取宽度</param>
     90         /// <param name="height">截取高度</param>
     91         /// <returns></returns>
     92         public static Image ImgCut(Image image, int startX, int startY, int width, int height)
     93         {
     94             Image img = image;//获取原始图片
     95             Image newImg = new Bitmap(width, height);//裁剪后的图片
     96             Graphics g = Graphics.FromImage(newImg);
     97             /*设置质量*/
     98             g.InterpolationMode = InterpolationMode.HighQualityBicubic;
     99             g.SmoothingMode = SmoothingMode.HighQuality;
    100             /*图片源位置,图片源大小*/
    101             Rectangle fromR = new Rectangle(startX, startY, width, height);
    102             /*绘图位置绘图大小*/
    103             Rectangle toR = new Rectangle(0, 0, width, height);
    104             /*画图*/
    105             g.DrawImage(img, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
    106             /*释放截图资源*/
    107             g.Dispose();
    108             return newImg;
    109             //newImg.Save(Server.MapPath(""));
    110             //newImg.Dispose();
    111         }
    112         #endregion
    113         #endregion
    114 
    115         #region 改变图片大小 public static Image ImgSize
    116         /// <summary>
    117         /// 改变图片大小
    118         /// </summary>
    119         /// <param name="image">图片</param>
    120         /// <param name="width">新宽度</param>
    121         /// <param name="height">新高度</param>
    122         /// <returns></returns>
    123         public static Image ImgSize(Image image, int width = 0, int height = 0)
    124         {
    125             Image img = image;//获取原始图片
    126             Image newImg = new Bitmap(width, height);//裁剪后的图片
    127             Graphics g = Graphics.FromImage(newImg);
    128             /*设置质量*/
    129             g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    130             g.SmoothingMode = SmoothingMode.HighQuality;
    131             /*图片源位置,图片源大小*/
    132             Rectangle fromR = new Rectangle(0, 0, img.Width, img.Height);
    133             /*绘图位置绘图大小*/
    134             if (width == 0 && height == 0) {
    135                 width = img.Width;
    136                 height = img.Height;
    137             }
    138             Rectangle toR = new Rectangle(0, 0, width, height);
    139             /*画图*/
    140             g.DrawImage(img, toR, fromR, System.Drawing.GraphicsUnit.Pixel);
    141             /*释放截图资源*/
    142             g.Dispose();
    143             return newImg;
    144         }
    145         #endregion
    146 
    147     }
    148 }
    149 
    150 Img
    View Code
  • 相关阅读:
    Linux Mint---shutter截图软件
    Linux Mint---fcitx中文,日语输入法
    Linux Mint---安装docky
    Linux Mint---开启桌面三维特效
    Linux Mint---ATI显卡驱动安装篇
    Linux Mint---更新软件源
    Spring Cloud 微服务服务间调用session共享问题
    Jooq批量插入 batch
    idea安装SonarLint语法检测插件
    JVM到底是什么?
  • 原文地址:https://www.cnblogs.com/liuph/p/4323883.html
Copyright © 2011-2022 走看看