设置首页收藏本站
开启左侧

[C#] 分享一下转图方法,转WriteableBitmap

[复制链接]
zizheng 发表于 2024-6-28 11:17:48 | 显示全部楼层 |阅读模式
  1. #halcon 转
  2. public static WriteableBitmap ToWriteableBitmap(this HObject hObject)
  3. {
  4.     HOperatorSet.GenEmptyObj(out var dataHObject);

  5.     var pixelFormat = PixelFormat.Undefined;
  6.     //判断halcon通道个数
  7.     HOperatorSet.CountChannels(hObject, out var channelCount);
  8.     if (channelCount == 1)
  9.     {
  10.         dataHObject = hObject;
  11.         pixelFormat = PixelFormat.Format8bppIndexed;
  12.     }
  13.     else if (channelCount == 3)
  14.     {
  15.         //把三通道生成交错图片
  16.         HOperatorSet.InterleaveChannels(hObject, out dataHObject, "rgb", "match", 0);
  17.         pixelFormat = PixelFormat.Format24bppRgb;
  18.     }
  19.     HOperatorSet.GetImagePointer1(dataHObject, out var imagePtr, out var type, out var width, out var height);
  20.     if (channelCount == 3)
  21.     {
  22.         width /= 3;
  23.     }
  24.     var strideInfo = new BitmapStrideInfo(width, height, pixelFormat);
  25.     WriteableBitmap writeableBitmap = null;
  26.     if (channelCount == 1)
  27.     {
  28.         writeableBitmap =
  29.             new WriteableBitmap(width, height, 96, 96, PixelFormats.Indexed8, BitmapPalettes.Gray256);
  30.     }
  31.     else if(channelCount == 3)
  32.     {
  33.         writeableBitmap =
  34.             new WriteableBitmap(width, height, 96, 96, PixelFormats.Rgb24, null);
  35.     }
  36.     else
  37.     {
  38.         throw new NotImplementedException($"{channelCount}通道数暂不支持");
  39.     }
  40.     try
  41.     {
  42.         writeableBitmap.Lock();
  43.         unsafe
  44.         {
  45.             var readSpan = new Span<byte>(imagePtr.IP.ToPointer(), strideInfo.EfficientTotalSize);
  46.             var writeSpan = new Span<byte>(writeableBitmap.BackBuffer.ToPointer(), strideInfo.Stride * strideInfo.Height);

  47.             for (int i = 0; i < strideInfo.Height; i++)
  48.             {
  49.                 readSpan.Slice(i * strideInfo.EfficientPerRowBytes, strideInfo.EfficientPerRowBytes)
  50.                     .CopyTo(writeSpan.Slice(i * strideInfo.Stride, strideInfo.EfficientPerRowBytes));
  51.             }
  52.         }
  53.         writeableBitmap.AddDirtyRect(new Int32Rect(0,0,width,height));

  54.         return writeableBitmap;
  55.     }
  56.     finally
  57.     {
  58.         writeableBitmap.Unlock();
  59.     }
  60. }
复制代码
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
 楼主| zizheng 发表于 2024-6-28 13:19:12 | 显示全部楼层
自己顶一个
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
lyyyy 发表于 2024-6-28 14:37:49 | 显示全部楼层
感谢分享,学习一下
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
北方的风 发表于 2024-8-15 15:57:28 | 显示全部楼层
请问BitmapStrideInfo类是哪个命名空间的?我这里没有这个类
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
Owen-Zang 发表于 2024-11-18 16:58:16 | 显示全部楼层
同问三楼内容
奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表