[英]Save byte array as 1 Bit image file (.png or bmp.)
我有一個字節數組,我想將它保存為圖像文件(.png 或 .bmp)。 我的字節數組中的每一位都代表一個像素。 圖像應為黑色\/白色。 我的代碼只提供黑色圖像(所有像素均為黑色),無論字節數組數據如何,我都找不到問題。
我有以下代碼:
byte[] image_bitmap = FillMyArray();
// Here create the Bitmap to the height, width and format
Bitmap bmp = new Bitmap(image_width, image_height, PixelFormat.Format1bppIndexed);
// Create a BitmapData and Lock all pixels to be written
BitmapData bmpData = bmp.LockBits(
new Rectangle(0, 0, bmp.Width, bmp.Height),
ImageLockMode.WriteOnly, bmp.PixelFormat);
// Copy the data from the byte array into BitmapData.Scan0
Marshal.Copy(image_bitmap, 0, bmpData.Scan0, image_bitmap.Length);
// Unlock the pixels
bmp.UnlockBits(bmpData);
// save images (unfortunately all pixels are always black)
bmp.Save("file1.bmp", ImageFormat.Bmp);
bmp.Save("file2.png", ImageFormat.Png);
bmpData 全部傳入
字節為 0
bmpData 零表示黑色數據可能不會出現
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.