C# bitmap图像和halcon图像

 

using System.Drawing;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Navigation;
using System.Windows.Shapes;
using HalconDotNet;
using Microsoft.Win32;
using System;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;

 

var fileName = ImageFileDialog.FileName;
var image = new HImage();
image.ReadImage(fileName);
Hsmart.HalconWindow.DispObj(image);

HTuple ImageData = new HTuple();
HTuple tempType = new HTuple();
HTuple tempW = new HTuple();
HTuple tempH = new HTuple();

HOperatorSet.GetImagePointer1(image,out ImageData,out tempType,out tempW,out tempH);


System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(tempW.I, tempH.I, tempW.I * 1, System.Drawing.Imaging.PixelFormat.Format8bppIndexed, ImageData);

ColorPalette palette = bitmap.Palette;
for (int i = 0; i < 256; i++)
palette.Entries[i] = System.Drawing.Color.FromArgb(i, i, i);
bitmap.Palette = palette;

int x = 320; // 切图的起始X坐标
int y = 490; // 切图的起始Y坐标
int width = 600; // 切图的宽度
int height = 160; // 切图的高度
System.Drawing.Rectangle cropRect = new System.Drawing.Rectangle(x, y, width, height);

// 现在你可以使用bitmap对象进行各种操作
bitmap.Save("output.bmp", System.Drawing.Imaging.ImageFormat.Bmp);

Bitmap croppedImage = bitmap.Clone(cropRect, bitmap.PixelFormat);
croppedImage.Save("CropOutput.bmp", System.Drawing.Imaging.ImageFormat.Bmp);

posted @ 2025-02-16 11:37  QuincyYi  阅读(86)  评论(0)    收藏  举报