#region 灰度圖轉二維數組
/// <summary>
/// 灰度圖轉二維數組
/// </summary>
/// <param name="bitmap">灰度圖</param>
/// <returns></returns>
public double[,] grayPictureToTwoArray(Bitmap bitmap)
{
int width = bitmap.Width;
int height = bitmap.Height;
double[,] result = new double[width, height];
Rectangle rect = new Rectangle(0, 0, width, height);
System.Drawing.Imaging.BitmapData bmpData = bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);
IntPtr ptr = bmpData.Scan0;
int bytes = bmpData.Stride * height;
byte[] rgbValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
//前3位地址是分別是BGR,第4位爲A(alpha)
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
result[i, j] = rgbValues[(j * width + i)];
}
}
bitmap.UnlockBits(bmpData);
return result;
}
#endregion
#region 灰度二維數組轉灰度圖
/// <summary>
/// 灰度二維數組轉灰度圖
/// </summary>
/// <param name="imageArray">存儲灰度值的二維數組</param>
/// <returns></returns>
public Bitmap grayTwoArrayToGrayPicture(double[,] imageArray)
{
Bitmap resultBitmap = new Bitmap(imageArray.GetLength(0), imageArray.GetLength(1));
int width = resultBitmap.Width;
int height = resultBitmap.Height;
Rectangle rect = new Rectangle(0, 0, width, height);
System.Drawing.Imaging.BitmapData bmpData = resultBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, resultBitmap.PixelFormat);
IntPtr ptr = bmpData.Scan0;
int bytes = bmpData.Stride * height;
byte[] rgbValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
//前3位地址是分別是BGR,第4位爲A(alpha)
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
int temp = (j * width + i) << 2;
rgbValues[temp + 3] = 255;
rgbValues[temp + 2] = rgbValues[temp + 1] = rgbValues[temp] = (byte)(imageArray[i, j]*255);
}
}
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
resultBitmap.UnlockBits(bmpData);
return resultBitmap;
}
#endregion
//創建光柵圖
public void create()
{
int width = 1920, height = 1080;
Bitmap[,] bitmap = new Bitmap[3, 4];
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 4; ++j)
{
bitmap[i, j] = new Bitmap(width, height);
}
}
int[] f = { 73, 64, 56 };
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 4; ++j)
{
//圖像轉換爲數組
double[,] Ce = grayPictureToTwoArray(bitmap[i, j]);
for (int k = 0; k < width; ++k)
{
for (int w = 0; w < height; ++w)
{
Ce[k, w] = 0.5 + 0.5 * Math.Cos(2 * Math.PI * (k ) * f[i] / (height) + j * Math.PI / 2);
}
}
//數組轉換爲圖像,保存到本地
Bitmap temp = grayTwoArrayToGrayPicture(Ce);
temp.Save("D:\\image" + (i + 1) + "_" + (j + 1) + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}
}
}