一、Byte[] 转 System.Drawing.Bitmap

public static Bitmap CreateBitmap(byte[] originalImageData, int originalWidth, int originalHeight)
{

//指定8位格式,即256色
Bitmap resultBitmap = new Bitmap(originalWidth, originalHeight, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
//将该位图存入内存中
MemoryStream curImageStream = new MemoryStream();
resultBitmap.Save(curImageStream, System.Drawing.Imaging.ImageFormat.Bmp);
curImageStream.Flush();
//由于位图数据需要DWORD对齐(4byte倍数),计算需要补位的个数
int curPadNum = ((originalWidth * 8 + 31) / 32 * 4) - originalWidth;
//最终生成的位图数据大小
int bitmapDataSize = ((originalWidth * 8 + 31) / 32 * 4) * originalHeight;
//数据部分相对文件开始偏移,具体可以参考位图文件格式
int dataOffset = ReadData(curImageStream, 10, 4);
//改变调色板,因为默认的调色板是32位彩色的,需要修改为256色的调色板
int paletteStart = 54;
int paletteEnd = dataOffset;
int color = 0;
for (int i = paletteStart; i < paletteEnd; i += 4)
{

byte[] tempColor = new byte[4];
tempColor[0] = (byte)color;
tempColor[1] = (byte)color;
tempColor[2] = (byte)color;
tempColor[3] = (byte)0;
color++;
curImageStream.Position = i;
curImageStream.Write(tempColor, 0, 4);

}
//最终生成的位图数据,以及大小,高度没有变,宽度需要调整
byte[] destImageData = new byte[bitmapDataSize];
int destWidth = originalWidth + curPadNum;
//生成最终的位图数据,注意的是,位图数据 从左到右,从下到上,所以需要颠倒
for (int originalRowIndex = originalHeight - 1; originalRowIndex >= 0; originalRowIndex--)
{
  int destRowIndex = originalHeight - originalRowIndex - 1;
  for (int dataIndex = 0; dataIndex < originalWidth; dataIndex++)
    {
      //同时还要注意,新的位图数据的宽度已经变化destWidth,否则会产生错位
      destImageData[destRowIndex * destWidth + dataIndex] = originalImageData[originalRowIndex * originalWidth + dataIndex];
      }

}

//将流的Position移到数据段
curImageStream.Position = dataOffset;
//将新位图数据写入内存中
curImageStream.Write(destImageData, 0, bitmapDataSize);
curImageStream.Flush();
//将内存中的位图写入Bitmap对象
resultBitmap = new Bitmap(curImageStream);
return resultBitmap;

}

public static int ReadData(MemoryStream curStream, int startPosition, int length)
{

int result = -1;
byte[] tempData = new byte[length];
curStream.Position = startPosition;
curStream.Read(tempData, 0, length);
result = BitConverter.ToInt32(tempData, 0);
return result;

}

  

  

C# Image 、 byte[] 、Bitmap之间的转化的更多相关文章

  1. Android中Bitmap, Drawable, Byte,ID之间的转化

    Android中Bitmap, Drawable, Byte,ID之间的转化 1.  Bitmap 转化为 byte ByteArrayOutputStream out = new ByteArray ...

  2. java中 16进制字符串 与普通字符串 与 byte数组 之间的转化

    方法依赖commons-codec包  maven的引入方式如下 <dependency> <groupId>commons-codec</groupId> < ...

  3. Byte[]、Image、Bitmap 之间的相互转换

    原文:Byte[].Image.Bitmap 之间的相互转换 /// <summary>        /// 将图片Image转换成Byte[]        /// </summ ...

  4. C#中char[]与string之间的转换;byte[]与string之间的转化

    目录 1.char[]与string之间的转换 2.byte[]与string之间的转化 1.char[]与string之间的转换 //string 转换成 Char[] string str=&qu ...

  5. JAVA 文件与base64之间的转化, 以及Web实现base64上传文件

    <1>文件与base64字符串之间的转化 package servlet_file_upload; import java.io.File; import java.io.FileInpu ...

  6. Java中InputStream和String之间的转化

    https://blog.csdn.net/lmy86263/article/details/60479350 在Java中InputStream和String之间的转化十分普遍,本文主要是总结一下转 ...

  7. javascript中日期格式与时间戳之间的转化

    日期格式与时间戳之间的转化 一:日期格式转化为时间戳 function timeTodate(date) { var new_str = date.replace(/:/g,'-'); new_str ...

  8. C#入门篇6-6:字符串操作 StringBiulder string char[]之间的转化

    //StringBiulder string char[]之间的转化 public static void Fun3() { StringBuilder sb = new StringBuilder( ...

  9. NSJSONSerialization-JSON数据与NSDictionary和NSArray之间的转化

    转载▼     在iOS  5 中,苹果引入了一个解析JSON串的NSJSONSerialization类. 通过该类,我们可以完成JSON数据与NSDictionary和NSArray之间的转化. ...

随机推荐

  1. Codeforces Round #375 (Div. 2) - B

    题目链接:http://codeforces.com/contest/723/problem/B 题意:给定一个字符串.只包含_,大小写字母,左右括号(保证不会出现括号里面套括号的情况),_分隔开单词 ...

  2. [工作中的设计模式]备忘录模式memento

    一.模式解析 备忘录对象是一个用来存储另外一个对象内部状态的快照的对象.备忘录模式的用意是在不破坏封装的条件下,将一个对象的状态捕捉(Capture)住,并外部化,存储起来,从而可以在将来合适的时候把 ...

  3. Rectangle(csu)

    Description Now ,there are some rectangles. The area of these rectangles is 1* x or 2 * x ,and now y ...

  4. HTML5(常用的表单控件)

    常用的HTML5的表单控件: Input 类型: color color 类型用在input字段主要用于选取颜色,如下所示: 从拾色器中选择一个颜色: 选择你喜欢的颜色: <input type ...

  5. 请将 php.ini 中的 short_open_tag 设置为 On,否则无法继续安装。

    安装的wamp套件,访问http://localhost/Discuz/install/index.PHP进行安装操作,提示 对不起,请将 php.ini 中的 short_open_tag 设置为 ...

  6. get请求

    写在前面的话 XMLHttpRequest对象的open方法的第一个参数为request-type,取值可以为get或post.本篇介绍get请求. get请求的目的,主要是为了获取数据.虽然get请 ...

  7. Java classpath 如何自动添加web-content /lib下的jar包

    右键属性--Java build path--Libraries--add library--Web app libraries -- next --next -- ok

  8. Leetcode Reverse Words in a String

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

  9. 让 Web 站点崩溃最常见的七大原因

    磁盘已满 导致系统无法正常运行的最可能的原因是磁盘已满.一个好的网络管理员会密切关注磁盘的使用情况,隔一定的时间,就需要将磁盘上的一些负载转存到备份存储介质中(例如磁带).   日志文件会很快用光所有 ...

  10. UVA 11021 C - Tribles(概率DP)

    记忆化就可以搞定,比赛里都没做出来,真的是态度有问题啊... #include <iostream> #include<cstdio> #include<cstring& ...