码迷,mamicode.com
首页 > Windows程序 > 详细

C#一些图像处理的函数

时间:2015-09-16 12:09:57      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

 1         /// <summary>
 2         /// 图像URL地址转byte数组
 3         /// </summary>
 4         /// <param name="FileUrl">图像URL地址</param>
 5         /// <returns>图像的byte数组</returns>
 6         public static byte[] UrlToByte(string FileUrl)
 7         {
 8             return File.ReadAllBytes(FileUrl);
 9         }
10 
11         /// <summary>
12         /// 图像byte数组转图像
13         /// </summary>
14         /// <param name="ByteOfImg">图像的byte数组</param>
15         /// <returns>图像</returns>
16         public static Image ByteToImage(byte[] ByteOfImg)
17         {
18             MemoryStream ms = new MemoryStream(ByteOfImg);
19             Image image = Image.FromStream(ms);
20             ms.Dispose();
21             return image;
22         }
23 
24         /// <summary>
25         /// 图像转byte数组
26         /// </summary>
27         /// <param name="img">图像</param>
28         /// <returns>图像的byte数组</returns>
29         public static byte[] ImageToByte(Image img)
30         {
31             MemoryStream mstream = new MemoryStream();
32             img.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg);
33             byte[] byData = new Byte[mstream.Length];
34             mstream.Position = 0;
35             mstream.Read(byData, 0, byData.Length);
36             mstream.Close();
37             return byData;
38         } 
39 
40 
41         /// <summary>
42         /// 图像转灰度分量
43         /// </summary>
44         /// <param name="img">图像</param>
45         /// <returns>图像的灰度分量</returns>
46         public static byte[] ImageToY(Image img)
47         {
48             int Var_H = img.Height;
49             int Var_W = img.Width;
50             int tem_r, tem_g, tem_b = 0;
51             int length = 0;
52             Bitmap Var_SaveBmp = (Bitmap)img;
53             byte[] gray = new byte[Var_H * Var_W];
54 
55             for (int j = 0; j < Var_H; j++)
56                 for (int i = 0; i < Var_W; i++)
57                 {
58                     Color tem_color = Var_SaveBmp.GetPixel(i, j);
59                     tem_r = tem_color.R;
60                     tem_g = tem_color.G;
61                     tem_b = tem_color.B;
62                     gray[length++] = (byte)((tem_r * 38 + tem_g * 75 + tem_b * 15) >> 7);      //Gray = (R*38 + G*75 + B*15) >> 7
63                 }
64             return gray;
65         }

 

C#一些图像处理的函数

标签:

原文地址:http://www.cnblogs.com/kim-cst-dlt/p/4812779.html

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