方法一:

        /// <summary>
/// 将数组转换成彩色图片
/// </summary>
/// <param name="rawValues">图像的byte数组</param>
/// <param name="width">图像的宽</param>
/// <param name="height">图像的高</param>
/// <returns>Bitmap对象</returns>
public Bitmap ToColorBitmap(byte[] rawValues, int width, int height)
{
//// 申请目标位图的变量,并将其内存区域锁定
try
{
if (width != oldPicWidth || height != oldPicHeight)//如果图像尺寸发生变化,则需要重新new一下Bitmap对象
{
if (m_currBitmap != null)
m_currBitmap = null; m_currBitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
m_rect = new Rectangle(, , width, height);
m_bitmapData = m_currBitmap.LockBits(m_rect, ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb); } IntPtr iptr = m_bitmapData.Scan0; // 获取bmpData的内存起始位置 //// 用Marshal的Copy方法,将刚才得到的内存字节数组复制到BitmapData中
System.Runtime.InteropServices.Marshal.Copy(rawValues, , iptr, width * height * ); if (width != oldPicWidth || height != oldPicHeight)
{
m_currBitmap.UnlockBits(m_bitmapData);
oldPicWidth = width;
oldPicHeight = height;
} //// 算法到此结束,返回结果 return m_currBitmap;
}
catch (System.Exception ex)
{
return null;
}
}

上述方法有个问题,如果是从在线视频流中取数据,如果在短时间内,多次调用此方法,则会抛GDI+异常,或者提示Bitmap对象被占用。为了解决这个问题,后来想到了用Bitmap数组来解决。

方法如下

方法二:

        private Bitmap[] m_pBitmaps = new Bitmap[];
private int m_nCurrBitmapIdx = -;
public Bitmap ToColorBitmap2(byte[] rawValues, int width, int height)
{
// 申请目标位图的变量,并将其内存区域锁定
//初始化Bitmap数组
if (m_bFrmSizeChange || m_nCurrBitmapIdx < )
{
for (int i = ; i < ; i++)
{
m_pBitmaps[i] = new Bitmap(width, height, PixelFormat.Format24bppRgb);
}
m_nCurrBitmapIdx = ;
m_bFrmSizeChange = false;
}
Bitmap bmp = m_pBitmaps[m_nCurrBitmapIdx];
m_nCurrBitmapIdx++;
if (m_nCurrBitmapIdx >= )
m_nCurrBitmapIdx = ; try
{
//Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
BitmapData bmpData = bmp.LockBits(new Rectangle(, , width, height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb); //// 获取图像参数
//int stride = bmpData.Stride; // 扫描线的宽度
IntPtr iptr = bmpData.Scan0; // 获取bmpData的内存起始位置
//int scanBytes = stride * height;// 用stride宽度,表示这是内存区域的大小 //// 用Marshal的Copy方法,将刚才得到的内存字节数组复制到BitmapData中
System.Runtime.InteropServices.Marshal.Copy(rawValues, , iptr, width * height * );
bmp.UnlockBits(bmpData); // 解锁内存区域
//// 算法到此结束,返回结果
return bmp;
}
catch (System.Exception e)
{
//Tools.m_CreateLogTxt("ToColorBitmap2", e.ToString(), Index);
return null;
}
}

C# byte数组转成Bitmap对象的更多相关文章

  1. 获取网络图片并显示在picturbox上,byte[]数组转换成Image:

    private void getWebPicture_Click(object sender, EventArgs e) { WebRequest request = WebRequest.Creat ...

  2. 将一个读取流转换成bitmap对象

     将一个读取流转换成bitmap对象:         BitmapFactory:可以将文件,读取流,字节数组转换成一个Bitmap对象.         Bitmap bitmap = Bitma ...

  3. Android中如何将Bitmap byte裸数据转换成Bitmap图片int数据

    Android中如何将Bitmap byte裸数据转换成Bitmap图片int数据 2014-06-11 10:45:14   阅读375次 我们在JNI中处理得到的BMP图片Raw数据,我们应该如何 ...

  4. c# 将byte数组保存成图片

    将byte数组保存成图片: 方式一:System.IO.File.WriteAllBytes(@"c:\test.jpg", bytes); 方式二:MemoryStream ms ...

  5. JAVA中将byte[]数组转成16进制字符串

    方法一: /** * byte数组转化为16进制字符串 * @param bytes * @return */ public static String byteToHexString(byte[] ...

  6. js 一维数组转成tree 对象

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. C# byte数组转换成List<String>

    byte[] bys=buffer; string[] AllDataList=  Encoding.Default.GetString(bys).Split(Environment.NewLine. ...

  8. C#中如何把byte[]数组转换成其他类型

    http://bbs.csdn.net/topics/20447859 byte[] bytes = new byte[256]; //receive some stream from network ...

  9. 基于List数组转换成tree对象

    package com.shjysoft.yunxi.sync.webservice; import java.util.ArrayList;import java.util.Date;import ...

随机推荐

  1. Ubuntu下MongoDB的安装

    一.MongoDB介绍 MongoDB 是一个是一个基于分布式文件存储的数据库,介于关系数据库和非关系数据库之间,是非关系数据库当中功能最丰富,最像关系数据库的.他支持的数据结构非常松散,是类似jso ...

  2. NumPy字节交换

    NumPy - 字节交换 我们已经知道,存储在计算机内存中的数据取决于 CPU 使用的架构. 它可以是小端(最小有效位存储在最小地址中)或大端(最小有效字节存储在最大地址中). numpy.ndarr ...

  3. ElasticSearch数据副本模型

    介绍 ES里面的每一个索引(Index)由多个shard组成,每一个shard有多个副本.这些副本被称为同步组.当增加或者删除文档时,这些副本之间必须保持同步,以便让所有副本都能包含相同的文档.如果同 ...

  4. Sqlserver 查询 临时字段

    临时字段格式   字段名=N'字段值' 例子如下: select cEmp_C, cEmp_N, oper_id=N'001', log_pw=N'123', sSex, cDept_C, cDept ...

  5. spring3: AOP 之切面实例化模型 ——跟我学spring3

    所谓切面实例化模型指何时实例化切面. Spring AOP支持AspectJ的singleton.perthis.pertarget实例化模型(目前不支持percflow.percflowbelow ...

  6. KindEditor 上传文件

    Jsp页面代码: <script> var editor; KindEditor.ready(function(K) { editor = K.create('textarea[name= ...

  7. IE兼容性测试工具IETester

    IE兼容性测试工具:IETester 1.这种做法,不能做到100%的覆盖: 2.实际的业务场景会比IEtester更符合.

  8. Activiti快速入门

    1.什么是Activiti 在解释activiti之前我们看一下什么是工作流.工作流(Workflow),就是“业务过程的部分或整体在计算机应用环境下的自动化”,它主要解决的是“使在多个参与者之间按照 ...

  9. IE中iframe兼容性问题

    在使用iframe的时候,有时候想要让调用的iframe框架里面的不显示白背景,让它变得透明,在firefox是透明的,但是在IE浏览器却不透明. 这个其实比较容易解决,只需要增加一个属性即可. 就是 ...

  10. PostgreSQL归档日志 手动触发归档 select pg_switch_xlog()

    (转,做记录) 一 环境信息--1.1 PostgreSQL 版本 9.3.0 --1.2 postgresql.conf wal_level = hot_standbyarchive_mode = ...