相机SDK取像获取halcon数据类型格式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wuguanghao/article/details/77150775

当我们使用相机SDK采集图像时,得到的往往是Bitmap图像,在这给大家分享一下自己写的Bitmap如何转换成HImage的代码,希望能帮到大家。

    /// <summary> 
    /// 彩色图Bitmap转换成HImage 
    /// </summary> 
    /// <param name="bImage"></param> 
    /// <returns></returns> 
    HImage Bitmap2HImage_24(Bitmap bImage) 
    { 
        Bitmap bImage24; 
        BitmapData bmData = null; 
        Rectangle rect; 
        IntPtr pBitmap; 
        IntPtr pPixels; 
        HImage hImage = new HImage(); 


        rect = new Rectangle(0, 0, bImage.Width, bImage.Height); 
        bImage24 = new Bitmap(bImage.Width, bImage.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bImage24); 
        g.DrawImage(bImage, rect); 
        g.Dispose(); 


        bmData = bImage24.LockBits(rect, ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); 
        pBitmap = bmData.Scan0; 
        pPixels = pBitmap; 


        hImage.GenImageInterleaved(pPixels, "bgr", bImage.Width, bImage.Height, -1, "byte", 0, 0, 0, 0, -1, 0); 


        bImage24.UnlockBits(bmData); 


        return hImage; 
    } 


    /// <summary> 
    /// Bitmap转换成HImage 
    /// </summary> 
    /// <param name="bImage"></param> 
    /// <returns></returns> 
    HImage Bitmap2HImage_8(Bitmap bImage) 
    { 
        Bitmap bImage8; 
        BitmapData bmData = null; 
        Rectangle rect; 
        IntPtr pBitmap; 
        IntPtr pPixels; 
        HImage hImage = new HImage(); 


        rect = new Rectangle(0, 0, bImage.Width, bImage.Height); 
        bImage8 = new Bitmap(bImage.Width, bImage.Height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed); 
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bImage8); 
        g.DrawImage(bImage, rect); 
        g.Dispose(); 


        bmData = bImage8.LockBits(rect, ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format8bppIndexed); 
        pBitmap = bmData.Scan0; 
        pPixels = pBitmap; 


        hImage.GenImage1("byte", bImage.Width, bImage.Height, pPixels); 


        bImage8.UnlockBits(bmData); 


        return hImage; 
    }

猜你喜欢

转载自blog.csdn.net/wuguanghao/article/details/77150775
今日推荐