using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace ConvertBitmapToArray
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Bitmap bitmap = new Bitmap(@"C:\Temp\Gimp Maps\Bitmaps\test1.bmp");
CreateArray(bitmap);
File.Create(@"C:\Temp\Gimp Maps\Maps.cs");
}
private void CreateArray(Bitmap bmp)
{
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);
IntPtr ptr = bmpData.Scan0;
int bytes = Math.Abs(bmpData.Stride) * bmp.Height;
byte[] rgbValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
bmp.UnlockBits(bmpData);
for (int i = 0; i < rgbValues.Count(); i++)
{
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
我有一个黑白尺寸8x8或800x800的位图,我得到了rgbValues数组。 现在循环:
for (int i = 0; i < rgbValues.Count(); i++)
{
}
我想创建从0和1构建的2d int数组。例如:
int[,] map = new int[,]
{
{0,0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,0,},
{1,1,1,1,0,0,0,0,},
{0,0,0,1,0,0,0,0,},
{0,0,1,1,0,0,0,0,},
{0,0,0,0,1,1,1,0,},
{0,0,0,0,0,0,0,1,},
};
因此255将为0而0将为1.在int数组示例中,1为黑色,0为白色。
我试过了:
private void CreateArray(Bitmap bmp)
{
int[] pix = new int[bmp.Width*bmp.Height];
int x, y, rgb, val;
for (y = 0; y < bmp.Height; y++)
{
for (x = 0; x < bmp.Width; x++)
{
Color c = bmp.GetPixel(x, y);
rgb = c.ToArgb();
if (rgb == 0xff000000) // if black
{
val = 0;
}
else
val = 1;
pix[y * bmp.Width + x] = val;
}
}
}
但是我不确定Color c和rgb = c.ToArgb();
在if条件下,我得到绿线说:
警告1 与积分常数的比较是没用的;常量超出了'int'类型的范围
如何使用LockBits或GetPixel执行此操作?我想知道使用每种方法有多么不同。
这就是我到目前为止所做的:
private void CreateArray(Bitmap bmp)
{
int[,] array = new int[,]
{
};
// Lock the bitmap's bits.
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData =
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
bmp.PixelFormat);
// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;
// Declare an array to hold the bytes of the bitmap.
int bytes = Math.Abs(bmpData.Stride) * bmp.Height;
byte[] rgbValues = new byte[bytes];
int x, y, rgb, val;
for (y = 0; y < bmp.Height; y++)
{
for (x = 0; x < bmp.Width; x++)
{
var row = ptr + (y * bmpData.Stride);
var pixel = row + x * ; // bpp will be 4 in your case
// A bit=0
// R bit=1
// G bit=2
// B bit=3
// (depending on your image pixel format)
for (var bit = 0; bit < bpp; bit++)
{
var pixelComponent = pixel[bit];
}
}
}
}
答案 0 :(得分:3)
要以更合理的方式遍历图像的字节,请将unsafe
关键字添加到函数签名(private unsafe void ...
),并确保通过项目属性启用unsafe
代码
var pt = (byte*)bmpData.Scan0;
现在您可以轻松遍历x和y:
var row = pt + (y * bmpData.Stride);
var pixel = row + x * bpp; // bpp will be 4 in your case
// A bit=0
// R bit=1
// G bit=2
// B bit=3
// (depending on your image pixel format)
for (var bit = 0; bit < bpp; bit++)
{
var pixelComponent = pixel[bit];
}
如果您确实希望最终结果为二维数组,则应使用[,]
或[][]
代替[]
(单维数组)
LockBits
方法与GetPixel
方法之间的巨大差异是性能。 GetPixel
实际上锁定了这些位,检索了一个像素并再次解锁了这些位 - 这意味着在多次重复执行时会出现可怕的性能。
修改强>
以下是完整的代码:
private unsafe void CreateArray(Bitmap bmp)
{
// Note that is it somewhat a standard
// to define 2d array access by [y,x] (not [x,y])
bool[,] bwValues = new bool[bmp.Height, bmp.Width];
// Lock the bitmap's bits.
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData =
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
bmp.PixelFormat);
// Get the address of the first line.
byte* ptr = (byte*)bmpData.Scan0;
// Declare an array to hold the bytes of the bitmap.
int bytes = Math.Abs(bmpData.Stride) * bmp.Height;
for (int y = 0; y < bmp.Height; y++)
{
var row = ptr + (y * bmpData.Stride);
for (int x = 0; x < bmp.Width; x++)
{
var pixel = row + x * 4; // ARGB has 4 bytes per pixel
// A bit=0
// R bit=1
// G bit=2
// B bit=3
// (depending on your image pixel format)
// Check if A = R = G = B = 255 (meaning the pixel is white)
bool isWhite = (pixel[0] == 255 &&
pixel[1] == 255 &&
pixel[2] == 255 &&
pixel[3] == 255);
// Assume that anything that isn't white is black
bwValues[y, x] = isWhite;
}
}
// Do whatever you want with vwValues here
}