📄 bitmapconvert.cs
字号:
//////////////////////////////////////////////////////////////////////////////////////////
// 注意:图像中的PixelFormat的类型.以下是针对24位及32位位图,若是其它位图无法处理。
//如:32位图int nOffset = stride - bm.Width * 4; pbmpdata += 4;
// 24位图int nOffset = stride - bm.Width * 3; pbmpdata += 3;
//////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
class BitmapConvert
{
/// <summary>
/// 将二维数据转化为对应的Bitmap像素值
/// </summary>
/// <param name="sourseData"></源数据>
/// <returns></24位位图>
public static Bitmap ConvertToBitmap(double[,] sourseData)
{
int height = sourseData.GetLength(0), width = sourseData.GetLength(1);
Bitmap resultBmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
BitmapData bmData = resultBmp.LockBits(new Rectangle(0, 0, resultBmp.Width, resultBmp.Height),
ImageLockMode.ReadWrite, resultBmp.PixelFormat);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
int nOffset = stride - resultBmp.Width * 3;
unsafe
{
byte* pbmpdata = (byte*)(void*)Scan0;
try
{
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
pbmpdata[0] = (byte)sourseData[y, x];
pbmpdata[1] = pbmpdata[0];
pbmpdata[2] = pbmpdata[0];
pbmpdata += 3;
}
pbmpdata += nOffset;
}
}
catch (Exception ex)
{
String mes = ex.ToString();
resultBmp.UnlockBits(bmData);
return null;
}
}
resultBmp.UnlockBits(bmData);
return resultBmp;
}
/// <summary>
/// 形成特定颜色的Bitmap
/// </summary>
/// <param name="sz"></要形成位图的大小>
/// <param name="clr1"></要形成位图的颜色>
/// <returns></24位位图>
public static Bitmap ConvertToBitmap(Size sz, Color cl)
{
Bitmap resultBmp = new Bitmap(sz.Width, sz.Height,
PixelFormat.Format24bppRgb);
BitmapData bmData = resultBmp.LockBits(new Rectangle(0, 0, resultBmp.Width, resultBmp.Height),
ImageLockMode.ReadWrite, resultBmp.PixelFormat);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
int nOffset = stride - resultBmp.Width * 3;
unsafe
{
byte* pbmpdata = (byte*)(void*)Scan0;
try
{
for (int y = 0; y < resultBmp.Height; ++y)
{
for (int x = 0; x < resultBmp.Width; ++x)
{
pbmpdata[0] = cl.B;
pbmpdata[1] = cl.G;
pbmpdata[2] = cl.R;
pbmpdata += 3;
}
pbmpdata += nOffset;
}
}
catch (Exception ex)
{
String mes = ex.ToString();
resultBmp.UnlockBits(bmData);
return null;
}
}
resultBmp.UnlockBits(bmData);
return resultBmp;
}
/// <summary>
/// 形成特定颜色的Bitmap, 并在矩形区域内画上另一颜色
/// </summary>
/// <param name="sz"></形成图像的尺寸>
/// <param name="clr1"></默认的颜色>
/// <param name="rect"></要画特别颜色的矩形区域>
/// <param name="clr2"></特别的颜色>
/// <returns></returns>
public static Bitmap ConvertToBitmap(Size sz, Color clr1, Rectangle rect, Color clr2)
{
Bitmap resultBmp = new Bitmap(sz.Width, sz.Height,
PixelFormat.Format24bppRgb);
BitmapData bmData = resultBmp.LockBits(new Rectangle(0, 0, resultBmp.Width, resultBmp.Height),
ImageLockMode.ReadWrite, resultBmp.PixelFormat);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
int nOffset = stride - resultBmp.Width * 3;
Color cl;
unsafe
{
byte* pbmpdata = (byte*)(void*)Scan0;
try
{
for (int y = 0; y < resultBmp.Height; ++y)
{
for (int x = 0; x < resultBmp.Width; ++x)
{
if (y >= rect.Top && y <= rect.Bottom &&
x >= rect.Left && x <= rect.Right)
{
cl = clr2;
}
else
cl = clr1;
pbmpdata[0] = cl.B;
pbmpdata[1] = cl.G;
pbmpdata[2] = cl.R;
pbmpdata += 3;
}
pbmpdata += nOffset;
}
}
catch (Exception ex)
{
String mes = ex.ToString();
resultBmp.UnlockBits(bmData);
return null;
}
}
resultBmp.UnlockBits(bmData);
return resultBmp;
}
/// <summary>
/// 按查找表形成Bitmap
/// </summary>
/// <param name="sourseData"></源数据>
/// <param name="lut"></查找表>
/// <param name="minValue"></最小值>
/// <returns></24位位图>
public static Bitmap ConvertToBitmap(int[,] sourseData, int[] lut, int minValue)
{
int height = sourseData.GetLength(0), width = sourseData.GetLength(1);
Bitmap resultBmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
BitmapData bmData = resultBmp.LockBits(new Rectangle(0, 0, resultBmp.Width, resultBmp.Height),
ImageLockMode.ReadWrite, resultBmp.PixelFormat);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
int nOffset = stride - resultBmp.Width * 3;
unsafe
{
byte* pbmpdata = (byte*)(void*)Scan0;
try
{
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
pbmpdata[0] = (byte)lut[sourseData[y, x] - minValue];
pbmpdata[1] = pbmpdata[0];
pbmpdata[2] = pbmpdata[0];
pbmpdata += 3;
}
pbmpdata += nOffset;
}
}
catch (Exception ex)
{
String mes = ex.ToString();
resultBmp.UnlockBits(bmData);
return null;
}
}
resultBmp.UnlockBits(bmData);
return resultBmp;
}
/// <summary>
/// 将Bitmap像素值转化为二维数据
/// </summary>
public static int[,] ConvertToData(Bitmap bm)
{
int[,] resultData = new int[bm.Height, bm.Width];
int OffsetByPixel = 3;
PixelFormat bmformat = bm.PixelFormat;
if (bmformat == PixelFormat.Format8bppIndexed)
OffsetByPixel = 1;
else if (bmformat == PixelFormat.Format24bppRgb)
OffsetByPixel = 3;
else if ((bmformat == PixelFormat.Format32bppRgb) || (bmformat == PixelFormat.Format32bppArgb) || (bmformat == PixelFormat.Format32bppPArgb))
OffsetByPixel = 4;
else
throw new ArgumentException();
BitmapData bmData = bm.LockBits(new Rectangle(0, 0, bm.Width, bm.Height),
ImageLockMode.ReadWrite, bm.PixelFormat);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
int nOffset = stride - bm.Width * OffsetByPixel;
unsafe
{
byte* pbmpdata = (byte*)(void*)Scan0;
try
{
for (int y = 0; y < bm.Height; ++y)
{
for (int x = 0; x < bm.Width; ++x)
{
resultData[y, x] = pbmpdata[0];
pbmpdata += OffsetByPixel;
}
pbmpdata += nOffset;
}
}
catch (Exception ex)
{
String mes = ex.ToString();
bm.UnlockBits(bmData);
return null;
}
}
bm.UnlockBits(bmData);
return resultData;
}
/// <summary>
/// 将矩形区域内的Bitmap像素值转化为二维数据
/// Note:有问题
/// </summary>
/// <param name="bm"></param>
/// <param name="rect"></param>
/// <returns></returns>
public static double[,] ConvertToData(Bitmap bm, Rectangle rect)
{
double[,] resultData = new double[rect.Height, rect.Width];
int OffsetByPixel = 3;
PixelFormat bmformat = bm.PixelFormat;
if (bmformat == PixelFormat.Format8bppIndexed)
OffsetByPixel = 1;
else if (bmformat == PixelFormat.Format24bppRgb)
OffsetByPixel = 3;
else if ((bmformat == PixelFormat.Format32bppRgb) || (bmformat == PixelFormat.Format32bppArgb) || (bmformat == PixelFormat.Format32bppPArgb))
OffsetByPixel = 4;
else
throw new ArgumentException();
BitmapData bmData = bm.LockBits(new Rectangle(0, 0, bm.Width, bm.Height),
ImageLockMode.ReadWrite, bm.PixelFormat);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
int nOffset = stride - bm.Width * OffsetByPixel;
unsafe
{
byte* pbmpdata = (byte*)(void*)Scan0;
try
{
pbmpdata += rect.Y * (bm.Width * 3 + nOffset);
for (int i = 0; i < rect.Height; ++i)
{
pbmpdata += rect.X * 3;
for (int j = 0; j < rect.Width; ++j)
{
resultData[i, j] = pbmpdata[0];
pbmpdata += OffsetByPixel;
}
pbmpdata += (bm.Width - rect.Width - rect.X) * 3 + nOffset;
}
}
catch (Exception ex)
{
String mes = ex.ToString();
bm.UnlockBits(bmData);
return null;
}
}
bm.UnlockBits(bmData);
return resultData;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -