C# byte数组转成Bitmap对象
方法一:
/// <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对象的更多相关文章
- 获取网络图片并显示在picturbox上,byte[]数组转换成Image:
private void getWebPicture_Click(object sender, EventArgs e) { WebRequest request = WebRequest.Creat ...
- 将一个读取流转换成bitmap对象
将一个读取流转换成bitmap对象: BitmapFactory:可以将文件,读取流,字节数组转换成一个Bitmap对象. Bitmap bitmap = Bitma ...
- Android中如何将Bitmap byte裸数据转换成Bitmap图片int数据
Android中如何将Bitmap byte裸数据转换成Bitmap图片int数据 2014-06-11 10:45:14 阅读375次 我们在JNI中处理得到的BMP图片Raw数据,我们应该如何 ...
- c# 将byte数组保存成图片
将byte数组保存成图片: 方式一:System.IO.File.WriteAllBytes(@"c:\test.jpg", bytes); 方式二:MemoryStream ms ...
- JAVA中将byte[]数组转成16进制字符串
方法一: /** * byte数组转化为16进制字符串 * @param bytes * @return */ public static String byteToHexString(byte[] ...
- js 一维数组转成tree 对象
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- C# byte数组转换成List<String>
byte[] bys=buffer; string[] AllDataList= Encoding.Default.GetString(bys).Split(Environment.NewLine. ...
- C#中如何把byte[]数组转换成其他类型
http://bbs.csdn.net/topics/20447859 byte[] bytes = new byte[256]; //receive some stream from network ...
- 基于List数组转换成tree对象
package com.shjysoft.yunxi.sync.webservice; import java.util.ArrayList;import java.util.Date;import ...
随机推荐
- maven中pom.xml标签介绍
pom作为项目对象模型.通过xml表示maven项目,使用pom.xml来实现.主要描述了项目:包括配置文件:开发者需要遵循的规则,缺陷管理系统,组织和licenses,项目的url,项目的依赖性,以 ...
- Sqoop-将MySQL数据导入到hive orc表
sqoop创建并导入数据到hive orc表 sqoop import \ --connect jdbc:mysql://localhost:3306/spider \ --username root ...
- WebSocket相关
原文:http://www.cnblogs.com/jinjiangongzuoshi/p/5062092.html 前言 今天看了一些资料,记录一下心得. websocket是html5引入的一个新 ...
- MyEclipse 为xml添加本地的dtd文件
在使用Eclipse或MyEclipse编辑XML文件的时候经常会碰到编辑器不提示的现象,这常常是因为其xml文件需要参考的DTD文件找不到,还有因为网络的问题不能及时提示而产生的.Eclipse/M ...
- Linux嵌入式 -- 内核简介(x86)
0. 嵌入式系统 以应用为中心,软硬件可裁剪,对功耗.对体积.对成本等都有严格要求的专用计算机系统. 1. linux体系结构 2. 为什么 划分为 用户空间 和 内核控件 ? 分两级,内核和应用 ...
- UniquePaths,UniquePaths2,路径问题。动态规划。
UniquePaths:给定m*n矩阵,从(0,0)到(m-1,n-1)路径条数.只能向下向右走. 算法分析:这和爬楼梯问题很像,到(m,n)的路径数是到(m-1,n)和(m,n-1)路径和.第一行, ...
- 在oracle中插入数据报错:ORA-00984列在此处不允许
这里报错的原因就是当数据类型varchar2时没有使用单引号. 没写单引号,不管是双引号还是什么都没写都会报这个错误.
- SPOJ - LCS2
后缀自动机板子题 https://vjudge.net/problem/28017/origin 找多串的最长公共子串 //#pragma comment(linker, "/stack:2 ...
- 【scala】集合框架
- 面向对象设计原则-SOLID
SOLID的意思是: Single responsibility principle 单一职责原则 Open/close principle 开放/封闭原则 Liskov substitution p ...