C#保存图片到文件夹区分8位和24位

时间:2024-01-21 19:40:21

1.保存图像--24位位图(显示的图像,包括增加结果到界面上的数据)

            Image image2 = default(Image);
            image2 = cogRecordDisplay1.CreateContentBitmap(
                Cognex.VisionPro.Display.CogDisplayContentBitmapConstants.Image, null, 0);

            image2.Save(@"D:\Savemap\" + DateTime.Now.ToString("yyyyMMddhhmmssff") + ".bmp", ImageFormat.Bmp);

2.保存图像(输入进来的图像,8位位图,可用作再次取像分析)

 第一种方法:
CogImageFile image1 = new CogImageFile(); image1.Open(@"D:\Savemap\"+ DateTime.Now.ToString("yyyyMMddhhmmssff") + ".bmp",CogImageFileModeConstants.Write); image1.Append(imageFormInput); image1.Close();

第二种方法:

CogImage8Grey cogimage8grey = new CogImage8Grey();
cogimage8grey = imageFormInput as CogImage8Grey;
SaveCognexImage(@"D:\Savemap\" + DateTime.Now.ToString("yyyyMMddhhmmssff") + ".bmp", ref cogimage8grey);


public void SaveCognexImage(string fileName, ref Cognex.VisionPro.CogImage8Grey image)
{
try
{
int W = image.Width;
int H = image.Height;
Cognex.VisionPro.ICogImage8PixelMemory tM = image.Get8GreyPixelMemory(Cognex.VisionPro.CogImageDataModeConstants.Read, 0, 0, W, H);
Bitmap grayscale = new Bitmap(tM.Width, tM.Height, tM.Stride, PixelFormat.Format8bppIndexed, tM.Scan0);
Bitmap bitmap = new Bitmap(1, 1, PixelFormat.Format8bppIndexed);
ColorPalette palette = bitmap.Palette;
for (int i = 0; i <= bitmap.Palette.Entries.Length - 1; i++)
{
palette.Entries[i] = Color.FromArgb(255, i, i, i);
}
bitmap.Dispose();
grayscale.Palette = palette;
grayscale.Save(fileName, ImageFormat.Bmp);
tM.Dispose();
}
catch
{
}
}