Bitmap和byte[]的相互转换
/// <summary>
/// 将Bitmap转换为字节数组
/// </summary>
/// <param name="width">图像宽度</param>
/// <param name="height">图像长度</param>
/// <param name="channel">图像通道</param>
/// <param name="img">原图像</param>
/// <returns></returns>
public static byte[] getByteStreamFromBitmap(int width, int height, int channel, Bitmap img)
{
byte[] bytes = new byte[width * height * channel];
BitmapData im = img.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, img.PixelFormat);
int stride = im.Stride;
int offset = stride - width * channel;
int length = stride * height;
byte[] temp = new byte[stride * height];
Marshal.Copy(im.Scan0, temp, 0, temp.Length);
img.UnlockBits(im);
int posreal = 0;
int posscan = 0;
for (int c = 0; c < height; c++)
{
for (int d = 0; d < width * channel; d++)
{
bytes[posreal++] = temp[posscan++];
}
posscan += offset;
}
return bytes;
} /// <summary>
/// 将一个字节数组转换为8bit灰度位图
/// </summary>
/// <param name="rawValues">显示字节数组</param>
/// <param name="width">图像宽度</param>
/// <param name="height">图像高度</param>
/// <returns>位图</returns>
public static Bitmap ToGrayBitmap(byte[] rawValues, int width, int height)
{
//申请目标位图的变量,并将其内存区域锁定
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); //获取图像参数
int stride = bmpData.Stride; // 扫描线的宽度
int offset = stride - width; // 显示宽度与扫描线宽度的间隙
IntPtr iptr = bmpData.Scan0; // 获取bmpData的内存起始位置
int scanBytes = stride * height;// 用stride宽度,表示这是内存区域的大小 //下面把原始的显示大小字节数组转换为内存中实际存放的字节数组
int posScan = 0, posReal = 0;// 分别设置两个位置指针,指向源数组和目标数组
byte[] pixelValues = new byte[scanBytes]; //为目标数组分配内存 for (int x = 0; x < height; x++)
{
//下面的循环节是模拟行扫描
for (int y = 0; y < width; y++)
{
pixelValues[posScan++] = rawValues[posReal++];
}
posScan += offset; //行扫描结束,要将目标位置指针移过那段“间隙”
} //用Marshal的Copy方法,将刚才得到的内存字节数组复制到BitmapData中
System.Runtime.InteropServices.Marshal.Copy(pixelValues, 0, iptr, scanBytes);
bmp.UnlockBits(bmpData); // 解锁内存区域 //下面的代码是为了修改生成位图的索引表,从伪彩修改为灰度
ColorPalette tempPalette;
using (Bitmap tempBmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed))
{
tempPalette = tempBmp.Palette;
}
for (int i = 0; i < 256; i++)
{
tempPalette.Entries[i] = Color.FromArgb(i, i, i);
} bmp.Palette = tempPalette; //算法到此结束,返回结果
return bmp;
}
Bitmap和byte[]的相互转换的更多相关文章
- android开发之Bitmap 、byte[] 、 Drawable之间的相互转换
一.相关概念 1.Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable) ...
- Android-Drawable、Bitmap、byte[]、资源文件相互转换
我们在Android的开发中,经常可以遇到图片的处理,当中,有很多是 Bitmap.Drawable.byte[]和资源文件它们直接相互转换. 今天就此总结一下: 1.资源文件转为Drawable 2 ...
- Android Drawable、Bitmap、byte[]之间的转换
转自http://blog.csdn.net/june5253/article/details/7826597 1.Bitmap-->Drawable Bitmap drawable2Bitma ...
- Android中Bitmap, Drawable, Byte,ID之间的转化
Android中Bitmap, Drawable, Byte,ID之间的转化 1. Bitmap 转化为 byte ByteArrayOutputStream out = new ByteArray ...
- Drawable、Bitmap、byte[]之间的转换
android在处理一写图片资源的时候,会进行一些类型的转换: 1 Drawable → Bitmap 的简单方法 ((BitmapDrawable)res.getDrawable(R.drawabl ...
- WPF中的Bitmap与byte
原文:WPF中的Bitmap与byte public MainWindow() { InitializeComponent(); byte[] b = GetPictureData(@"F: ...
- Android Bitmap Drawable byte[] InputStream 相互转换方法
用android处理图片的时候,由于得到的资源不一样,所以经常要在各种格式中相互转化,以下介绍了 Bitmap Drawable byte[] InputStream 之间的转换方法: import ...
- c# 靠谱的bitmap转byte[]
public static byte[] Bitmap2Byte(Bitmap bitmap) { using (MemoryStream stream = new MemoryStream()) { ...
- Android中Bitmap, Drawable, Byte之间的转化
1. Bitmap 转化为 byte ByteArrayOutputStream out = new ByteArrayOutputStream(); bitmap.compress(Bitmap. ...
随机推荐
- ABAQUS和UG许可证冲突问题的解决方案
前段时间重新安装了ABAQUS,更新到了2020版本后,发现NX UG怎么突然打不开了,搜索一下,发现是两个许可证有冲突.找了很多解决方案,主要归纳为以下两种: 方法一:Lmtools修改法 先说结论 ...
- Go 语言入门 1-管道的特性及实现原理
入坑 go 也快一年了,从今天开始会定期分享一下 Go 语言学习过程中的一些基础知识. go 语言中的管道, 主要是用于协程之间的通信, 比 UNIX 的管道更加轻量和易用. 我们先看一下管道的数据结 ...
- Go 语言入门 3-动态数组(slice)的特性及实现原理
go 语言中的动态数组(slice),是基于数组实现的,可以相比数组而言更加的灵活.其他语言的 slice 通常仅是一个 API, 但是 go 语言的 slice 不仅仅是一种操作, 也是一种数据结构 ...
- django_day03
django_day03 Django的view(视图) CBV和FBV FBV:function based view 基于函数的视图 CBV:class based view 基于类的视图 fro ...
- LIKE与等式查询比较
我们知道 char 是定长类型的数据,如果数据长度小于定义的长度,会在字符串尾部加上空格.而对于空格的处理,对于等式匹配,或length等,会忽略空格.而对于like 或模式匹配,空格不能忽略. 一. ...
- 一门能让你五分钟学会的语言-Brainfuck
看到标题,不出意外的话,你肯定开始骂我了:**标题党,什么编程语言五分钟就能学会? 其实我本来也是不相信的,但是学过了才知道这是真的. 1.Brainfuck 看到这个小标题,不要误会,我没有骂人. ...
- MinIO存储桶通知指南
官方文档地址:http://docs.minio.org.cn/docs/master/minio-bucket-notification-guide 存储桶(Bucket)如果发生改变,比如上传对象 ...
- Elasticsearch:分布式计分
- 使用cnpm创建vue项目(含离线安装)
# 全局安装淘宝cnpm npm install -g cnpm --registry=https://registry.npm.taobao.org #升级 npm cnpm install npm ...
- 项目的依赖包(node_modules)删除
快速删除依赖包一共分为三部 1.打开命令行(管理员身份),执行 npm i -g npkill 2.cd 进入到想删除的项目中,执行 npkill 3.执行完成会进入到npkill页面,等待搜索完成, ...