C# Byte[]、Image、Bitmap 之间的相互转换
- //byte[] 转图片
- public static Bitmap BytesToBitmap(byte[] Bytes)
- {
- MemoryStream stream = null;
- try
- {
- stream = new MemoryStream(Bytes);
- return new Bitmap((Image)new Bitmap(stream));
- }
- catch (ArgumentNullException ex)
- {
- throw ex;
- }
- catch (ArgumentException ex)
- {
- throw ex;
- }
- finally
- {
- stream.Close();
- }
- }
- //图片转byte[]
- public static byte[] BitmapToBytes(Bitmap Bitmap)
- {
- MemoryStream ms = null;
- try
- {
- ms = new MemoryStream();
- Bitmap.Save(ms, Bitmap.RawFormat);
- byte[] byteImage = new Byte[ms.Length];
- byteImage = ms.ToArray();
- return byteImage;
- }
- catch (ArgumentNullException ex)
- {
- throw ex;
- }
- finally
- {
- ms.Close();
- }
- }
- }
- =====================
- * Stream 和 byte[] 之间的转换
- * - - - - - - - - - - - - - - - - - - - - - - - */
- /// <summary>
- /// 将 Stream 转成 byte[]
- /// </summary>
- public byte[] StreamToBytes(Stream stream)
- {
- byte[] bytes = new byte[stream.Length];
- stream.Read(bytes, , bytes.Length);
- // 设置当前流的位置为流的开始
- stream.Seek(, SeekOrigin.Begin);
- return bytes;
- }
- /// <summary>
- /// 将 byte[] 转成 Stream
- /// </summary>
- public Stream BytesToStream(byte[] bytes)
- {
- Stream stream = new MemoryStream(bytes);
- return stream;
- }
- /* - - - - - - - - - - - - - - - - - - - - - - - -
- * Stream 和 文件之间的转换
- * - - - - - - - - - - - - - - - - - - - - - - - */
- /// <summary>
- /// 将 Stream 写入文件
- /// </summary>
- public void StreamToFile(Stream stream,string fileName)
- {
- // 把 Stream 转换成 byte[]
- byte[] bytes = new byte[stream.Length];
- stream.Read(bytes, , bytes.Length);
- // 设置当前流的位置为流的开始
- stream.Seek(, SeekOrigin.Begin);
- // 把 byte[] 写入文件
- FileStream fs = new FileStream(fileName, FileMode.Create);
- BinaryWriter bw = new BinaryWriter(fs);
- bw.Write(bytes);
- bw.Close();
- fs.Close();
- }
- /// <summary>
- /// 从文件读取 Stream
- /// </summary>
- public Stream FileToStream(string fileName)
- {
- // 打开文件
- FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
- // 读取文件的 byte[]
- byte[] bytes = new byte[fileStream.Length];
- fileStream.Read(bytes, , bytes.Length);
- fileStream.Close();
- // 把 byte[] 转换成 Stream
- Stream stream = new MemoryStream(bytes);
- return stream;
- }
C# Byte[]、Image、Bitmap 之间的相互转换的更多相关文章
- Byte[]、Image、Bitmap 之间的相互转换
原文:Byte[].Image.Bitmap 之间的相互转换 /// <summary> /// 将图片Image转换成Byte[] /// </summ ...
- android开发之Bitmap 、byte[] 、 Drawable之间的相互转换
一.相关概念 1.Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable) ...
- C# Image 、 byte[] 、Bitmap之间的转化
一.Byte[] 转 System.Drawing.Bitmap public static Bitmap CreateBitmap(byte[] originalImageData, int ori ...
- byte[],bitmap,drawable之间的相互转换
Byte[]转Bitmap BitmapFactory.decodeByteArray(data, 0, data.length); Bitmap转Byte[] ByteArrayOutputStre ...
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】
package com.soai.imdemo; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; ...
- Byte[]、Image、Bitmap_之间的相互转换
1.将图片Image转换成Byte[] /// <summary> /// 将图片Image转换成Byte[] /// </summary> ...
- Android中Bitmap对象和字节流之间的相互转换
android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte import java.io.B ...
- Android中Bitmap对象和字节流之间的相互转换(转)
android 将图片内容解析成字节数组:将字节数组转换为ImageView可调用的Bitmap对象:图片缩放:把字节数组保存为一个文件:把Bitmap转Byte import java.io.Buf ...
- Python网络编程——主机字节序和网络字节序之间的相互转换
If you ever need to write a low-level network application, it may be necessary to handle the low-lev ...
随机推荐
- Vue项目页面跳转时候的,浏览器窗口上方的进度条显示
1.安装: cnpm install --save nprogress 2.在main.js中引入: import NProgress from 'nprogress' import 'nprogre ...
- MAC下是用brew安装Redis
启动redis redis-server /etc/redis.conf 安装brew 在命令行里运行下面的命令,需要等待一段时间. ruby -e "$(curl -fsSL https: ...
- lsblk命令
lsblk命令用于列出所有可用块设备的信息,而且还能显示他们之间的依赖关系,但是它不会列出RAM盘的信息.块设备有硬盘,闪存盘,cd-ROM等等.lsblk命令包含在util-linux-ng包中,现 ...
- python 正则表达式 -- IP地址验证
p = re.compile("^((?:(2[0-4]\d)|(25[0-5])|([01]?\d\d?))\.){3}(?:(2[0-4]\d)|(255[0-5])|([01]?\d\ ...
- C++中的new、operator new与placement new
转:http://www.cnblogs.com/luxiaoxun/archive/2012/08/10/2631812.html new/delete与operator new/operator ...
- WebSphere ILog JRules 域的介绍和定制
WebSphere ILog JRules 域的介绍和定制 引言 随着企业业务的不断发展,越来越多的企业正经历着以下的情形: 企业需要对于业务系统的频繁变化做出及时的关注和响应,例如,竞争对手或经济环 ...
- 键盘 Input子系统
应用层测试代码 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <li ...
- 115道Java经典面试题(面中率最高、最全)
115道Java经典面试题(面中率最高.最全) Java是一个支持并发.基于类和面向对象的计算机编程语言.下面列出了面向对象软件开发的优点: 代码开发模块化,更易维护和修改. 代码复用. 增强代码的可 ...
- hbase1.4.0安装和使用
jia下载地址: http://mirrors.shuosc.org/apache/hbase/1.4.0/ 解压 tar -zxvf hbase-1.4.0-bin.tar.gz 修改环境变量 [ ...
- SpringBoot2 时间类型自动格式化 自动转换
package com.archibladwitwicke.springboot2.chapter03.configurer; import com.archibladwitwicke.springb ...