码迷,mamicode.com
首页 > 其他好文 > 详细

图片分割合并处理

时间:2015-03-04 16:13:13      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

代码展示:

技术分享
  1 /// <summary>
  2     /// 画图类
  3     /// </summary>
  4     public class Draw
  5     {
  6         /// <summary>
  7         /// 方向
  8         /// </summary>
  9         public enum Align
 10         {
 11             /// <summary>
 12             /// 垂直方向
 13             /// </summary>
 14             Vertical,
 15             /// <summary>
 16             /// 水平方向
 17             /// </summary>
 18             Horizontal,
 19         }
 20         /// <summary>
 21         /// 通过URL获取图片
 22         /// </summary>
 23         /// <param name="url">图片的URL地址</param>
 24         /// <returns></returns>
 25         public Bitmap GetBmpFromUrl(string url)
 26         {
 27             if (url == "")
 28             {
 29                 return null;
 30             }
 31             WebClient wc = new WebClient();
 32             Stream s = wc.OpenRead(url);
 33             Image img = Image.FromStream(s);
 34             Bitmap bmp = new Bitmap(img);
 35             return bmp;
 36         }
 37         /// <summary>
 38         /// 把一组图片合成一张图片,图片规格以第一张为准
 39         /// </summary>
 40         /// <param name="bmplist">等待合并的图片数组</param>
 41         /// <param name="align">图片排列方向</param>
 42         /// <returns></returns>
 43         public Bitmap ComposeImage(Bitmap[] bmplist, Align align, params Size[] size)
 44         {
 45             int width = 0;
 46             if (align == Align.Horizontal)
 47             {
 48                 foreach (Bitmap b in bmplist)
 49                 {
 50                     width += b.Width;
 51                 }
 52                 Bitmap BMP = new Bitmap(width, bmplist[0].Height);
 53                 Graphics g = Graphics.FromImage(BMP);
 54                 int x = 0;
 55                 for (int i = 0; i < bmplist.Length; i++)
 56                 {
 57                     int b = bmplist[i].Height / bmplist[0].Height;
 58                     Bitmap bm = new Bitmap(bmplist[i].Width / b, bmplist[0].Height);
 59                     Graphics gh = Graphics.FromImage(bm);
 60                     gh.InterpolationMode = InterpolationMode.HighQualityBicubic;
 61                     gh.DrawImage(bmplist[i], new Rectangle(0, 0, bmplist[i].Width / b, bmplist[0].Height), new Rectangle(0, 0, bmplist[i].Width, bmplist[i].Height), GraphicsUnit.Pixel);
 62                     g.DrawImage(bm, x, 0);
 63                     x += bm.Width;
 64                 }
 65                 return BMP;
 66             }
 67             else
 68             {
 69                 foreach (Bitmap b in bmplist)
 70                 {
 71                     width += b.Height;
 72                 }
 73                 Bitmap BMP = new Bitmap(bmplist[0].Height, width);
 74                 Graphics g = Graphics.FromImage(BMP);
 75                 int y = 0;
 76                 for (int i = 0; i < bmplist.Length; i++)
 77                 {
 78                     int b = bmplist[i].Width / bmplist[0].Width;
 79                     Bitmap bm = new Bitmap(bmplist[0].Width, bmplist[i].Height / b);
 80                     Graphics gh = Graphics.FromImage(bm);
 81                     gh.InterpolationMode = InterpolationMode.HighQualityBicubic;
 82                     gh.DrawImage(bmplist[i], new Rectangle(0, 0, bmplist[0].Width, bmplist[i].Height / b), new Rectangle(0, 0, bmplist[i].Width, bmplist[i].Height), GraphicsUnit.Pixel);
 83                     g.DrawImage(bm, 0, y);
 84                     y += bm.Height;
 85                 }
 86                 return BMP;
 87             }
 88         }
 89         /// <summary>
 90         /// 平均分割图片
 91         /// </summary>
 92         /// <param name="bmp">等待分割的图片</param>
 93         /// <param name="shares">需要分割的数量</param>
 94         /// <param name="align">分割方向</param>
 95         /// <returns>分割后的图片数组</returns>
 96         public Bitmap[] SplitImage(Bitmap bmp, int shares, Align align)
 97         {
 98             int Width = bmp.Width;
 99             int Height = bmp.Height;
100             int AverageWidth = 0;
101             int AverageHeight = 0;
102             Bitmap[] list = new Bitmap[shares];
103             switch (align)
104             { 
105                 case Align.Horizontal:
106                     AverageHeight = Height / shares;
107                     for (int i = 0; i < shares; i++)
108                     {
109                         Bitmap bm = new Bitmap(Width, AverageHeight);
110                         Graphics gh = Graphics.FromImage(bm);
111                         gh.InterpolationMode = InterpolationMode.HighQualityBicubic;
112                         gh.DrawImage(bmp, new Rectangle(0, 0, bm.Width, bm.Height), new Rectangle(0, bm.Height * i, bmp.Width, bm.Height), GraphicsUnit.Pixel);
113                         list[i] = bm;
114                     }
115                     break;
116                 case Align.Vertical:
117                     AverageWidth = Width / shares;
118                     for (int i = 0; i < shares; i++)
119                     {
120                         Bitmap bm = new Bitmap(AverageWidth, bmp.Height);
121                         Graphics gh = Graphics.FromImage(bm);
122                         gh.InterpolationMode = InterpolationMode.HighQualityBicubic;
123                         gh.DrawImage(bmp, new Rectangle(0, 0, bm.Width, bm.Height), new Rectangle(bm.Width * i, 0, bm.Width, bmp.Height), GraphicsUnit.Pixel);
124                         list[i] = bm;
125                     }
126                     break;
127             }
128             return list;
129         }
130         /// <summary>
131         /// 交叉分割图片
132         /// </summary>
133         /// <param name="bmp">等待分割的图片</param>
134         /// <param name="rows">分割的行数</param>
135         /// <param name="cols">分割的列数</param>
136         /// <returns>分割后的图片数组</returns>
137         public Bitmap[] SplitImageEx(Bitmap bmp, int rows, int cols)
138         {
139             int Width = bmp.Width;
140             int Height = bmp.Height;
141             int AverageWidth = 0;
142             int AverageHeight = 0;
143             int Index = 0;
144             Bitmap[] list = new Bitmap[rows * cols];
145             AverageHeight = Height / rows;
146             AverageWidth = Width / cols;
147             for (int i = 0; i < rows; i++)
148             {
149                 Bitmap bm = new Bitmap(bmp.Width, AverageHeight);
150                 Graphics gh = Graphics.FromImage(bm);
151                 gh.InterpolationMode = InterpolationMode.HighQualityBicubic;
152                 gh.DrawImage(bmp, new Rectangle(0, 0, bm.Width, bm.Height), new Rectangle(0, bm.Height * i, bmp.Width, bm.Height), GraphicsUnit.Pixel);
153                 
154                 for (int j = 0; j < cols; j++)
155                 {
156                     Bitmap bm1 = new Bitmap(AverageWidth, AverageHeight);
157                     Graphics gh1 = Graphics.FromImage(bm1);
158                     gh1.InterpolationMode = InterpolationMode.HighQualityBicubic;
159                     gh1.DrawImage(bm, new Rectangle(0, 0, AverageWidth, AverageHeight), new Rectangle(bm1.Width * j, 0, AverageWidth, AverageHeight), GraphicsUnit.Pixel);
160                     list[Index] = bm1;
161                     Index++;
162                 }
163             }
164             return list;
165         }
166         /// <summary>
167         /// 从指定图片指定位置截取指定大小的图片
168         /// </summary>
169         /// <param name="bmp">等待截取的图片</param>
170         /// <param name="rect">指定的截取位置及大小</param>
171         /// <returns>截取后的图片</returns>
172         public Bitmap Screenshot(Bitmap bmp, Rectangle rect)
173         {
174             Bitmap bm = new Bitmap(rect.Width, rect.Height);
175             Graphics gh = Graphics.FromImage(bm);
176             gh.InterpolationMode = InterpolationMode.HighQualityBicubic;
177             gh.DrawImage(bmp, new Rectangle(0, 0, rect.Width, rect.Height), rect, GraphicsUnit.Pixel);
178             return bm;
179         }
180     }
View Code

 

图片分割合并处理

标签:

原文地址:http://www.cnblogs.com/Leander723/p/4313458.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!