bmp转Hobject

bmp转Hobject基本代码

public void Bitmap2HObjectBpp8(Bitmap SrcImage, out HObject image)
        {
            try
            {
                Point po = new Point(0, 0);
                Size so = new Size(SrcImage.Width, SrcImage.Height);//template.Width, template.Height
                Rectangle ro = new Rectangle(po, so);
 
                Bitmap DstImg = new Bitmap(SrcImage.Width, SrcImage.Height, PixelFormat.Format8bppIndexed);
                DstImg = SrcImage.Clone(ro, PixelFormat.Format8bppIndexed);
 
                Rectangle rect = new Rectangle(0, 0, DstImg.Width, DstImg.Height);
                BitmapData srcBmpData = DstImg.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
                HOperatorSet.GenImage1(out image, "byte", DstImg.Width, DstImg.Height, srcBmpData.Scan0);
                DstImg.UnlockBits(srcBmpData);
                
            }
            catch (Exception ex)
            {
                image = null;
            }
        }

bmp转Hobject进阶代码

public static HObject BitmapToHImage(Bitmap SrcImage)
{
HObject Hobj;
HOperatorSet.GenEmptyObj(out Hobj);

        Point po = new Point(0, 0);
        Size so = new Size(SrcImage.Width, SrcImage.Height);//template.Width, template.Height
        Rectangle ro = new Rectangle(po, so);

        Bitmap DstImage = new Bitmap(SrcImage.Width, SrcImage.Height, PixelFormat.Format8bppIndexed);
        DstImage = SrcImage.Clone(ro, PixelFormat.Format8bppIndexed);

        int width = DstImage.Width;
        int height = DstImage.Height;
        
        Rectangle rect = new Rectangle(0, 0, width, height);
        System.Drawing.Imaging.BitmapData dstBmpData =
            DstImage.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);//pImage.PixelFormat
        int PixelSize = Bitmap.GetPixelFormatSize(dstBmpData.PixelFormat) / 8;
        int stride = dstBmpData.Stride;
        
        //重点在此
        unsafe
        {
            int count = height * width;
            byte[] data = new byte[count];
            byte* bptr = (byte*)dstBmpData.Scan0;
            fixed (byte* pData = data)
            {
                for (int i = 0; i < height; i++)
                    for (int j = 0; j < width; j++ )
                        {
                             data[i * width + j ] = bptr[i * stride + j];
                        }
                HOperatorSet.GenImage1(out Hobj, "byte", width, height, new IntPtr(pData));
            }
        }

        DstImage.UnlockBits(dstBmpData);

        return Hobj;
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值