熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C#中使用byte[]數據,生成Bitmap

2022-06-13   來源: .NET編程 

  /// <summary>
/// 使用byte[]數據生成色灰度 BMP 位圖
/// </summary>
/// <param name=originalImageData></param>
/// <param name=originalWidth></param>
/// <param name=originalHeight></param>
/// <returns></returns>
        public static Bitmap CreateBitmap(byte[] originalImageData int originalWidth int originalHeight)
{
//指定位格式
            Bitmap resultBitmap = new Bitmap(originalWidth originalHeight SystemDrawingImagingPixelFormatFormatbppIndexed);
//將該位圖存入內存中
            MemoryStream curImageStream = new MemoryStream();
resultBitmapSave(curImageStream SystemDrawingImagingImageFormatBmp);
curImageStreamFlush();
//由於位圖數據需要DWORD對齊(byte倍數)計算需要補位的個數
            int curPadNum = ((originalWidth * + ) / * ) originalWidth;
//最終生成的位圖數據大小
            int bitmapDataSize = ((originalWidth * + ) / * ) * originalHeight;
//數據部分相對文件開始偏移具體可以參考位圖文件格式
            int dataOffset = ReadData(curImageStream );
//改變調色板因為默認的調色板是位彩色的需要修改為色的調色板
            int paletteStart = ;
int paletteEnd = dataOffset;
int color = ;
for (int i = paletteStart; i < paletteEnd; i += )
{
byte[] tempColor = new byte[];
tempColor[] = (byte)color;
tempColor[] = (byte)color;
tempColor[] = (byte)color;
tempColor[] = (byte);
color++;
curImageStreamPosition = i;
curImageStreamWrite(tempColor );
}
//最終生成的位圖數據以及大小高度沒有變寬度需要調整
            byte[] destImageData = new byte[bitmapDataSize];
int destWidth = originalWidth + curPadNum;
//生成最終的位圖數據注意的是位圖數據 從左到右從下到上所以需要顛倒
            for (int originalRowIndex = originalHeight ; originalRowIndex >= ; originalRowIndex)
{
int destRowIndex = originalHeight originalRowIndex ;
for (int dataIndex = ; dataIndex < originalWidth; dataIndex++)
{
//同時還要注意新的位圖數據的寬度已經變化destWidth否則會產生錯位
                    destImageData[destRowIndex * destWidth + dataIndex] = originalImageData[originalRowIndex * originalWidth + dataIndex];
}
}
//將流的Position移到數據段  
            curImageStreamPosition = dataOffset;
//將新位圖數據寫入內存中
            curImageStreamWrite(destImageData bitmapDataSize);
curImageStreamFlush();
//將內存中的位圖寫入Bitmap對象
            resultBitmap = new Bitmap(curImageStream);
return resultBitmap;
}
/// <summary>
/// 從內存流中指定位置讀取數據
/// </summary>
/// <param name=curStream></param>
/// <param name=startPosition></param>
/// <param name=length></param>
/// <returns></returns>
        public static int ReadData(MemoryStream curStream int startPosition int length)
{
int result = ;
byte[] tempData = new byte[length];
curStreamPosition = startPosition;
curStreamRead(tempData length);
result = BitConverterToInt(tempData );
return result;
}
/// <summary>
/// 向內存流中指定位置寫入數據
/// </summary>
/// <param name=curStream></param>
/// <param name=startPosition></param>
/// <param name=length></param>
/// <param name=value></param>
        public static void WriteData(MemoryStream curStream int startPosition int length int value)
{
curStreamPosition = startPosition;
curStreamWrite(BitConverterGetBytes(value) length);
}


From:http://tw.wingwit.com/Article/program/net/201311/12589.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.