二进制图片格式转换

View Code
 1  public static bool IsAllowedExtension(byte[] imgArray, FileExtension[] fileEx)
 2         {
 3             MemoryStream ms = new MemoryStream(imgArray);
 4             System.IO.BinaryReader br = new System.IO.BinaryReader(ms);
 5             string fileclass = "";
 6             byte buffer;
 7             try
 8             {
 9                 buffer = br.ReadByte();
10                 fileclass = buffer.ToString();
11                 buffer = br.ReadByte();
12                 fileclass += buffer.ToString();
13             }
14             catch
15             {
16             }
17             br.Close();
18             ms.Close();
19             foreach (FileExtension fe in fileEx)
20             {
21                 if (Int32.Parse(fileclass) == (int)fe)
22                     return true;
23             }
24             return false;
25         }
26         public  enum FileExtension
27         {
28             JPG = 255216,
29             GIF = 7173,
30             PNG = 13780,
31             SWF = 6787,
32             RAR = 8297,
33             ZIP = 8075,
34             _7Z = 55122,
35             XLS = 208207,
36             XLSX = 8075,
37             bmp = 6677
38         }
39 
40 调用
41  //判断下是否为gif
42                         if (IsAllowedExtension(buffer, new FileExtension[] { FileExtension.GIF,FileExtension.PNG,FileExtension.bmp }))
43                         {
44                             using (MemoryStream newstream = new MemoryStream())
45                             {
46                                 bitmap.Save(newstream, System.Drawing.Imaging.ImageFormat.Jpeg);
47                                 //
48                                 using (Bitmap newbitmap = new Bitmap(newstream, true))
49                                 {
50 }
51 }
52 }

posted on 2012-08-30 13:45  hardy_Wang  阅读(378)  评论(0编辑  收藏  举报

导航