最近調用熱敏列印機需要列印單色,位深度為1的bmp照片,找了半天網上都是半成品,最後實作了,先謝謝各位大神,整體還是很有幫助,但是還是有些差距。
第一次寫部落格,不怎麼會寫,可能語言描述不是很好,但是代碼是完完整整寫入,寫對
static void Main(string[] args)
{
Bitmap b = (Bitmap)Bitmap.FromFile(@"D:\Books\2223.jpg");
Bitmap returnbit1 = RgbToGrayScale(b);
Bitmap xxx = GTo2Bit(returnbit1);
xxx.Save(@"D:\Books\XXXX1.jpg");
}
#region 灰階處理
/// <summary>
/// 将源圖像灰階化,并轉化為8位灰階圖像。
/// </summary>
/// <param name="original"> 源圖像。 </param>
/// <returns> 8位灰階圖像。 </returns>
public static Bitmap RgbToGrayScale(Bitmap original)
{
if (original != null)
{
// 将源圖像記憶體區域鎖定
Rectangle rect = new Rectangle(0, 0, original.Width, original.Height);
BitmapData bmpData = original.LockBits(rect, ImageLockMode.ReadOnly,
PixelFormat.Format24bppRgb);
// 擷取圖像參數
int width = bmpData.Width;
int height = bmpData.Height;
int stride = bmpData.Stride; // 掃描線的寬度,比實際圖檔要大
int offset = stride - width * 3; // 顯示寬度與掃描線寬度的間隙
IntPtr ptr = bmpData.Scan0; // 擷取bmpData的記憶體起始位置的指針
int scanBytesLength = stride * height; // 用stride寬度,表示這是記憶體區域的大小
// 分别設定兩個位置指針,指向源數組和目标數組
int posScan = 0, posDst = 0;
byte[] rgbValues = new byte[scanBytesLength]; // 為目标數組配置設定記憶體
Marshal.Copy(ptr, rgbValues, 0, scanBytesLength); // 将圖像資料拷貝到rgbValues中
// 配置設定灰階數組
byte[] grayValues = new byte[width * height]; // 不含未用空間。
// 計算灰階數組
byte blue, green, red, YUI;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
blue = rgbValues[posScan];
green = rgbValues[posScan + 1];
red = rgbValues[posScan + 2];
YUI = (byte)(0.229 * red + 0.587 * green + 0.144 * blue);
//grayValues[posDst] = (byte)((blue + green + red) / 3);
grayValues[posDst] = YUI;
posScan += 3;
posDst++;
}
// 跳過圖像資料每行未用空間的位元組,length = stride - width * bytePerPixel
posScan += offset;
}
// 記憶體解鎖
Marshal.Copy(rgbValues, 0, ptr, scanBytesLength);
original.UnlockBits(bmpData); // 解鎖記憶體區域
// 建構8位灰階位圖
Bitmap retBitmap = BuiltGrayBitmap(grayValues, width, height);
return retBitmap;
}
else
{
return null;
}
}
/// <summary>
/// 用灰階數組建立一個8位灰階圖像。
/// </summary>
/// <param name="rawValues"> 灰階數組(length = width * height)。 </param>
/// <param name="width"> 圖像寬度。 </param>
/// <param name="height"> 圖像高度。 </param>
/// <returns> 建立的8位灰階位圖。 </returns>
private static Bitmap BuiltGrayBitmap(byte[] rawValues, int width, int height)
{
// 建立一個8位灰階位圖,并鎖定記憶體區域操作
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
// 計算圖像參數
int offset = bmpData.Stride - bmpData.Width; // 計算每行未用空間位元組數
IntPtr ptr = bmpData.Scan0; // 擷取首位址
int scanBytes = bmpData.Stride * bmpData.Height; // 圖像位元組數 = 掃描位元組數 * 高度
byte[] grayValues = new byte[scanBytes]; // 為圖像資料配置設定記憶體
// 為圖像資料指派
int posSrc = 0, posScan = 0; // rawValues和grayValues的索引
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
grayValues[posScan++] = rawValues[posSrc++];
}
// 跳過圖像資料每行未用空間的位元組,length = stride - width * bytePerPixel
posScan += offset;
}
// 記憶體解鎖
Marshal.Copy(grayValues, 0, ptr, scanBytes);
bitmap.UnlockBits(bmpData); // 解鎖記憶體區域
// 修改生成位圖的索引表,從僞彩修改為灰階
ColorPalette palette;
// 擷取一個Format8bppIndexed格式圖像的Palette對象
using (Bitmap bmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed))
{
palette = bmp.Palette;
}
for (int i = 0; i < 256; i++)
{
palette.Entries[i] = Color.FromArgb(i, i, i);
}
// 修改生成位圖的索引表
bitmap.Palette = palette;
return bitmap;
}
#endregion
/// <summary>
/// 壓縮圖片 /// </summary>
/// <param name="fileStream">圖片流</param>
/// <param name="quality">壓縮質量0-100之間 數值越大質量越高</param>
/// <returns></returns>
private static byte[] CompressionImage(Bitmap file, long quality)
{
using (System.Drawing.Image img = (Image)file)
{
using (Bitmap bitmap = new Bitmap(img))
{
//ImageCodecInfo CodecInfo = GetEncoder(img.RawFormat);
ImageCodecInfo CodecInfo = GetEncoderInfo("image/jpeg");
System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, quality);
myEncoderParameters.Param[0] = myEncoderParameter;
using (MemoryStream ms = new MemoryStream())
{
bitmap.Save(ms, CodecInfo, myEncoderParameters);
myEncoderParameters.Dispose();
myEncoderParameter.Dispose();
return ms.ToArray();
}
}
}
}
private static ImageCodecInfo GetEncoderInfo(String mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for (j = 0; j < encoders.Length; ++j)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}
#region 二值化
/// <summary>
/// 将源灰階圖像二值化,并轉化為1位二值圖像。
/// </summary>
/// <param name="bmp"> 源灰階圖像。 </param>
/// <returns> 1位二值圖像。 </returns>
public static Bitmap GTo2Bit(Bitmap bmp)
{
if (bmp != null)
{
// 将源圖像記憶體區域鎖定
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly,
PixelFormat.Format8bppIndexed);
// 擷取圖像參數
int leng, offset_1bit = 0;
int width = bmpData.Width;
int height = bmpData.Height;
int stride = bmpData.Stride; // 掃描線的寬度,比實際圖檔要大
int offset = stride - width; // 顯示寬度與掃描線寬度的間隙
IntPtr ptr = bmpData.Scan0; // 擷取bmpData的記憶體起始位置的指針
int scanBytesLength = stride * height; // 用stride寬度,表示這是記憶體區域的大小
if (width % 32 == 0)
{
leng = width / 8;
}
else
{
leng = width / 8 + (4 - (width / 8 % 4));
if (width % 8 != 0)
{
offset_1bit = leng - width / 8;
}
else
{
offset_1bit = leng - width / 8;
}
}
// 分别設定兩個位置指針,指向源數組和目标數組
int posScan = 0, posDst = 0;
byte[] rgbValues = new byte[scanBytesLength]; // 為目标數組配置設定記憶體
Marshal.Copy(ptr, rgbValues, 0, scanBytesLength); // 将圖像資料拷貝到rgbValues中
// 配置設定二值數組
byte[] grayValues = new byte[leng * height]; // 不含未用空間。
// 計算二值數組
int x, v, t = 0;
for (int i = 0; i < height; i++)
{
for (x = 0; x < width; x++)
{
v = rgbValues[posScan];
t = (t << 1) | (v > 100 ? 1 : 0);
if (x % 8 == 7)
{
grayValues[posDst] = (byte)t;
posDst++;
t = 0;
}
posScan++;
}
if ((x %= 8) != 7)
{
t <<= 8 - x;
grayValues[posDst] = (byte)t;
}
// 跳過圖像資料每行未用空間的位元組,length = stride - width * bytePerPixel
posScan += offset;
posDst += offset_1bit;
}
// 記憶體解鎖
Marshal.Copy(rgbValues, 0, ptr, scanBytesLength);
bmp.UnlockBits(bmpData); // 解鎖記憶體區域
// 建構1位二值位圖
Bitmap retBitmap = twoBit(grayValues, width, height);
return retBitmap;
}
else
{
return null;
}
}
/// <summary>
/// 用二值數組建立一個1位二值圖像。
/// </summary>
/// <param name="rawValues"> 二值數組(length = width * height)。 </param>
/// <param name="width"> 圖像寬度。 </param>
/// <param name="height"> 圖像高度。 </param>
/// <returns> 建立的1位二值位圖。 </returns>
private static Bitmap twoBit(byte[] rawValues, int width, int height)
{
// 建立一個1位二值位圖,并鎖定記憶體區域操作
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format1bppIndexed);
BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.WriteOnly, PixelFormat.Format1bppIndexed);
// 計算圖像參數
int offset = bmpData.Stride - bmpData.Width / 8; // 計算每行未用空間位元組數
IntPtr ptr = bmpData.Scan0; // 擷取首位址
int scanBytes = bmpData.Stride * bmpData.Height; // 圖像位元組數 = 掃描位元組數 * 高度
byte[] grayValues = new byte[scanBytes]; // 為圖像資料配置設定記憶體
// 為圖像資料指派
int posScan = 0; // rawValues和grayValues的索引
for (int i = 0; i < height; i++)
{
for (int j = 0; j < bmpData.Width / 8; j++)
{
grayValues[posScan] = rawValues[posScan];
posScan++;
}
// 跳過圖像資料每行未用空間的位元組,length = stride - width * bytePerPixel
posScan += offset;
}
// 記憶體解鎖
Marshal.Copy(grayValues, 0, ptr, scanBytes);
bitmap.UnlockBits(bmpData); // 解鎖記憶體區域
// 修改生成位圖的索引表
ColorPalette palette;
// 擷取一個Format8bppIndexed格式圖像的Palette對象
using (Bitmap bmp = new Bitmap(1, 1, PixelFormat.Format1bppIndexed))
{
palette = bmp.Palette;
}
for (int i = 0; i < 2; i = +254)
{
palette.Entries[i] = Color.FromArgb(i, i, i);
}
// 修改生成位圖的索引表
bitmap.Palette = palette;
return bitmap;
}
#endregion
}
整體是轉轉換了單色的bmp格式,位深度也是1,但是此時還是不能列印,通過屬性看了一下,雖然都轉換,但是出現一個問題,都知道單色bmp照片的大小很小,接近于普通照片的百分之1左右,但是這個轉換之後大小還是沒有改變,是以繼續尋找。
public Bitmap Merge(Bitmap img)
{
//System.Drawing.Image img = System.Drawing.Image.FromFile("88.bmp");
ImageAttributes ta = new ImageAttributes();
//如實際發現幾個簡單又好用的矩陣:
float[][] mm = new float[][]{ //彩色變灰階的矩陣
new float[]{0.4f, 0.4f, 0.4f, 0, 0},
new float[]{0.3f, 0.3f, 0.3f, 0, 0},
new float[]{0.3f, 0.3f, 0.3f, 0, 0},
new float[]{0, 0, 0, 1, 0},
new float[]{0, 0, 0, 0, 1}
};
ColorMatrix cmt = new ColorMatrix(mm);
ta.SetColorMatrix(cmt);
Bitmap bmp = new Bitmap(img.Width, img.Height);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(img, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ta);
//g.DrawString("Foxit PDF Reader",new Font("宋體",8),new SolidBrush(Color.White),0,0);
g.Dispose();
//以下,把反相或者塗畫後的像點資料每一行的每8點簡單合并成1byte存儲
int midrgb = Color.FromArgb(128, 128, 128).ToArgb();
int stride;//簡單公式((width/8)+3)&(~3)
stride = (bmp.Width % 8) == 0 ? (bmp.Width / 8) : (bmp.Width / 8) + 1;
stride = (stride % 4) == 0 ? stride : ((stride / 4) + 1) * 4;
int k = bmp.Height * stride;
byte[] buf = new byte[k];
for (int j = 0; j < bmp.Height; j++)
{
k = j * stride;//因圖像寬度不同、有的可能有填充位元組需要跳越
int x = 0, ab = 0;
for (int i = 0; i < bmp.Width; i++)
{
//從灰階變單色(下法如果直接從彩色變單色效果不太好,不過反相也可以在這裡控制)
if ((bmp.GetPixel(i, j)).ToArgb() > midrgb) ab = ab * 2 + 1; else ab = ab * 2;
x++;
if (x == 8)
{
buf[k++] = (byte)ab;
ab = 0;
x = 0;
}
}
if (x > 0)
{
//循環實作:剩餘有效資料不滿1位元組的情況下須把它們移往位元組的高位部分
for (int t = x; t < 8; t++) ab = ab * 2;
buf[k++] = (byte)ab;
}
}
Bitmap bb = new Bitmap(img.Width, img.Height, PixelFormat.Format1bppIndexed);
BitmapData dt = bb.LockBits(new Rectangle(0, 0, bb.Width, bb.Height), ImageLockMode.ReadWrite, bb.PixelFormat);
Marshal.Copy(buf, 0, dt.Scan0, buf.Length);
bb.UnlockBits(dt);
bb.Save(@"C:\\Users\\QFXuQ\\Desktop\\99.bmp", ImageFormat.Bmp);
bb.Dispose();
bmp.Dispose();
img.Dispose();
return bb;
}
這個就是試過半天可以生成的,标準的。
源碼項目下載下傳:https://download.csdn.net/download/qq_34159223/10538553