• c# Bitmap byte[] Stream 文件相互转换


    1. //byte[] 转图片  
    2. publicstatic Bitmap BytesToBitmap(byte[] Bytes) 
    3.         { 
    4.             MemoryStream stream = null
    5.             try 
    6.             { 
    7.                 stream = new MemoryStream(Bytes); 
    8.                 returnnew Bitmap((Image)new Bitmap(stream)); 
    9.             } 
    10.             catch (ArgumentNullException ex) 
    11.             { 
    12.                 throw ex; 
    13.             } 
    14.             catch (ArgumentException ex) 
    15.             { 
    16.                 throw ex; 
    17.             } 
    18.             finally 
    19.             { 
    20.                 stream.Close(); 
    21.             } 
    22.         }  
    23.  
    24. //图片转byte[]   
    25.         publicstaticbyte[] BitmapToBytes(Bitmap Bitmap) 
    26.         { 
    27.             MemoryStream ms = null
    28.             try 
    29.             { 
    30.                 ms = new MemoryStream(); 
    31.                 Bitmap.Save(ms, Bitmap.RawFormat); 
    32.                 byte[] byteImage = new Byte[ms.Length]; 
    33.                 byteImage = ms.ToArray(); 
    34.                 return byteImage; 
    35.             } 
    36.             catch (ArgumentNullException ex) 
    37.             { 
    38.                 throw ex; 
    39.             } 
    40.             finally 
    41.             { 
    42.                 ms.Close(); 
    43.             } 
    44.         } 
    45.     } 
    46.  
    47. ===================== 
    48.  
    49. * Stream 和 byte[] 之间的转换 
    50. * - - - - - - - - - - - - - - - - - - - - - - - */
    51. /// <summary>  
    52. /// 将 Stream 转成 byte[]  
    53. /// </summary>  
    54. publicbyte[] StreamToBytes(Stream stream) 
    55.     byte[] bytes = newbyte[stream.Length]; 
    56.     stream.Read(bytes, 0, bytes.Length); 
    57.  
    58.     // 设置当前流的位置为流的开始  
    59.     stream.Seek(0, SeekOrigin.Begin); 
    60.     return bytes; 
    61.  
    62. /// <summary>  
    63. /// 将 byte[] 转成 Stream  
    64. /// </summary>  
    65. public Stream BytesToStream(byte[] bytes) 
    66.     Stream stream = new MemoryStream(bytes); 
    67.     return stream; 
    68.  
    69.  
    70. /* - - - - - - - - - - - - - - - - - - - - - - - -
    71. * Stream 和 文件之间的转换
    72. * - - - - - - - - - - - - - - - - - - - - - - - */ 
    73. /// <summary>  
    74. /// 将 Stream 写入文件  
    75. /// </summary>  
    76. publicvoid StreamToFile(Stream stream,string fileName) 
    77.     // 把 Stream 转换成 byte[]  
    78.     byte[] bytes = newbyte[stream.Length]; 
    79.     stream.Read(bytes, 0, bytes.Length); 
    80.     // 设置当前流的位置为流的开始  
    81.     stream.Seek(0, SeekOrigin.Begin); 
    82.  
    83.     // 把 byte[] 写入文件  
    84.     FileStream fs = new FileStream(fileName, FileMode.Create); 
    85.     BinaryWriter bw = new BinaryWriter(fs); 
    86.     bw.Write(bytes); 
    87.     bw.Close(); 
    88.     fs.Close(); 
    89.  
    90. /// <summary>  
    91. /// 从文件读取 Stream  
    92. /// </summary>  
    93. public Stream FileToStream(string fileName) 
    94. {             
    95.     // 打开文件  
    96.     FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); 
    97.     // 读取文件的 byte[]  
    98.     byte[] bytes = newbyte[fileStream.Length]; 
    99.     fileStream.Read(bytes, 0, bytes.Length); 
    100.     fileStream.Close(); 
    101.     // 把 byte[] 转换成 Stream  
    102.     Stream stream = new MemoryStream(bytes); 
    103.     return stream; 
    104. }  
  • 相关阅读:
    面向对象反射、元类
    面向对象高级
    面向对象之封装
    抽象、继承、组合
    面向对象基础
    常用模块及其使用(二)
    常用模块及其使用
    模块及模块的使用
    drf框架之视图类、视图家族和路由组件
    drf框架群查接口的筛选组件之搜索过滤组件、排序过滤组件、分页器组件、django_filter插件
  • 原文地址:https://www.cnblogs.com/fogwang/p/3182390.html
Copyright © 2020-2023  润新知