?? fastbwbmp.cs
字號(hào):
/**
*
* (IEC16022Sharp DataMatrix bar code generation lib)
*
* FastBWBmp: a class for direct creation of 1 bit/pixel BMP file
* (c) 2007 Fabrizio Accatino <fhtino@yahoo.com>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
// Many informations about BMP format from http://www.fortunecity.com/skyscraper/windows/364/bmpffrmt.html
using System;
using System.IO;
namespace IEC16022Sharp
{
public class FastBWBmp
{
private int _width;
private int _height;
private byte[,] _dots;
private byte[] _pixelData;
private byte[] _fileBytes;
public FastBWBmp( byte[,] dots)
{
_width = dots.GetLength(1);
_height = dots.GetLength(0);
_dots = dots;
_pixelData = ConvertTo1BitPixelData();
_fileBytes = BuildFileBytes();
}
/// <summary>
/// Get the byte array of the bmp file data
/// </summary>
/// <returns></returns>
public byte[] ToByteArray()
{
return _fileBytes;
}
/// <summary>
/// Save bmp to file
/// </summary>
public void Save(string fileName)
{
using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
fs.Write(_fileBytes, 0, _fileBytes.Length);
}
}
/// <summary>
/// Save bmp to stream
/// </summary>
public void Save(Stream strm)
{
strm.Write(_fileBytes, 0, _fileBytes.Length);
}
private byte[] ConvertTo1BitPixelData()
{
int rows = _dots.GetLength(0);
int cols = _dots.GetLength(1);
// intero superiore
int bytesPerRow = cols / 8 + (cols % 8 == 0 ? 0 : 1);
// arrotonda sempre a multipli di 4 bytes
if (bytesPerRow % 4 > 0)
bytesPerRow += 4 - bytesPerRow % 4;
// Alloca spazio per i pixel
byte[] bytes = new byte[bytesPerRow * rows];
// Ciclo allocazione dot --> pixel
for (int r = 0; r < rows; r++)
{
// Idea iniziale: ogni byte
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -