代码
public class ImageFormatTools
{
public class ImageFormatTools
{
#region 判断图像的正确格式
public static ImageFormat GetImageFormat(string filePath)
{
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] headerBytes = br.ReadBytes(16);
if (IsJpeg(headerBytes))
{
return ImageFormat.Jpeg;
}
else if (IsPng(headerBytes))
{
return ImageFormat.Png;
}
else if (IsGif(headerBytes))
{
return ImageFormat.Gif;
}
else if (IsBmp(headerBytes))
{
return ImageFormat.Bmp;
}
else
{
return null;
}
}
}
}
private static bool IsJpeg(byte[] headerBytes)
{
return headerBytes.Length >= 2 && headerBytes[0] == 0xFF && headerBytes[1] == 0xD8;
}
private static bool IsPng(byte[] headerBytes)
{
return headerBytes.Length >= 8 && headerBytes[0] == 137
&& headerBytes[1] == 80 && headerBytes[2] == 78
&& headerBytes[3] == 71 && headerBytes[4] == 13
&& headerBytes[5] == 10 && headerBytes[6] == 26
&& headerBytes[7] == 10;
}
private static bool IsGif(byte[] headerBytes)
{
return headerBytes.Length >= 3 && headerBytes[0] == 71
&& headerBytes[1] == 73 && headerBytes[2] == 70;
}
private static bool IsBmp(byte[] headerBytes)
{
return headerBytes.Length >= 2 && headerBytes[0] == 66
&& headerBytes[1] == 77;
}
#endregion
public static int GetImageChannelCount(string imagePath ,out Bitmap bitmap,out PixelFormat format)
{
try
{
Image image = Image.FromFile(imagePath);
bitmap = new Bitmap(image);
format = bitmap.PixelFormat;
image.Dispose();
switch (format)
{
case PixelFormat.Format8bppIndexed:
return 1;
case PixelFormat.Format24bppRgb:
return 3;
case PixelFormat.Format32bppRgb:
case PixelFormat.Format32bppArgb:
return 4;
default:
return -1;
}
}
catch (Exception ex)
{
throw new ArgumentException($"参数异常:{ex.Message}");
}
}
public static void ReadImage(string path,out HImage hImage,out string info)
{
hImage = new HImage();
Random random = new Random();
info = null;
int channel = GetImageChannelCount(path, out Bitmap bitmap, out PixelFormat format);
switch (format)
{
case PixelFormat.Format8bppIndexed:
try
{
hImage.ReadImage(path);
}
catch (Exception ex)
{
BitmapToHImageBpp8(bitmap, out hImage);
info= ex.Message;
}
break;
case PixelFormat.Format24bppRgb:
try
{
hImage.ReadImage(path);
}
catch (Exception ex)
{
BitmapToHImageBpp24(bitmap, out hImage);
info = ex.Message;
}
break;
case PixelFormat.Format32bppRgb:
case PixelFormat.Format32bppArgb:
try
{
hImage.ReadImage(path);
}
catch (Exception ex)
{
BitmapToHImageBpp32(bitmap, out hImage, format);
info = ex.Message;
}
break;
default:
try
{
hImage.ReadImage(path);
}
catch (Exception ex)
{
BitmapToHImageBpp8(bitmap, out hImage);
info = ex.Message;
}
break;
}
}
public static void BitmapToHImageBpp24(Bitmap bmp, out HImage image)
{
try
{
HObject tempImage = new HObject();
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
HOperatorSet.GenImageInterleaved(out tempImage, srcBmpData.Scan0, "bgr", bmp.Width, bmp.Height, 0, "byte", 0, 0, 0, 0, -1, 0);
bmp.UnlockBits(srcBmpData);
image = (HImage)tempImage.Clone();
}
catch (Exception ex)
{
image = null;
}
}
public static void BitmapToHImageBpp32(Bitmap bmp, out HImage image, PixelFormat pixelFormat,int channel)
{
try
{
image = new HImage();
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, pixelFormat);
image.GenImageInterleaved(srcBmpData.Scan0, "rgbx", bmp.Width, bmp.Height, 0, "byte", bmp.Width, bmp.Height, 0, 0, -1, 0);
bmp.UnlockBits(srcBmpData);
}
catch (Exception ex)
{
image = null;
}
}
public static void BitmapToHImageBpp8(Bitmap bmp,out HImage image)
{
try
{
image = new HImage();
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
image.GenImage1("byte", bmp.Width, bmp.Height, srcBmpData.Scan0);
bmp.UnlockBits(srcBmpData);
}
catch (Exception ex)
{
image = null;
}
}
public static void BitmapToHObjectBpp24(Bitmap bmp ,out HObject image)
{
try
{
image = new HObject();
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
HOperatorSet.GenImageInterleaved(out image, srcBmpData.Scan0, "bgr", bmp.Width, bmp.Height, 0, "byte", 0, 0, 0, 0, -1, 0);
bmp.UnlockBits(srcBmpData);
}
catch (Exception ex)
{
image = null;
}
}
}
public static int GetImageChannelCount(string imagePath ,out Bitmap bitmap,out PixelFormat format)
{
try
{
Image image = Image.FromFile(imagePath);
bitmap = new Bitmap(image);
format = bitmap.PixelFormat;
image.Dispose();
switch (format)
{
case PixelFormat.Format8bppIndexed:
return 1;
case PixelFormat.Format24bppRgb:
return 3;
case PixelFormat.Format32bppRgb:
case PixelFormat.Format32bppArgb:
return 4;
default:
return -1;
}
}
catch (Exception ex)
{
throw new ArgumentException($"参数异常:{ex.Message}");
}
}
public static void BitmapToHImageBpp24(Bitmap bmp, out HImage image)
{
try
{
HObject tempImage = new HObject();
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
HOperatorSet.GenImageInterleaved(out tempImage, srcBmpData.Scan0, "bgr", bmp.Width, bmp.Height, 0, "byte", 0, 0, 0, 0, -1, 0);
bmp.UnlockBits(srcBmpData);
image = (HImage)tempImage.Clone();
}
catch (Exception ex)
{
image = null;
}
}
public static void BitmapToHImageBpp32(Bitmap bmp, out HImage image, PixelFormat pixelFormat)
{
try
{
image = new HImage();
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, pixelFormat);
image.GenImageInterleaved(srcBmpData.Scan0, "rgbx", bmp.Width, bmp.Height, 0, "byte", bmp.Width, bmp.Height, 0, 0, -1, 0);
bmp.UnlockBits(srcBmpData);
}
catch (Exception ex)
{
image = null;
}
}
public static void BitmapToHImageBpp8(Bitmap bmp,out HImage image)
{
try
{
image = new HImage();
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
image.GenImage1("byte", bmp.Width, bmp.Height, srcBmpData.Scan0);
bmp.UnlockBits(srcBmpData);
}
catch (Exception ex)
{
image = null;
}
}
public static void BitmapToHObjectBpp24(Bitmap bmp ,out HObject image)
{
try
{
image = new HObject();
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
HOperatorSet.GenImageInterleaved(out image, srcBmpData.Scan0, "bgr", bmp.Width, bmp.Height, 0, "byte", 0, 0, 0, 0, -1, 0);
bmp.UnlockBits(srcBmpData);
}
catch (Exception ex)
{
image = null;
}
}
}